Skip to content

Commit bc04374

Browse files
authored
Optimized Jest tests (#29)
1 parent f44c7e0 commit bc04374

File tree

4 files changed

+99
-7
lines changed

4 files changed

+99
-7
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
22
/lib
3+
/coverage

__tests__/adaptRequest.test.ts

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,29 @@ import {
55
adaptRequest,
66
adaptNumericFilters,
77
parseRange,
8+
filterRegex,
89
} from "../src/adaptRequest";
910

11+
describe("filterRegex test", () => {
12+
it("filter regex should add string to object", () => {
13+
const itemsJsFacets = {};
14+
15+
const facet1 = "color:blue";
16+
const filter1 = filterRegex(itemsJsFacets, facet1);
17+
const result1 = {
18+
color: ["blue"],
19+
};
20+
expect(filter1).toStrictEqual(result1);
21+
22+
const facet2 = "color:red";
23+
const filter2 = filterRegex(itemsJsFacets, facet2);
24+
const result2 = {
25+
color: ["blue", "red"],
26+
};
27+
expect(filter2).toStrictEqual(result2);
28+
});
29+
});
30+
1031
describe("adaptPage tests", () => {
1132
it("adaptpage(x) should return x+1", () => {
1233
expect(adaptPage(0)).toBe(1);
@@ -16,14 +37,15 @@ describe("adaptPage tests", () => {
1637
});
1738

1839
describe("adaptRequest tests", () => {
19-
it("adaptRequest should convert request to ItemsJs request", () => {
40+
it("adaptRequest should convert (max parameters) request to ItemsJs request", () => {
2041
const instantsearchRequest: MultipleQueriesQuery = {
2142
indexName: "products",
2243
params: {
2344
query: "a",
2445
page: 2,
2546
hitsPerPage: 5,
2647
facets: ["price", "in_stock"],
48+
facetFilters: ["category:electronics"],
2749
numericFilters: ["price>=10", "price<=100"],
2850
},
2951
};
@@ -35,6 +57,26 @@ describe("adaptRequest tests", () => {
3557
expect(itemsjsRequest.per_page).toBe(5);
3658
expect(itemsjsRequest.aggregations).toMatchObject(["price", "in_stock"]);
3759
expect(itemsjsRequest.filter).toBeDefined(); // Returns native javascript .filter() function
60+
expect(itemsjsRequest.filters).toStrictEqual({ category: ["electronics"] });
61+
expect(itemsjsRequest.indexName).toBe("products");
62+
expect(itemsjsRequest.sort).toBe("products");
63+
});
64+
65+
it("adaptRequest should convert (min parameters) request to ItemsJs request ", () => {
66+
const instantsearchRequest: MultipleQueriesQuery = {
67+
indexName: "products",
68+
params: {
69+
query: "a",
70+
page: 2,
71+
hitsPerPage: 5,
72+
},
73+
};
74+
75+
const itemsjsRequest = adaptRequest(instantsearchRequest);
76+
77+
expect(itemsjsRequest.query).toBe("a");
78+
expect(itemsjsRequest.page).toBe(3);
79+
expect(itemsjsRequest.per_page).toBe(5);
3880
expect(itemsjsRequest.indexName).toBe("products");
3981
expect(itemsjsRequest.sort).toBe("products");
4082
});
@@ -136,8 +178,8 @@ describe("regexInput tests group in three", () => {
136178
});
137179
});
138180

139-
describe("adaptFacets tests", () => {
140-
it("adaptFacets should convert nested arrays to Itemsjs format", () => {
181+
describe("adaptFilters tests", () => {
182+
it("adaptFilters should convert nested arrays to Itemsjs format", () => {
141183
const instantsearchFacets = [
142184
["category:electronics", "category:men's clothing"],
143185
["color:blue"],
@@ -152,7 +194,7 @@ describe("adaptFacets tests", () => {
152194
expect(adaptedResult).toMatchObject(itemsJsFacets);
153195
});
154196

155-
it("adaptFacets should convert a single array to Itemsjs format", () => {
197+
it("adaptFilters should convert a single array to Itemsjs format", () => {
156198
const instantsearchFacets = [
157199
"category:electronics",
158200
"category:men's clothing",
@@ -166,7 +208,7 @@ describe("adaptFacets tests", () => {
166208
expect(adaptedResult).toMatchObject(itemsJsFacets);
167209
});
168210

169-
it("adaptFacets should convert single and nested arrays to Itemsjs format", () => {
211+
it("adaptFilters should convert single and nested arrays to Itemsjs format", () => {
170212
const instantsearchFacets = [
171213
["category:electronics", "category:men's clothing"],
172214
"color:Blue",
@@ -181,4 +223,14 @@ describe("adaptFacets tests", () => {
181223
const adaptedResult = adaptFilters(instantsearchFacets);
182224
expect(adaptedResult).toMatchObject(itemsJsFacets);
183225
});
226+
227+
it("adaptFilters should throw an error when no array is given", () => {
228+
const instantsearchFacets = "category:electronics";
229+
230+
expect(() => {
231+
adaptFilters(instantsearchFacets);
232+
}).toThrowError(
233+
new Error("request.params.facetFilters does not contain an array")
234+
);
235+
});
184236
});

__tests__/adapter.test.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import products from "./products.json";
2-
import { performSearch, createIndex } from "../src/adapter";
2+
import { performSearch, createIndex, getSearchClient } from "../src/adapter";
33
import {
44
MultipleQueriesQuery,
55
MultipleQueriesResponse,
@@ -52,6 +52,45 @@ const requests: MultipleQueriesQuery[] = [
5252
},
5353
];
5454

55+
describe("getSearchClient", () => {
56+
it("getSearchClient", () => {
57+
const queries: MultipleQueriesQuery[] = [
58+
{
59+
indexName: "instant_search",
60+
params: {
61+
highlightPreTag: "<ais-highlight-0000000000>",
62+
highlightPostTag: "</ais-highlight-0000000000>",
63+
query: "",
64+
maxValuesPerFacet: 10,
65+
page: 0,
66+
hitsPerPage: per_page,
67+
facets: ["category.lvl0"],
68+
tagFilters: "",
69+
},
70+
},
71+
{
72+
indexName: "instant_search",
73+
params: {
74+
highlightPreTag: "<ais-highlight-0000000000>",
75+
highlightPostTag: "</ais-highlight-0000000000>",
76+
query: "",
77+
maxValuesPerFacet: 10,
78+
page: 0,
79+
hitsPerPage: per_page,
80+
facets: ["price"],
81+
tagFilters: "",
82+
},
83+
},
84+
];
85+
86+
expect(() => {
87+
getSearchClient().searchForFacetValues();
88+
}).toThrowError(new Error("Not implemented"));
89+
90+
expect(getSearchClient().search(queries)).toBeDefined();
91+
});
92+
});
93+
5594
describe("performSearch", () => {
5695
it("Performs a search", async () => {
5796
createIndex(products, options);

src/adaptRequest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export function adaptFilters(instantsearchFacets) {
5656
return itemsJsFacets;
5757
}
5858

59-
function filterRegex(itemsJsFacets, facet) {
59+
export function filterRegex(itemsJsFacets, facet) {
6060
const facetRegex = new RegExp(/(.+)(:)(.+)/);
6161
const [, name, , value] = facet.match(facetRegex);
6262
if (itemsJsFacets[name]) {

0 commit comments

Comments
 (0)