From 389a4cb1c4c855acf7eef62b44a300e05bc5af49 Mon Sep 17 00:00:00 2001 From: alecmerdler Date: Fri, 17 Feb 2017 13:40:05 -0800 Subject: [PATCH] fixed tests --- .../regex-match-view.component.spec.ts | 30 ++++++++++++++++++ .../regex-match-view.component.ts | 6 ++-- test/data/test.db | Bin 1306624 -> 1306624 bytes 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/static/js/directives/ui/regex-match-view/regex-match-view.component.spec.ts b/static/js/directives/ui/regex-match-view/regex-match-view.component.spec.ts index 86e18b99c..a4a5c0aaf 100644 --- a/static/js/directives/ui/regex-match-view/regex-match-view.component.spec.ts +++ b/static/js/directives/ui/regex-match-view/regex-match-view.component.spec.ts @@ -9,6 +9,36 @@ describe("RegexMatchViewComponent", () => { }); describe("filterMatches", () => { + var items: ({value: string})[]; + beforeEach(() => { + items = [{value: "master"}, {value: "develop"}, {value: "production"}]; + }); + + it("returns null if given invalid regex expression", () => { + var regexstr: string = "\\asfd\\"; + + expect(component.filterMatches(regexstr, items, true)).toBe(null); + }); + + it("returns a subset of given items matching the given regex expression if given 'shouldMatch' as true", () => { + var regexstr: string = `^${items[0].value}$`; + var matches: ({value: string})[] = component.filterMatches(regexstr, items, true); + + expect(matches.length).toBeGreaterThan(0); + matches.forEach((match) => { + expect(items).toContain(match); + }); + }); + + it("returns a subset of given items not matching the given regex expression if given 'shouldMatch' as false", () => { + var regexstr: string = `^${items[0].value}$`; + var nonMatches: ({value: string})[] = component.filterMatches(regexstr, items, false); + + expect(nonMatches.length).toBeGreaterThan(0); + nonMatches.forEach((nonMatch) => { + expect(items).toContain(nonMatch); + }); + }); }); }); diff --git a/static/js/directives/ui/regex-match-view/regex-match-view.component.ts b/static/js/directives/ui/regex-match-view/regex-match-view.component.ts index 2c987906c..946771a95 100644 --- a/static/js/directives/ui/regex-match-view/regex-match-view.component.ts +++ b/static/js/directives/ui/regex-match-view/regex-match-view.component.ts @@ -48,7 +48,7 @@ export class RegexMatchViewComponent implements ng.IComponentController { } - public filterMatches(regexstr: string, items: any[], shouldMatch: boolean): any[] { + public filterMatches(regexstr: string, items: ({value: string})[], shouldMatch: boolean): ({value: string})[] | null { regexstr = regexstr || '.+'; try { @@ -58,8 +58,8 @@ export class RegexMatchViewComponent implements ng.IComponentController { } return items.filter(function(item) { - var value = item['value']; - var m = value.match(regex); + var value: string = item.value; + var m: RegExpMatchArray = value.match(regex); var matches: boolean = !!(m && m[0].length == value.length); return matches == shouldMatch; }); diff --git a/test/data/test.db b/test/data/test.db index 5d0c6c4c78967925ead613b565730fe9727aff1e..9c7a22b34ab4c1da80f7bd8882a9f159c5241908 100644 GIT binary patch delta 320 zcmZ9_u};H43y7*QtDB2sb`MPh3y#>S5G$luGr-@94Rp_zYruRc%tc9sd8zvZX@|If|7ovnfGAOCWe1RjCy rEFYME@iQ@SZo9z#pTGToKMxS|0x=&DgZKhKEC|Fx+yD0qGbR84FJCz3