Skip to content

Commit

Permalink
Rewrite SearchPlugin 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 3353df8 commit 5dedc0e
Show file tree
Hide file tree
Showing 21 changed files with 193 additions and 163 deletions.
60 changes: 31 additions & 29 deletions src/tests/integration/search-plugins/file-search-plugin.test.ts
Expand Up @@ -75,11 +75,11 @@ describe(FileSearchPlugin.name, (): void => {
};
});
const plugin = new FileSearchPlugin(options, testIconSet, emptyBlackList);
const actual = plugin.getAllItems();
const actualLength = actual.length;
const expectedLength = (testFiles.length + subFolders.length) * parentFolders.length;

expect(actualLength).toBe(expectedLength);
plugin.getAllItems().then((result) => {
const actualLength = result.length;
const expectedLength = (testFiles.length + subFolders.length) * parentFolders.length;
expect(actualLength).toBe(expectedLength);
});
});

it("should return all files and folders (recursively) if recursive search is set to true", (): void => {
Expand All @@ -91,13 +91,15 @@ describe(FileSearchPlugin.name, (): void => {
};
});
const plugin = new FileSearchPlugin(options, testIconSet, emptyBlackList);
const actual = plugin.getAllItems();
const actualLength = actual.length;
const expectedLength = (parentFolders.length * subFolders.length * testFiles.length)
+ (parentFolders.length * testFiles.length)
+ (parentFolders.length * subFolders.length);
plugin.getAllItems().then((result) => {
const actualLength = result.length;
const expectedLength = (parentFolders.length * subFolders.length * testFiles.length)
+ (parentFolders.length * testFiles.length)
+ (parentFolders.length * subFolders.length);

expect(actualLength).toBe(expectedLength);
});

expect(actualLength).toBe(expectedLength);
});

it("all returned items should have name, execution argument and tags set", (): void => {
Expand All @@ -108,12 +110,12 @@ describe(FileSearchPlugin.name, (): void => {
};
});
const plugin = new FileSearchPlugin(options, testIconSet, emptyBlackList);
const actual = plugin.getAllItems();

for (const item of actual) {
expect(item.name.length).toBeGreaterThan(0);
expect(item.executionArgument.length).toBeGreaterThan(0);
}
plugin.getAllItems().then((result) => {
for (const item of result) {
expect(item.name.length).toBeGreaterThan(0);
expect(item.executionArgument.length).toBeGreaterThan(0);
}
});
});

it("should set the right icon", (): void => {
Expand All @@ -124,25 +126,25 @@ describe(FileSearchPlugin.name, (): void => {
};
});
const plugin = new FileSearchPlugin(options, testIconSet, emptyBlackList);
const actual = plugin.getAllItems();
plugin.getAllItems().then((result) => {
const actualFiles = result.filter((a) => {
return a.icon === testIconSet.fileIcon;
});

const actualFiles = actual.filter((a) => {
return a.icon === testIconSet.fileIcon;
});
const actualFolders = result.filter((a) => {
return a.icon === testIconSet.folderIcon;
});

const actualFolders = actual.filter((a) => {
return a.icon === testIconSet.folderIcon;
expect(actualFiles.length).toBe(testFiles.length * parentFolders.length);
expect(actualFolders.length).toBe(subFolders.length * parentFolders.length);
});

expect(actualFiles.length).toBe(testFiles.length * parentFolders.length);
expect(actualFolders.length).toBe(subFolders.length * parentFolders.length);
});

it("should return an empty array if no folders are specified", (): void => {
const plugin = new FileSearchPlugin([], testIconSet, emptyBlackList);
const acutal = plugin.getAllItems();

expect(acutal.length).toBe(0);
plugin.getAllItems().then((result) => {
expect(result.length).toBe(0);
});
});
});

Expand Down
6 changes: 4 additions & 2 deletions src/tests/unit/fake-search-plugin.ts
Expand Up @@ -12,7 +12,9 @@ export class FakeSearchPlugin implements SearchPlugin {
return this.searchResultItems.length;
}

public getAllItems(): SearchResultItem[] {
return this.searchResultItems;
public getAllItems(): Promise<SearchResultItem[]> {
return new Promise((resolve, reject) => {
resolve(this.searchResultItems);
});
}
}
Expand Up @@ -23,6 +23,9 @@ describe(CalculatorInputValidator.name, (): void => {

it("should return false when passing in an invalid argument", (): void => {
const invalidInputs = [
undefined,
null,
"",
"s",
"kg",
"log()",
Expand Down
Expand Up @@ -12,15 +12,15 @@ describe(EnvironmentVariableSearchPlugin.name, (): void => {

describe(plugin.getAllItems.name, (): void => {
it("should return an search result item for each item in the environment variable collection", (): void => {
const actual = plugin.getAllItems();
plugin.getAllItems().then((result) => {
expect(result.length).toBe(Object.keys(fakeEnvironmentVariableCollection).length);

expect(actual.length).toBe(Object.keys(fakeEnvironmentVariableCollection).length);

for (const key of Object.keys(fakeEnvironmentVariableCollection)) {
const item = actual.filter((a) => a.name === key)[0];
expect(item).not.toBe(undefined);
expect(item).not.toBe(null);
}
for (const key of Object.keys(fakeEnvironmentVariableCollection)) {
const item = result.filter((a) => a.name === key)[0];
expect(item).not.toBe(undefined);
expect(item).not.toBe(null);
}
});
});
});

Expand Down
5 changes: 3 additions & 2 deletions src/tests/unit/search-plugins/fake-search-plugin.test.ts
Expand Up @@ -7,8 +7,9 @@ describe(FakeSearchPlugin.name, (): void => {

describe(plugin.getAllItems.name, (): void => {
it("should return all items", (): void => {
const actual = plugin.getAllItems();
expect(actual.length).toBe(items.length);
plugin.getAllItems().then((result) => {
expect(result.length).toBe(items.length);
});
});
});

Expand Down
9 changes: 5 additions & 4 deletions src/tests/unit/search-plugins/mac-os-settings-plugin.test.ts
Expand Up @@ -14,10 +14,11 @@ describe(MacOsSettingsPlugin.name, (): void => {

describe(searchPlugin.getAllItems.name, (): void => {
it("should return more than zero items", () => {
const actual = searchPlugin.getAllItems();
expect(actual).not.toBe(undefined);
expect(actual).not.toBe(null);
expect(actual.length).toBeGreaterThan(0);
searchPlugin.getAllItems().then((actual) => {
expect(actual).not.toBe(undefined);
expect(actual).not.toBe(null);
expect(actual.length).toBeGreaterThan(0);
});
});
});

Expand Down
26 changes: 13 additions & 13 deletions src/tests/unit/search-plugins/programs-plugin.test.ts
Expand Up @@ -26,19 +26,19 @@ describe("ProgramsPlugin", (): void => {

describe(programsPlugin.getAllItems.name, (): void => {
it("should return all programs", (): void => {
const actual = programsPlugin.getAllItems();

expect(actual.length).toBeGreaterThan(0);

for (const fakeProgram of fakePrograms) {
const filtered = actual.filter((a): boolean => {
return a.name === fakeProgram.name;
});

expect(filtered.length).toBe(1);
expect(filtered[0].name).toBe(fakeProgram.name);
expect(filtered[0].executionArgument).toBe(fakeProgram.executionArgument);
}
programsPlugin.getAllItems().then((result) => {
expect(result.length).toBeGreaterThan(0);

for (const fakeProgram of fakePrograms) {
const filtered = result.filter((a): boolean => {
return a.name === fakeProgram.name;
});

expect(filtered.length).toBe(1);
expect(filtered[0].name).toBe(fakeProgram.name);
expect(filtered[0].executionArgument).toBe(fakeProgram.executionArgument);
}
});
});
});

Expand Down
56 changes: 28 additions & 28 deletions src/tests/unit/search-plugins/shortcut-plugin.test.ts
Expand Up @@ -19,16 +19,16 @@ describe(ShortcutsPlugin.name, (): void => {

const plugin = new ShortcutsPlugin(shortcuts, "");

const actual = plugin.getAllItems();

for (const item of actual) {
const shortcut = shortcuts.filter((c: Shortcut): boolean => {
return c.name === item.name;
})[0];

expect(shortcut).not.toBe(undefined);
expect(item.executionArgument).toBe(shortcut.executionArgument);
}
plugin.getAllItems().then((actual) => {
for (const item of actual) {
const shortcut = shortcuts.filter((c: Shortcut): boolean => {
return c.name === item.name;
})[0];

expect(shortcut).not.toBe(undefined);
expect(item.executionArgument).toBe(shortcut.executionArgument);
}
});
});

it("should set the deafult icon if no icon is specified", (): void => {
Expand All @@ -47,16 +47,16 @@ describe(ShortcutsPlugin.name, (): void => {

const plugin = new ShortcutsPlugin(shortcuts, defaultIcon);

const actual = plugin.getAllItems();
plugin.getAllItems().then((actual) => {
for (const item of actual) {
const shortcut = shortcuts.filter((c: Shortcut): boolean => {
return c.name === item.name;
})[0];

for (const item of actual) {
const shortcut = shortcuts.filter((c: Shortcut): boolean => {
return c.name === item.name;
})[0];

expect(shortcut).not.toBe(undefined);
expect(item.icon).toBe(defaultIcon);
}
expect(shortcut).not.toBe(undefined);
expect(item.icon).toBe(defaultIcon);
}
});
});

it("should set the given icon if it is specified", (): void => {
Expand All @@ -77,16 +77,16 @@ describe(ShortcutsPlugin.name, (): void => {

const plugin = new ShortcutsPlugin(shortcuts, defaultIcon);

const actual = plugin.getAllItems();

for (const item of actual) {
const shortcut = shortcuts.filter((c: Shortcut): boolean => {
return c.name === item.name;
})[0];
plugin.getAllItems().then((actual) => {
for (const item of actual) {
const shortcut = shortcuts.filter((c: Shortcut): boolean => {
return c.name === item.name;
})[0];

expect(shortcut).not.toBe(undefined);
expect(item.icon).toBe(shortcut.icon);
}
expect(shortcut).not.toBe(undefined);
expect(item.icon).toBe(shortcut.icon);
}
});
});
});

Expand Down
5 changes: 3 additions & 2 deletions src/tests/unit/search-plugins/ueli-commands-plugin.test.ts
Expand Up @@ -5,8 +5,9 @@ describe(UeliCommandsSearchPlugin.name, (): void => {

describe(plugin.getAllItems.name, (): void => {
it("should return all items", (): void => {
const actual = plugin.getAllItems();
expect(actual.length).toBe(3);
plugin.getAllItems().then((actual) => {
expect(actual.length).toBe(3);
});
});
});

Expand Down
Expand Up @@ -22,8 +22,9 @@ describe(Windows10SettingsSearchPlugin.name, (): void => {

describe(searchPlugin.getAllItems.name, (): void => {
it("should return more than zero items", () => {
const actual = searchPlugin.getAllItems();
expect(actual.length).toBe(testSettings.length + testApps.length);
searchPlugin.getAllItems().then((actual) => {
expect(actual.length).toBe(testSettings.length + testApps.length);
});
});
});

Expand Down
14 changes: 7 additions & 7 deletions src/ts/input-validators/calculator-input-validator.ts
@@ -1,9 +1,13 @@
import { InputValidator } from "./input-validator";
import * as math from "mathjs";
import { StringHelpers } from "../helpers/string-helpers";
import * as math from "mathjs";

export class CalculatorInputValidator implements InputValidator {
public isValidForSearchResults(userInput: string): boolean {
public isValidForSearchResults(userInput: string | undefined | null): boolean {
if (userInput === undefined || userInput === null) {
return false;
}

if (!this.isValidInput(userInput)) {
return false;
}
Expand All @@ -18,7 +22,7 @@ export class CalculatorInputValidator implements InputValidator {
return !isNaN(result) || this.isValidMathType(result) || false;
}

private isValidMathType(input: any): boolean {
private isValidMathType(input: any | undefined): boolean {
const mathType = math.typeof(input);

if ((mathType === "Unit" && input.value === null)
Expand All @@ -34,10 +38,6 @@ export class CalculatorInputValidator implements InputValidator {
return false;
}

if (userInput === undefined || userInput === null) {
return false;
}

if (StringHelpers.trimAndReplaceMultipleWhiteSpacesWithOne(userInput).length === 0) {
return false;
}
Expand Down
26 changes: 14 additions & 12 deletions src/ts/search-plugins/environment-variable-search-plugin.ts
Expand Up @@ -15,19 +15,21 @@ export class EnvironmentVariableSearchPlugin implements SearchPlugin {
return Object.keys(this.variableCollection).length;
}

public getAllItems(): SearchResultItem[] {
const result: SearchResultItem[] = [];
public getAllItems(): Promise<SearchResultItem[]> {
return new Promise((resolve, reject) => {
const result: SearchResultItem[] = [];

for (const variableName of Object.keys(this.variableCollection)) {
result.push({
description: this.variableCollection[variableName],
executionArgument: this.variableCollection[variableName],
icon: this.iconSet.environmentVariableIcon,
name: variableName,
searchable: [variableName],
});
}
for (const variableName of Object.keys(this.variableCollection)) {
result.push({
description: this.variableCollection[variableName],
executionArgument: this.variableCollection[variableName],
icon: this.iconSet.environmentVariableIcon,
name: variableName,
searchable: [variableName],
});
}

return result;
resolve(result);
});
}
}
6 changes: 4 additions & 2 deletions src/ts/search-plugins/file-search-plugin.ts
Expand Up @@ -24,8 +24,10 @@ export class FileSearchPlugin implements SearchPlugin {
return this.items.length;
}

public getAllItems(): SearchResultItem[] {
return this.items;
public getAllItems(): Promise<SearchResultItem[]> {
return new Promise((resolve, reject) => {
resolve(this.items);
});
}

private loadFilesAndFolders(): SearchResultItem[] {
Expand Down

0 comments on commit 5dedc0e

Please sign in to comment.