Skip to content
This repository has been archived by the owner on Dec 31, 2024. It is now read-only.

Commit

Permalink
Added first tests
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverschwendener committed Mar 7, 2019
1 parent 670ccc8 commit 92093df
Show file tree
Hide file tree
Showing 7 changed files with 258 additions and 5 deletions.
14 changes: 14 additions & 0 deletions .vscode/launch.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@
"outFiles": [
"${workspaceRoot}/bundle/main.js"
]
},
{
"type": "node",
"request": "launch",
"name": "Jest Tests",
"program": "${workspaceRoot}/node_modules/jest/bin/jest",
"args": [
"--runInBand",
"--config",
"jest.json",
"src/tests"
],
"console": "internalConsole",
"internalConsoleOptions": "neverOpen"
}
]
}
2 changes: 1 addition & 1 deletion jest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"^.+\\.tsx?$": "ts-jest"
},
"verbose": true,
"testRegex": "((test|spec))\\.(jsx?|tsx?)$",
"testRegex": "((test|spec))\\.ts$",
"moduleFileExtensions": [
"ts",
"js",
Expand Down
6 changes: 2 additions & 4 deletions src/main/search-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class SearchEngine {
Promise.resolve(this.refreshIndexes());
}

public getSearchResults(userInput: string): Promise<SearchResultItem[]> {
public getSearchResults(userInput: string): Promise<SearchResultItem[]> {
return new Promise((resolve, reject) => {
if (userInput === undefined || userInput.length === 0) {
resolve([]);
Expand Down Expand Up @@ -126,9 +126,7 @@ export class SearchEngine {

Promise.all(pluginPromises)
.then((pluginsResults) => {
let all: SearchResultItem[] = [];

pluginsResults.forEach((r) => all = all.concat(r));
const all = pluginsResults.reduce((a, r) => a = a.concat(r));

const fuse = new Fuse(all, {
distance: 100,
Expand Down
7 changes: 7 additions & 0 deletions src/tests/dummy-icon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Icon } from "../common/icon/icon";
import { IconType } from "../common/icon/icon-type";

export const dummyIcon: Icon = {
parameter: "test",
type: IconType.SVG,
};
52 changes: 52 additions & 0 deletions src/tests/fake-search-plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { SearchPlugin } from "../main/search-plugin";
import { PluginType } from "../main/plugin-type";
import { SearchResultItem } from "../common/search-result-item";
import { UserConfigOptions } from "../common/config/user-config-options";
import { TranslationSet } from "../common/translation/translation-set";

export class FakeSearchPlugin implements SearchPlugin {
public pluginType: PluginType;
public openLocationSupported: boolean;
private readonly items: SearchResultItem[];
private readonly enabled: boolean;

constructor(pluginType: PluginType, items: SearchResultItem[], enabled: boolean) {
this.pluginType = pluginType;
this.items = items;
this.enabled = enabled;
}

public getAll(): Promise<SearchResultItem[]> {
return new Promise((resolve) => {
resolve(this.items);
});
}
public refreshIndex(): Promise<void> {
return new Promise((resolve, reject) => {
reject("Method not implemented.");
});
}
public clearCache(): Promise<void> {
return new Promise((resolve, reject) => {
reject("Method not implemented.");
});
}
public isEnabled(): boolean {
return this.enabled;
}
public execute(searchResultItem: SearchResultItem, privileged: boolean): Promise<void> {
return new Promise((resolve, reject) => {
reject("Method not implemented.");
});
}
public openLocation(searchResultItem: SearchResultItem): Promise<void> {
return new Promise((resolve, reject) => {
reject("Method not implemented.");
});
}
public updateConfig(updatedConfig: UserConfigOptions, translationSet: TranslationSet): Promise<void> {
return new Promise((resolve, reject) => {
reject("Method not implemented.");
});
}
}
179 changes: 179 additions & 0 deletions src/tests/search-engine.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
import { SearchEngine } from "../main/search-engine";
import { SearchPlugin } from "../main/search-plugin";
import { defaultUserConfigOptions } from "../common/config/default-user-config-options";
import { UserConfigOptions } from "../common/config/user-config-options";
import { englishTranslationSet } from "../common/translation/english-translation-set";
import { FakeSearchPlugin } from "./fake-search-plugin";
import { SearchResultItem } from "../common/search-result-item";
import { PluginType } from "../main/plugin-type";
import { dummyIcon } from "./dummy-icon";

describe(SearchEngine.name, () => {
it("should find search results when searching for the exact name", (done) => {
const items: SearchResultItem[] = [
{
description: "Google Chrome",
executionArgument: "C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Google Chrome.lnk",
hideMainWindowAfterExecution: true,
icon: dummyIcon,
name: "Google Chrome",
originPluginType: PluginType.ApplicationSearchPlugin,
searchable: ["Google Chrome"],
},
];

const searchPlugins: SearchPlugin[] = [
new FakeSearchPlugin(PluginType.ApplicationSearchPlugin, items, true),
];
const userConfig = {
//
} as UserConfigOptions;
const config = Object.assign({}, defaultUserConfigOptions, userConfig);

const searchEngine = new SearchEngine(searchPlugins, [], [], config, englishTranslationSet);

searchEngine.getSearchResults("Google Chrome")
.then((searchResults) => {
expect(searchResults.length).toBe(1);
expect(searchResults[0].name).toBe("Google Chrome");
done();
})
.catch((err) => done(err));
});

it("should return no search results found when searching for something non-existent", (done) => {
const items: SearchResultItem[] = [
{
description: "Google Chrome",
executionArgument: "C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Google Chrome.lnk",
hideMainWindowAfterExecution: true,
icon: dummyIcon,
name: "Google Chrome",
originPluginType: PluginType.ApplicationSearchPlugin,
searchable: ["Google Chrome"],
},
];
const translationSet = englishTranslationSet;

const searchPlugins: SearchPlugin[] = [
new FakeSearchPlugin(PluginType.ApplicationSearchPlugin, items, true),
];
const userConfig = {
//
} as UserConfigOptions;
const config = Object.assign({}, defaultUserConfigOptions, userConfig);

const searchEngine = new SearchEngine(searchPlugins, [], [], config, translationSet);

searchEngine.getSearchResults("blabla")
.then((searchResults) => {
expect(searchResults.length).toBe(1);
expect(searchResults[0].name).toBe(translationSet.noSearchResultsFoundTitle);
done();
})
.catch((err) => done(err));
});

it("it should be case insensitive", (done) => {
const items: SearchResultItem[] = [
{
description: "Google Chrome",
executionArgument: "C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Google Chrome.lnk",
hideMainWindowAfterExecution: true,
icon: dummyIcon,
name: "Google Chrome",
originPluginType: PluginType.ApplicationSearchPlugin,
searchable: ["Google Chrome"],
},
];
const translationSet = englishTranslationSet;

const searchPlugins: SearchPlugin[] = [
new FakeSearchPlugin(PluginType.ApplicationSearchPlugin, items, true),
];
const userConfig = {
//
} as UserConfigOptions;
const config = Object.assign({}, defaultUserConfigOptions, userConfig);

const searchEngine = new SearchEngine(searchPlugins, [], [], config, translationSet);

searchEngine.getSearchResults("gOoGlE ChRoMe")
.then((searchResults) => {
expect(searchResults.length).toBe(1);
expect(searchResults[0].name).toBe("Google Chrome");
done();
})
.catch((err) => done(err));
});

it("it should allow inprecise search terms when fuzzyness threshold is higher", (done) => {
const items: SearchResultItem[] = [
{
description: "Google Chrome",
executionArgument: "C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Google Chrome.lnk",
hideMainWindowAfterExecution: true,
icon: dummyIcon,
name: "Google Chrome",
originPluginType: PluginType.ApplicationSearchPlugin,
searchable: ["Google Chrome"],
},
];
const translationSet = englishTranslationSet;

const searchPlugins: SearchPlugin[] = [
new FakeSearchPlugin(PluginType.ApplicationSearchPlugin, items, true),
];
const userConfig = {
searchEngineOptions: {
fuzzyness: 0.8,
}
} as UserConfigOptions;
const config = Object.assign({}, defaultUserConfigOptions, userConfig);

const searchEngine = new SearchEngine(searchPlugins, [], [], config, translationSet);

searchEngine.getSearchResults("gglchrm")
.then((searchResults) => {
expect(searchResults.length).toBe(1);
expect(searchResults[0].name).toBe("Google Chrome");
done();
})
.catch((err) => done(err));
});

it("it should require more precise search terms when fuzzyness threshold is lower", (done) => {
const items: SearchResultItem[] = [
{
description: "Google Chrome",
executionArgument: "C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Google Chrome.lnk",
hideMainWindowAfterExecution: true,
icon: dummyIcon,
name: "Google Chrome",
originPluginType: PluginType.ApplicationSearchPlugin,
searchable: ["Google Chrome"],
},
];
const translationSet = englishTranslationSet;

const searchPlugins: SearchPlugin[] = [
new FakeSearchPlugin(PluginType.ApplicationSearchPlugin, items, true),
];
const userConfig = {
searchEngineOptions: {
fuzzyness: 0.2,
},
} as UserConfigOptions;
const config = Object.assign({}, defaultUserConfigOptions, userConfig);

const searchEngine = new SearchEngine(searchPlugins, [], [], config, translationSet);

searchEngine.getSearchResults("gglchrm")
.then((searchResults) => {
expect(searchResults.length).toBe(1);
expect(searchResults[0].name).toBe(translationSet.noSearchResultsFoundTitle);
done();
})
.catch((err) => done(err));
});
});
3 changes: 3 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,8 @@
},
"include": [
"./src/**/*.ts",
],
"exclude": [
"./src/**/*.test.ts",
]
}

0 comments on commit 92093df

Please sign in to comment.