Skip to content

Commit

Permalink
Rewrite Searcher interface to return promise
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverschwendener committed Nov 15, 2018
1 parent d03bd4b commit 3353df8
Show file tree
Hide file tree
Showing 24 changed files with 271 additions and 220 deletions.
40 changes: 22 additions & 18 deletions src/tests/integration/search-plugins/file-path-searcher.test.ts
Expand Up @@ -51,36 +51,40 @@ describe(FilePathSearcher.name, (): void => {

it("should return all files and folders when passing in a file path to an existing folder", (): void => {
const userInput = testFolder;
const actual = searcher.getSearchResult(userInput);
const expected = testFiles.length + subFolders.length;
expect(actual.length).toBe(expected);
searcher.getSearchResult(userInput).then((result) => {
const expected = testFiles.length + subFolders.length;
expect(result.length).toBe(expected);
});
});

it("should return a single entry when passing in a file path to an existing file", (): void => {
const userInput = join(testFolder, testFiles[0]);
const actual = searcher.getSearchResult(userInput);
expect(actual.length).toBe(1);
expect(actual[0].description).toBe(FilePathDescriptionBuilder.buildFilePathDescription(userInput));
expect(actual[0].executionArgument).toBe(userInput);
expect(actual[0].icon).toBe(testIconSet.fileIcon);
expect(actual[0].name).toBe(basename(userInput));
expect(actual[0].searchable.length).toBe(1);
expect(actual[0].searchable[0]).toBe(basename(userInput));
searcher.getSearchResult(userInput).then((result) => {
expect(result.length).toBe(1);
expect(result[0].description).toBe(FilePathDescriptionBuilder.buildFilePathDescription(userInput));
expect(result[0].executionArgument).toBe(userInput);
expect(result[0].icon).toBe(testIconSet.fileIcon);
expect(result[0].name).toBe(basename(userInput));
expect(result[0].searchable.length).toBe(1);
expect(result[0].searchable[0]).toBe(basename(userInput));
});
});

it("should sort the search results", (): void => {
const userInput = join(testFolder, "abc");
const actual = searcher.getSearchResult(userInput);
expect(actual.length).toBe(3);
expect(basename(actual[0].executionArgument)).toBe(testFiles.sort()[0]);
expect(basename(actual[1].executionArgument)).toBe(testFiles.sort()[1]);
expect(basename(actual[2].executionArgument)).toBe(testFiles.sort()[2]);
searcher.getSearchResult(userInput).then((result) => {
expect(result.length).toBe(3);
expect(basename(result[0].executionArgument)).toBe(testFiles.sort()[0]);
expect(basename(result[1].executionArgument)).toBe(testFiles.sort()[1]);
expect(basename(result[2].executionArgument)).toBe(testFiles.sort()[2]);
});
});

it("should return an empty array when passing in a file path to a file that does not exist", (): void => {
const userInput = join(testFolder, "non-existing-folder", "non-existing-file");
const actual = searcher.getSearchResult(userInput);
expect(actual.length).toBe(0);
searcher.getSearchResult(userInput).then((result) => {
expect(result.length).toBe(0);
});
});
});
});
6 changes: 4 additions & 2 deletions src/tests/unit/fake-searcher.ts
Expand Up @@ -11,7 +11,9 @@ export class FakeSearcher implements Searcher {
this.searchResultItems = searchResultItems;
}

public getSearchResult(userInput: string): SearchResultItem[] {
return this.searchResultItems;
public getSearchResult(userInput: string): Promise<SearchResultItem[]> {
return new Promise((resolve, reject) => {
resolve(this.searchResultItems);
});
}
}
76 changes: 31 additions & 45 deletions src/tests/unit/input-validation-service.test.ts
Expand Up @@ -14,7 +14,6 @@ describe(InputValidationService.name, (): void => {

describe("getSearchResults", () => {
it("should return an empty array if user input is an empty string, undefined or null", (): void => {

const userInputs = [
"",
" ",
Expand All @@ -39,18 +38,17 @@ describe(InputValidationService.name, (): void => {
},
];

const inputValidationService = new InputValidationService(config, combinations);

for (const userInput of userInputs) {
const actual = inputValidationService.getSearchResult(userInput as string);
expect(actual.length).toBe(0);
new InputValidationService(config, combinations).getSearchResult(userInput as string).then((result: SearchResultItem[]) => {
expect(result.length).toBe(0);
});
}
});

it("should return an empty array when combinations are an empty array", () => {
const inputValidationService = new InputValidationService(config, []);
const actual = inputValidationService.getSearchResult("anything");
expect(actual.length).toBe(0);
new InputValidationService(config, []).getSearchResult("anything").then((result: SearchResultItem[]) => {
expect(result.length).toBe(0);
});
});

it("should return an empty array if user input matches none of the searchers", (): void => {
Expand All @@ -70,11 +68,9 @@ describe(InputValidationService.name, (): void => {
},
];

const inputValidationService = new InputValidationService(config, combinations);

const actual = inputValidationService.getSearchResult("something");

expect(actual.length).toBe(0);
new InputValidationService(config, combinations).getSearchResult("something").then((result: SearchResultItem[]) => {
expect(result.length).toBe(0);
});
});

it("should return all items if user input matches all searchers", (): void => {
Expand All @@ -93,11 +89,9 @@ describe(InputValidationService.name, (): void => {
validator: new FakeInputValidator(true),
},
];
const inputValidationService = new InputValidationService(config, combinations);

const actual = inputValidationService.getSearchResult("something");

expect(actual.length).toBe(3);
new InputValidationService(config, combinations).getSearchResult("something").then((result: SearchResultItem[]) => {
expect(result.length).toBe(3);
});
});

it("should return only the items that match the user input", (): void => {
Expand All @@ -117,12 +111,10 @@ describe(InputValidationService.name, (): void => {
},
];

const inputValidationService = new InputValidationService(config, combinations);

const actual = inputValidationService.getSearchResult("something");

expect(actual.length).toBe(1);
expect(actual[0].name).toBe("Search Result 2");
new InputValidationService(config, combinations).getSearchResult("something").then((result) => {
expect(result.length).toBe(1);
expect(result[0].name).toBe("Search Result 2");
});
});

it("should return empty search result if no fallback search results are defined and user input does not match any searcher", (): void => {
Expand All @@ -142,11 +134,9 @@ describe(InputValidationService.name, (): void => {
},
];

const inputValidationService = new InputValidationService(config, combinations);

const actual = inputValidationService.getSearchResult("something");

expect(actual.length).toBe(0);
new InputValidationService(config, combinations).getSearchResult("something").then((result: SearchResultItem[]) => {
expect(result.length).toBe(0);
});
});

it("should return an emtpy array if defined fallback web search does not match any web searches", (): void => {
Expand Down Expand Up @@ -179,11 +169,9 @@ describe(InputValidationService.name, (): void => {
},
];

const inputValidationService = new InputValidationService(config, combinations);

const actual = inputValidationService.getSearchResult(userInput);

expect(actual.length).toBe(0);
new InputValidationService(config, combinations).getSearchResult(userInput).then((result: SearchResultItem[]) => {
expect(result.length).toBe(0);
});
});

it("should return a search result for each fallback search result if user input does not match any searcher", (): void => {
Expand Down Expand Up @@ -214,13 +202,11 @@ describe(InputValidationService.name, (): void => {
},
];

const inputValidationService = new InputValidationService(config, combinations);

const actual = inputValidationService.getSearchResult(userInput);

expect(actual.length).toBe(1);
expect(actual[0].name).toBe(WebSearchBuilder.buildSearchResultItem(userInput, webSearch).name);
expect(actual[0].executionArgument).toBe(`${fallBackWebSearchUrl}${userInput}`);
new InputValidationService(config, combinations).getSearchResult(userInput).then((result: SearchResultItem[]) => {
expect(result.length).toBe(1);
expect(result[0].name).toBe(WebSearchBuilder.buildSearchResultItem(userInput, webSearch).name);
expect(result[0].executionArgument).toBe(`${fallBackWebSearchUrl}${userInput}`);
});
});

it("should only return search results from first blocking searcher", (): void => {
Expand All @@ -241,10 +227,10 @@ describe(InputValidationService.name, (): void => {

const userInput = "search";

const searchResults = new InputValidationService(config, combinations).getSearchResult(userInput);

expect(searchResults.length).toBe(1);
expect(searchResults[0].name).toBe("Search Result 3");
new InputValidationService(config, combinations).getSearchResult(userInput).then((result) => {
expect(result.length).toBe(1);
expect(result[0].name).toBe("Search Result 3");
});
});
});
});
9 changes: 5 additions & 4 deletions src/tests/unit/searcher/calculator-searcher.test.ts
Expand Up @@ -27,10 +27,11 @@ describe(CalculatorSearcher.name, (): void => {
];

for (const combination of combinations) {
const actual = searcher.getSearchResult(combination.input);
expect(actual.filter.length).toBe(1);
expect(actual[0].name).toBe(combination.output.name);
expect(actual[0].executionArgument).toBe(combination.output.executionArgument);
searcher.getSearchResult(combination.input).then((result) => {
expect(result.filter.length).toBe(1);
expect(result[0].name).toBe(combination.output.name);
expect(result[0].executionArgument).toBe(combination.output.executionArgument);
});
}
});

Expand Down
9 changes: 5 additions & 4 deletions src/tests/unit/searcher/command-line-searcher.test.ts
Expand Up @@ -26,10 +26,11 @@ describe(CommandLineSearcher.name, (): void => {
];

for (const combination of combinations) {
const actual = searcher.getSearchResult(combination.input);
expect(actual.filter.length).toBe(1);
expect(actual[0].name).toBe(combination.output.name);
expect(actual[0].executionArgument).toBe(combination.output.executionArgument);
searcher.getSearchResult(combination.input).then((result) => {
expect(result.filter.length).toBe(1);
expect(result[0].name).toBe(combination.output.name);
expect(result[0].executionArgument).toBe(combination.output.executionArgument);
});
}
});

Expand Down
27 changes: 15 additions & 12 deletions src/tests/unit/searcher/custom-command-searcher.test.ts
Expand Up @@ -25,19 +25,21 @@ describe(CustomCommandSearcher.name, (): void => {
const searcher = new CustomCommandSearcher([], defaultShortcutIcon);

for (const notMatchingUserInput of notMatchingUserInputs) {
const actual = searcher.getSearchResult(notMatchingUserInput);
expect(actual).not.toBe(undefined);
expect(actual.length).toBe(0);
searcher.getSearchResult(notMatchingUserInput).then((result) => {
expect(result).not.toBe(undefined);
expect(result.length).toBe(0);
});
}
});

it("should return an empty array if user input does not match any custom command", (): void => {
const searcher = new CustomCommandSearcher(customCommands, defaultShortcutIcon);

for (const notMatchingUserInput of notMatchingUserInputs) {
const actual = searcher.getSearchResult(notMatchingUserInput);
expect(actual).not.toBe(undefined);
expect(actual.length).toBe(0);
searcher.getSearchResult(notMatchingUserInput).then((result) => {
expect(result).not.toBe(undefined);
expect(result.length).toBe(0);
});
}
});

Expand All @@ -55,12 +57,13 @@ describe(CustomCommandSearcher.name, (): void => {
];

for (const combination of combinations) {
const actual = searcher.getSearchResult(combination.input);
expect(actual.length).toBe(1);
expect(actual[0].executionArgument).toBe(combination.output.executionArgument);
expect(actual[0].name).toBe(`Open with command prompt: ipconfig /all`);
expect(actual[0].description).toBe(UeliHelpers.customCommandDescription);
expect(actual[0].searchable.length).toBe(0);
searcher.getSearchResult(combination.input).then((result) => {
expect(result.length).toBe(1);
expect(result[0].executionArgument).toBe(combination.output.executionArgument);
expect(result[0].name).toBe(`Open with command prompt: ipconfig /all`);
expect(result[0].description).toBe(UeliHelpers.customCommandDescription);
expect(result[0].searchable.length).toBe(0);
});
}
});
});
Expand Down
16 changes: 8 additions & 8 deletions src/tests/unit/searcher/email-adress-searcher.test.ts
Expand Up @@ -6,14 +6,14 @@ describe(EmailAddressSearcher.name, (): void => {

describe(searcher.getSearchResult.name, (): void => {
const userInput = "someone@mail.com";
const actual = searcher.getSearchResult(userInput);

expect(actual.length).toBe(1);
expect(actual[0].description).toBe(`Send an email to ${userInput}`);
expect(actual[0].executionArgument).toBe(`mailto:${userInput}`);
expect(actual[0].icon).toBe(testIconSet.emailIcon);
expect(actual[0].name).toBe(userInput);
expect(actual[0].searchable.length).toBe(0);
searcher.getSearchResult(userInput).then((result) => {
expect(result.length).toBe(1);
expect(result[0].description).toBe(`Send an email to ${userInput}`);
expect(result[0].executionArgument).toBe(`mailto:${userInput}`);
expect(result[0].icon).toBe(testIconSet.emailIcon);
expect(result[0].name).toBe(userInput);
expect(result[0].searchable.length).toBe(0);
});
});

it("should not block other searchers", (): void => {
Expand Down
5 changes: 3 additions & 2 deletions src/tests/unit/searcher/search-plugins-searcher.test.ts
Expand Up @@ -35,8 +35,9 @@ describe(SearchPluginsSearcher.name, (): void => {
const searcher = new SearchPluginsSearcher(testConfig, countManager, searchPluginManager);

it("should return the correct search result", (): void => {
const actual = searcher.getSearchResult(searchable);
expect(actual.length).toBe(plugins.length * items.length);
searcher.getSearchResult(searchable).then((result) => {
expect(result.length).toBe(plugins.length * items.length);
});
});
});

Expand Down
17 changes: 9 additions & 8 deletions src/tests/unit/searcher/web-search-searcher.test.ts
Expand Up @@ -28,10 +28,11 @@ describe(WebSearchSearcher.name, (): void => {
];

for (const combination of combinations) {
const actual = searcher.getSearchResult(combination.input);
expect(actual.filter.length).toBe(1);
expect(actual[0].name).toBe(combination.output.name);
expect(actual[0].executionArgument).toBe(combination.output.executionArgument);
searcher.getSearchResult(combination.input).then((result) => {
expect(result.filter.length).toBe(1);
expect(result[0].name).toBe(combination.output.name);
expect(result[0].executionArgument).toBe(combination.output.executionArgument);
});
}
});

Expand Down Expand Up @@ -69,10 +70,10 @@ describe(WebSearchSearcher.name, (): void => {
for (const dummyWebSearch of dummyWebSearches) {
for (const userSearchTerm of userSearchTerms) {
const userInput = `${dummyWebSearch.prefix}${WebSearchHelpers.webSearchSeparator}${userSearchTerm}`;
const actual = searcher.getSearchResult(userInput);

expect(actual.length).toBe(1);
expect(actual[0].executionArgument).toBe(`${dummyWebSearch.url}${trimmedUserInput}`);
searcher.getSearchResult(userInput).then((result) => {
expect(result.length).toBe(1);
expect(result[0].executionArgument).toBe(`${dummyWebSearch.url}${trimmedUserInput}`);
});
}
}
});
Expand Down
9 changes: 5 additions & 4 deletions src/tests/unit/searcher/web-url-searcher.test.ts
Expand Up @@ -26,10 +26,11 @@ describe(WebUrlSearcher.name, (): void => {
];

for (const combination of combinations) {
const actual = searcher.getSearchResult(combination.input);
expect(actual.filter.length).toBe(1);
expect(actual[0].name).toBe(combination.output.name);
expect(actual[0].executionArgument).toBe(combination.output.executionArgument);
searcher.getSearchResult(combination.input).then((result) => {
expect(result.filter.length).toBe(1);
expect(result[0].name).toBe(combination.output.name);
expect(result[0].executionArgument).toBe(combination.output.executionArgument);
});
}
});
});
Expand Down
4 changes: 4 additions & 0 deletions src/ts/helpers/string-helpers.ts
Expand Up @@ -16,6 +16,10 @@ export class StringHelpers {
}

public static trimAndReplaceMultipleWhiteSpacesWithOne(value: string): string {
if (value === undefined || value === null) {
return "";
}

return value.replace(/\s\s+/g, " ").trim();
}

Expand Down

0 comments on commit 3353df8

Please sign in to comment.