-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Expand file tree
/
Copy pathwithSearch.test.jsx
More file actions
67 lines (57 loc) · 1.68 KB
/
withSearch.test.jsx
File metadata and controls
67 lines (57 loc) · 1.68 KB
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
import assert from 'node:assert/strict';
import { describe, it, mock } from 'node:test';
const fakeDb = {};
const createMock = mock.fn(() => fakeDb);
const insertMultipleMock = mock.fn(async () => {});
const searchMock = mock.fn(async (_db, options) => ({ options }));
mock.module('@orama/orama', {
namedExports: {
create: createMock,
insertMultiple: insertMultipleMock,
search: searchMock,
},
});
const { addPrefixToDocs, createOramaClient } = await import(
'#site/components/withSearch'
);
describe('withSearch', () => {
it('adds the locale prefix to indexed document URLs', () => {
const db = {
docs: {
docs: {
one: { href: '/docs', title: 'Docs' },
},
},
};
assert.deepEqual(addPrefixToDocs(db, '/en'), {
docs: {
docs: {
one: { href: '/en/docs', title: 'Docs' },
},
},
});
});
it('warms the Orama indexes only once before searching', async () => {
createMock.mock.resetCalls();
insertMultipleMock.mock.resetCalls();
searchMock.mock.resetCalls();
global.fetch = mock.fn(async url => ({
json: async () => ({
docs: {
docs: {
[String(url)]: { href: '/result', title: 'Node.js' },
},
},
}),
}));
const { client, warmup } = createOramaClient({
'/docs': 'https://example.com/docs.json',
'/learn': 'https://example.com/learn.json',
});
await Promise.all([warmup(), warmup()]);
await client.search({ term: 'node' });
assert.equal(global.fetch.mock.callCount(), 2);
assert.equal(insertMultipleMock.mock.callCount(), 2);
assert.equal(searchMock.mock.callCount(), 1);
});
});