Skip to content

Commit

Permalink
Added tests for input validation service
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverschwendener committed Jun 3, 2018
1 parent 2076985 commit e0a774e
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 5 deletions.
13 changes: 13 additions & 0 deletions src/tests/unit/fake-input-validator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { InputValidator } from "../../ts/input-validators/input-validator";

export class FakeInputValidator implements InputValidator {
private returnValue: boolean;

constructor(returnValue: boolean) {
this.returnValue = returnValue;
}

public isValidForSearchResults(userInput: string): boolean {
return this.returnValue;
}
}
14 changes: 14 additions & 0 deletions src/tests/unit/fake-searcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Searcher } from "../../ts/searcher/searcher";
import { SearchResultItem } from "../../ts/search-result-item";

export class FakeSearcher implements Searcher {
private searchResultItems: SearchResultItem[];

constructor(searchResultItems: SearchResultItem[]) {
this.searchResultItems = searchResultItems;
}

public getSearchResult(userInput: string): SearchResultItem[] {
return this.searchResultItems;
}
}
115 changes: 115 additions & 0 deletions src/tests/unit/input-validation-service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { InputValidationService } from "../../ts/input-validation-service";
import { InputValidatorSearcherCombination } from "../../ts/input-validator-searcher-combination";
import { FakeSearcher } from "./fake-searcher";
import { FakeInputValidator } from "./fake-input-validator";
import { SearchResultItem } from "../../ts/search-result-item";

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 = [
"",
" ",
undefined,
null,
];

const combinations = [
{
searcher: new FakeSearcher([{ name: "Search Result 1" }] as SearchResultItem[]),
validator: new FakeInputValidator(true),
},
{
searcher: new FakeSearcher([{ name: "Search Result 2" }] as SearchResultItem[]),
validator: new FakeInputValidator(false),
},
{
searcher: new FakeSearcher([{ name: "Search Result 3" }] as SearchResultItem[]),
validator: new FakeInputValidator(false),
},
];

const inputValidationService = new InputValidationService(combinations);

for (const userInput of userInputs) {
const actual = inputValidationService.getSearchResult(userInput as string);
expect(actual.length).toBe(0);
}
});

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

it("should return an empty array if user input matches none of the searchers", (): void => {
const combinations = [
{
searcher: new FakeSearcher([{ name: "Search Result 1" }] as SearchResultItem[]),
validator: new FakeInputValidator(false),
},
{
searcher: new FakeSearcher([{ name: "Search Result 2" }] as SearchResultItem[]),
validator: new FakeInputValidator(false),
},
{
searcher: new FakeSearcher([{ name: "Search Result 3" }] as SearchResultItem[]),
validator: new FakeInputValidator(false),
},
];

const inputValidationService = new InputValidationService(combinations);

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

expect(actual.length).toBe(0);
});

it("should return all items if user input matches all searchers", (): void => {
const combinations = [
{
searcher: new FakeSearcher([{ name: "Search Result 1" }] as SearchResultItem[]),
validator: new FakeInputValidator(true),
},
{
searcher: new FakeSearcher([{ name: "Search Result 2" }] as SearchResultItem[]),
validator: new FakeInputValidator(true),
},
{
searcher: new FakeSearcher([{ name: "Search Result 3" }] as SearchResultItem[]),
validator: new FakeInputValidator(true),
},
];
const inputValidationService = new InputValidationService(combinations);

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

expect(actual.length).toBe(3);
});

it("should return only the items that match the user input", (): void => {
const combinations = [
{
searcher: new FakeSearcher([{ name: "Search Result 1" }] as SearchResultItem[]),
validator: new FakeInputValidator(false),
},
{
searcher: new FakeSearcher([{ name: "Search Result 2" }] as SearchResultItem[]),
validator: new FakeInputValidator(true),
},
{
searcher: new FakeSearcher([{ name: "Search Result 3" }] as SearchResultItem[]),
validator: new FakeInputValidator(false),
},
];

const inputValidationService = new InputValidationService(combinations);

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

expect(actual.length).toBe(1);
expect(actual[0].name).toBe("Search Result 2");
});
});
});
2 changes: 1 addition & 1 deletion src/ts/helpers/string-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export class StringHelpers {
}

public static stringIsWhiteSpace(value: string): boolean {
if (value === undefined) {
if (value === undefined || value === null) {
return true;
}

Expand Down
9 changes: 5 additions & 4 deletions src/ts/input-validation-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@ export class InputValidationService {

public getSearchResult(userInput: string): SearchResultItem[] {
let result = [] as SearchResultItem[];
userInput = StringHelpers.trimAndReplaceMultipleWhiteSpacesWithOne(userInput);

if (StringHelpers.stringIsWhiteSpace(userInput)) {
if (userInput === undefined || userInput === null || StringHelpers.stringIsWhiteSpace(userInput)) {
return result;
}

const trimmedUserInput = StringHelpers.trimAndReplaceMultipleWhiteSpacesWithOne(userInput);

for (const combination of this.validatorSearcherCombinations) {
if (combination.validator.isValidForSearchResults(userInput)) {
result = result.concat(combination.searcher.getSearchResult(userInput));
if (combination.validator.isValidForSearchResults(trimmedUserInput)) {
result = result.concat(combination.searcher.getSearchResult(trimmedUserInput));
}
}

Expand Down

0 comments on commit e0a774e

Please sign in to comment.