-
Notifications
You must be signed in to change notification settings - Fork 84
/
fetchIndexes.spec.ts
84 lines (74 loc) · 2.02 KB
/
fetchIndexes.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import lunr from "lunr";
import { fetchIndexes } from "./fetchIndexes";
jest.mock("lunr");
jest.mock("../../utils/proxiedGenerated");
const mockLunrIndexLoad = (jest.spyOn(
lunr.Index,
"load"
) as any).mockImplementation(() => `loaded-index`);
const mockFetch = (global.fetch = jest.fn());
describe("fetchIndexes", () => {
const baseUrl = "/";
const OLD_ENV = process.env;
beforeEach(() => {
jest.resetModules(); // most important - it clears the cache
process.env = { ...OLD_ENV }; // make a copy
});
afterAll(() => {
process.env = OLD_ENV; // restore old env
});
test("production with empty index", async () => {
process.env.NODE_ENV = "production";
mockFetch.mockResolvedValueOnce({
json: () => Promise.resolve([]),
});
const result = await fetchIndexes(baseUrl);
expect(mockFetch).toBeCalledWith("/search-index.json?_=abc");
expect(result).toEqual({
wrappedIndexes: [],
zhDictionary: [],
});
});
test("production", async () => {
process.env.NODE_ENV = "production";
mockLunrIndexLoad;
mockFetch.mockResolvedValueOnce({
json: () =>
Promise.resolve([
{
documents: [1, 2, 3],
index: {
invertedIndex: [
["hello"],
["世界"],
["和平"],
["世界"],
["hello"],
],
},
},
]),
});
const result = await fetchIndexes(baseUrl);
expect(mockFetch).toBeCalledWith("/search-index.json?_=abc");
expect(result).toEqual({
wrappedIndexes: [
{
documents: [1, 2, 3],
index: "loaded-index",
type: 0,
},
],
zhDictionary: ["世界", "和平"],
});
});
test("development", async () => {
process.env.NODE_ENV = "development";
const result = await fetchIndexes(baseUrl);
expect(mockFetch).not.toBeCalled();
expect(result).toEqual({
wrappedIndexes: [],
zhDictionary: [],
});
});
});