-
-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added tests for input validation service
- Loading branch information
1 parent
2076985
commit e0a774e
Showing
5 changed files
with
148 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters