Skip to content

Commit

Permalink
Added test for input validation service
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverschwendener committed Nov 22, 2018
1 parent 81eed08 commit 6b95fdc
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 15 deletions.
39 changes: 31 additions & 8 deletions src/tests/unit/input-validation-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { SearchResultItem } from "../../ts/search-result-item";
import { UserConfigOptions } from "../../ts/user-config/user-config-options";
import { WebSearch } from "../../ts/web-search";
import { WebSearchBuilder } from "../../ts/builders/web-search-builder";
import { InputValidatorSearcherCombination } from "../../ts/input-validator-searcher-combination";

describe(InputValidationService.name, (): void => {
const config = {
Expand All @@ -24,7 +25,7 @@ describe(InputValidationService.name, (): void => {

const shouldBlockOtherSearchers = false;

const combinations = [
const combinations: InputValidatorSearcherCombination[] = [
{
searcher: new FakeSearcher(shouldBlockOtherSearchers, [{ name: "Search Result 1" }] as SearchResultItem[]),
validator: new FakeInputValidator(true),
Expand Down Expand Up @@ -55,7 +56,7 @@ describe(InputValidationService.name, (): void => {

it("should return an empty array if user input matches none of the searchers", (): void => {
const shouldBlockOtherSearchers = false;
const combinations = [
const combinations: InputValidatorSearcherCombination[] = [
{
searcher: new FakeSearcher(shouldBlockOtherSearchers, [{ name: "Search Result 1" }] as SearchResultItem[]),
validator: new FakeInputValidator(false),
Expand All @@ -79,7 +80,7 @@ describe(InputValidationService.name, (): void => {

it("should return all items if user input matches all searchers", (): void => {
const shouldBlockOtherSearchers = false;
const combinations = [
const combinations: InputValidatorSearcherCombination[] = [
{
searcher: new FakeSearcher(shouldBlockOtherSearchers, [{ name: "Search Result 1" }] as SearchResultItem[]),
validator: new FakeInputValidator(true),
Expand All @@ -102,7 +103,7 @@ describe(InputValidationService.name, (): void => {

it("should return only the items that match the user input", (): void => {
const shouldBlockOtherSearchers = false;
const combinations = [
const combinations: InputValidatorSearcherCombination[] = [
{
searcher: new FakeSearcher(shouldBlockOtherSearchers, [{ name: "Search Result 1" }] as SearchResultItem[]),
validator: new FakeInputValidator(false),
Expand All @@ -127,7 +128,7 @@ describe(InputValidationService.name, (): void => {

it("should return empty search result if no fallback search results are defined and user input does not match any searcher", (): void => {
const shouldBlockOtherSearchers = false;
const combinations = [
const combinations: InputValidatorSearcherCombination[] = [
{
searcher: new FakeSearcher(shouldBlockOtherSearchers, [{ name: "Search Result 1" }] as SearchResultItem[]),
validator: new FakeInputValidator(false),
Expand Down Expand Up @@ -164,7 +165,7 @@ describe(InputValidationService.name, (): void => {

const shouldBlockOtherSearchers = false;

const combinations = [
const combinations: InputValidatorSearcherCombination[] = [
{
searcher: new FakeSearcher(shouldBlockOtherSearchers, [{ name: "Search Result 1" }] as SearchResultItem[]),
validator: new FakeInputValidator(false),
Expand Down Expand Up @@ -199,7 +200,7 @@ describe(InputValidationService.name, (): void => {
config.webSearches = [webSearch];

const shouldBlockOtherSearchers = false;
const combinations = [
const combinations: InputValidatorSearcherCombination[] = [
{
searcher: new FakeSearcher(shouldBlockOtherSearchers, [{ name: "Search Result 1" }] as SearchResultItem[]),
validator: new FakeInputValidator(false),
Expand All @@ -224,7 +225,7 @@ describe(InputValidationService.name, (): void => {
});

it("should only return search results from first blocking searcher", (): void => {
const combinations = [
const combinations: InputValidatorSearcherCombination[] = [
{
searcher: new FakeSearcher(false, [{ name: "Search Result 1" }] as SearchResultItem[]),
validator: new FakeInputValidator(true),
Expand All @@ -246,5 +247,27 @@ describe(InputValidationService.name, (): void => {
expect(searchResults.length).toBe(1);
expect(searchResults[0].name).toBe("Search Result 3");
});

it("should slice the search results correctly", (): void => {
const limit = 3;
const newConfig = { searchEngineLimit: limit } as UserConfigOptions;
const userInput = "abc";

const combinations: InputValidatorSearcherCombination[] = [
{
searcher: new FakeSearcher(true, [
{ name: "a" },
{ name: "b" },
{ name: "c" },
{ name: "d" },
{ name: "e" },
] as SearchResultItem[]),
validator: new FakeInputValidator(true),
},
];

const actual = new InputValidationService(newConfig, combinations).getSearchResult(userInput);
expect(actual.length).toBe(limit);
});
});
});
2 changes: 1 addition & 1 deletion src/ts/input-validation-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ export class InputValidationService {
result = fallBackWebSearchSearcher.getSearchResult(userInput);
}

return result;
return result.slice(0, this.configOptions.searchEngineLimit);
}
}
12 changes: 6 additions & 6 deletions src/ts/production/production-searchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,7 @@ export class ProductionSearchers {
const countManager = new CountManager(new CountFileRepository(UeliHelpers.countFilePath));
const environmentVariableCollection = process.env as { [key: string]: string };

const result: InputValidatorSearcherCombination[] = [
{
searcher: new SearchPluginsSearcher(config, countManager, new ProductionSearchPluginManager(config, environmentVariableCollection)),
validator: new SearchPluginsInputValidator(),
},
];
const result: InputValidatorSearcherCombination[] = [];

if (config.features.calculator) {
result.push({
Expand Down Expand Up @@ -82,6 +77,11 @@ export class ProductionSearchers {
});
}

result.push({
searcher: new SearchPluginsSearcher(config, countManager, new ProductionSearchPluginManager(config, environmentVariableCollection)),
validator: new SearchPluginsInputValidator(),
});

return result;
}
}

0 comments on commit 6b95fdc

Please sign in to comment.