Skip to content

Commit 9d01cdb

Browse files
committed
fix(search-client): Added categorizationType to SearchClient class. Added and fixed unit-tests.
1 parent b85e3ad commit 9d01cdb

File tree

4 files changed

+149
-31
lines changed

4 files changed

+149
-31
lines changed

src/Categorize/CategorizeQueryConverter.spec.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { ICategory } from "../Data";
2-
import { Filter, OrderBy, Query, SearchType } from "../Common";
2+
import {
3+
Filter,
4+
OrderBy,
5+
Query,
6+
SearchType,
7+
CategorizationType
8+
} from "../Common";
39
import { CategorizeQueryConverter } from ".";
410

511
const fixedQuery = new Query({
@@ -28,7 +34,11 @@ const fixedQuery = new Query({
2834
matchPageSize: 20,
2935
maxSuggestions: 20,
3036
queryText: "test",
31-
searchType: SearchType.Keywords
37+
searchType: SearchType.Keywords,
38+
categorizationType: CategorizationType.All,
39+
matchGenerateContent: true,
40+
matchGenerateContentHighlights: false,
41+
uiLanguageCode: "en"
3242
});
3343

3444
describe("QueryConverters", () => {
@@ -55,7 +65,7 @@ describe("QueryConverters", () => {
5565
it("Should match expectations for REST V4 with given query", () => {
5666
let qc = new CategorizeQueryConverter();
5767
const expectedFindUrl =
58-
"http://localhost:9950/RestService/v4/search/categorize?c=mobile&ct=All&df=2017-03-13T09%3A00%3A00.000Z&dt=2017-03-13T09%3A00%3A00.000Z&f=Authors%7CBob%3BFileTypes%7Cdocx&q=test&t=Keywords";
68+
"http://localhost:9950/RestService/v4/search/categorize?c=mobile&ct=All&df=2017-03-13T09%3A00%3A00.000Z&dt=2017-03-13T09%3A00%3A00.000Z&f=Authors%7CBob%3BFileTypes%7Cdocx&l=en&q=test&t=Keywords";
5969

6070
expect(
6171
qc.getUrl(

src/Find/Find.spec.ts

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fetch from "jest-fetch-mock";
22

33
import { Find, IFindSettings, FindTriggers } from ".";
4-
import { IMatches } from "../Data";
4+
import { IMatches, IMatchItem } from "../Data";
55
import { Query } from "../Common";
66

77
describe("Find basics", () => {
@@ -96,30 +96,31 @@ describe("Find basics", () => {
9696
it("Should be able to Find some results", () => {
9797
fetch.resetMocks();
9898
// Not caring about the response, just to allow the fetch to complete.
99-
fetch.mockResponse(JSON.stringify(null));
99+
fetch.mockResponse(
100+
JSON.stringify({
101+
bestBets: [],
102+
didYouMeanList: [],
103+
errorMessage: null,
104+
estimatedMatchCount: 1,
105+
expandedQuery: "",
106+
nextPageRef: 0,
107+
searchMatches: [{} as IMatchItem]
108+
} as IMatches)
109+
);
100110
let settings = {
101111
baseUrl: "http://localhost:9950/",
102-
cbRequest: jest.fn((url, reqInit) => {
103-
expect(typeof url).toBe("string");
104-
expect(typeof reqInit).toBe("object");
105-
}),
106-
cbSuccess: jest.fn((url, reqInit) => {
107-
expect(typeof url).toBe("string");
108-
expect(typeof reqInit).toBe("object");
109-
})
112+
cbError: jest.fn(),
113+
cbRequest: jest.fn(),
114+
cbSuccess: jest.fn()
110115
} as IFindSettings;
111116

112117
let find = new Find(settings, null, fetch);
113118
find.fetch()
114-
.then(response => {
115-
expect(typeof response).toBe("object");
116-
})
117119
.catch(error => {
118120
fail("Should not fail");
119121
})
120122
.then(() => {
121-
expect(settings.cbRequest).toHaveBeenCalled();
122-
expect(settings.cbSuccess).toHaveBeenCalled();
123+
expect(settings.cbSuccess).toHaveBeenCalledTimes(1);
123124
});
124125
});
125126

@@ -147,7 +148,7 @@ describe("Find basics", () => {
147148
fail("Should not yield an error");
148149
})
149150
.then(() => {
150-
expect(settings.cbRequest).toHaveBeenCalled();
151+
expect(settings.cbRequest).toHaveBeenCalledTimes(1);
151152
expect(settings.cbSuccess).not.toHaveBeenCalled();
152153
});
153154
});
@@ -166,10 +167,10 @@ describe("Find basics", () => {
166167
find.update({ queryText: "search-1" });
167168
expect(settings.cbResultState).not.toBeCalled();
168169
find.shouldUpdate("queryText", { queryText: "search-1" });
169-
expect(settings.cbResultState).toBeCalledTimes(1);
170+
expect(settings.cbResultState).toHaveBeenCalledTimes(1);
170171
find.shouldUpdate("queryText", { queryText: "search-2" });
171-
expect(settings.cbResultState).toBeCalledTimes(2);
172+
expect(settings.cbResultState).toHaveBeenCalledTimes(2);
172173
find.shouldUpdate("queryText", { queryText: "search-2" });
173-
expect(settings.cbResultState).toBeCalledTimes(3);
174+
expect(settings.cbResultState).toHaveBeenCalledTimes(3);
174175
});
175176
});

src/SearchClient.spec.ts

Lines changed: 90 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
} from "./SearchClient";
99

1010
import reference from "./test-data/categories.json";
11+
import { CategorizationType } from "./Common";
1112

1213
describe("SearchClient basics", () => {
1314
it("Should have imported SearchClient class defined", () => {
@@ -530,7 +531,7 @@ describe("SearchClient filter interface", () => {
530531

531532
client.deferUpdates(false, false);
532533

533-
expect(findFetch).toBeCalledTimes(1);
534+
expect(findFetch).toHaveBeenCalledTimes(1);
534535
expect(pClient.settings.query.matchPage).toEqual(2);
535536

536537
findFetch.mockReset();
@@ -620,15 +621,29 @@ describe("SearchClient filter interface", () => {
620621
expect(client.matchGrouping).toBeTruthy();
621622
expect(client.query.matchGrouping).toBeTruthy();
622623
expect((client as any)._query.matchGrouping).toBeTruthy();
624+
});
623625

624-
mockFindRequest.mockReset();
625-
mockCatRequest.mockReset();
626-
client.update(); // Updating according to triggers and settings
627-
expect(mockFindRequest).toHaveBeenCalledTimes(1);
628-
expect(mockCatRequest).toHaveBeenCalledTimes(1);
626+
it("Should update as expected when client query properties changes", () => {
627+
let mockFindRequest = jest.fn();
628+
let mockFindSuccess = jest.fn();
629+
let mockCatRequest = jest.fn();
630+
let mockCatSuccess = jest.fn();
631+
632+
let client = new SearchClient({
633+
baseUrl: "http://localhost:9950/",
634+
find: {
635+
cbRequest: mockFindRequest,
636+
cbSuccess: mockFindSuccess
637+
},
638+
categorize: {
639+
cbRequest: mockCatRequest,
640+
cbSuccess: mockCatSuccess
641+
},
642+
query: {
643+
matchGrouping: true
644+
}
645+
});
629646

630-
mockFindRequest.mockReset();
631-
mockCatRequest.mockReset();
632647
let q = client.query;
633648
q.queryText = "test2\n"; // Modifying the reference, so should already do the updates.
634649
client.update(q); // So the update should not do work.
@@ -677,5 +692,72 @@ describe("SearchClient filter interface", () => {
677692
client.forceUpdate(null, false, false, false); // Forcing an update, but stopping services
678693
expect(mockFindRequest).toHaveBeenCalledTimes(0);
679694
expect(mockCatRequest).toHaveBeenCalledTimes(0);
695+
696+
mockFindRequest.mockReset();
697+
mockCatRequest.mockReset();
698+
client.uiLanguageCode = "no";
699+
expect(mockFindRequest).toHaveBeenCalledTimes(0);
700+
expect(mockCatRequest).toHaveBeenCalledTimes(1);
701+
702+
mockFindRequest.mockReset();
703+
mockCatRequest.mockReset();
704+
client.categorizationType = CategorizationType.DocumentHitsOnly;
705+
expect(mockFindRequest).toHaveBeenCalledTimes(0);
706+
expect(mockCatRequest).toHaveBeenCalledTimes(1);
707+
708+
mockFindRequest.mockReset();
709+
mockCatRequest.mockReset();
710+
client.matchGenerateContent = true;
711+
expect(mockFindRequest).toHaveBeenCalledTimes(1);
712+
expect(mockCatRequest).toHaveBeenCalledTimes(0);
713+
714+
// No changes - same value applied again.
715+
mockFindRequest.mockReset();
716+
mockCatRequest.mockReset();
717+
client.matchGenerateContent = true;
718+
expect(mockFindRequest).toHaveBeenCalledTimes(0);
719+
expect(mockCatRequest).toHaveBeenCalledTimes(0);
720+
721+
mockFindRequest.mockReset();
722+
mockCatRequest.mockReset();
723+
client.matchGenerateContentHighlights = false;
724+
expect(mockFindRequest).toHaveBeenCalledTimes(1);
725+
expect(mockCatRequest).toHaveBeenCalledTimes(0);
726+
727+
mockFindRequest.mockReset();
728+
mockCatRequest.mockReset();
729+
client.matchOrderBy = OrderBy.Date;
730+
expect(mockFindRequest).toHaveBeenCalledTimes(1);
731+
expect(mockCatRequest).toHaveBeenCalledTimes(0);
732+
733+
mockFindRequest.mockReset();
734+
mockCatRequest.mockReset();
735+
client.clientId = "new";
736+
expect(mockFindRequest).toHaveBeenCalledTimes(1);
737+
expect(mockCatRequest).toHaveBeenCalledTimes(1);
738+
739+
mockFindRequest.mockReset();
740+
mockCatRequest.mockReset();
741+
client.dateFrom = { M: -2 };
742+
expect(mockFindRequest).toHaveBeenCalledTimes(1);
743+
expect(mockCatRequest).toHaveBeenCalledTimes(1);
744+
745+
mockFindRequest.mockReset();
746+
mockCatRequest.mockReset();
747+
client.dateTo = { M: -1 };
748+
expect(mockFindRequest).toHaveBeenCalledTimes(1);
749+
expect(mockCatRequest).toHaveBeenCalledTimes(1);
750+
751+
mockFindRequest.mockReset();
752+
mockCatRequest.mockReset();
753+
client.matchPageSize = 25;
754+
expect(mockFindRequest).toHaveBeenCalledTimes(1);
755+
expect(mockCatRequest).toHaveBeenCalledTimes(0);
756+
757+
mockFindRequest.mockReset();
758+
mockCatRequest.mockReset();
759+
client.searchType = SearchType.Relevance;
760+
expect(mockFindRequest).toHaveBeenCalledTimes(1);
761+
expect(mockCatRequest).toHaveBeenCalledTimes(1);
680762
});
681763
});

src/SearchClient.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import {
1818
OrderBy,
1919
Query,
2020
SearchType,
21-
CategoryPresentation
21+
CategoryPresentation,
22+
CategorizationType
2223
} from "./Common";
2324
import { ICategory, IGroup } from "./Data";
2425
import { Find } from "./Find";
@@ -366,6 +367,30 @@ export class SearchClient implements AuthToken {
366367
return true;
367368
}
368369

370+
/**
371+
* Gets the currently active categorizationType value.
372+
*/
373+
get categorizationType(): CategorizationType {
374+
return this._query.categorizationType;
375+
}
376+
377+
/**
378+
* Sets the currently active categorizationType.
379+
*
380+
* Will run trigger-checks and potentially update services.
381+
*/
382+
set categorizationType(categorizationType: CategorizationType) {
383+
// tslint:disable-next-line:triple-equals
384+
if (categorizationType != this._query.categorizationType) {
385+
const oldValue = this._query.categorizationType;
386+
this._query.clientId = categorizationType;
387+
388+
this.autocomplete.categorizationTypeChanged(oldValue, this._query);
389+
this.categorize.categorizationTypeChanged(oldValue, this._query);
390+
this.find.categorizationTypeChanged(oldValue, this._query);
391+
}
392+
}
393+
369394
/**
370395
* Gets the currently active client-id value.
371396
*/

0 commit comments

Comments
 (0)