generated from antfu/starter-ts
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.test.ts
75 lines (71 loc) · 2.16 KB
/
index.test.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
/* eslint-disable no-console */
import { describe, expect, it } from 'vitest'
import { query } from '../src'
import { omit } from '../src/utils'
import PaginationFixtures from './fixtures/pagination.fixture.json'
import SortingFixtures from './fixtures/sorting.fixture.json'
import FilteringFixtures from './fixtures/filtering.fixture.json'
import SearchFixtures from './fixtures/search.fixture.json'
const fixtures = [
{
key: 'pagination',
tests: PaginationFixtures,
},
{
key: 'sorting',
tests: SortingFixtures,
},
{
key: 'search',
tests: SearchFixtures,
},
{
key: 'filtering',
tests: FilteringFixtures,
},
]
for (const fixture of fixtures) {
describe(`${fixture.key} tests`, () => {
for (const test of fixture.tests) {
it(test.title, () => {
const result = (query as any)(test.data, test.query)
const matchResult = Array.isArray(result) ? { rows: result } : omit(result, ['unpaginatedRows'])
console.log('expected', JSON.stringify(test.result, null, 2))
console.log('actual', JSON.stringify(matchResult, null, 2))
expect(matchResult).toEqual(test.result)
})
}
})
}
describe('performance check', () => {
const getValue = () => Math.floor(Math.random() * 100)
const startItems = performance.now()
const items = Array.from({ length: 1000000 }, (_, i) => ({
id: i,
name: `Item ${i}`,
value: getValue(),
other: [],
address: { city: 'New York', country: 'USA' },
age: Math.floor(Math.random() * 100),
}))
const endItems = performance.now()
it('query 1M rows - paginate + sort + search + filter in less than 50ms', () => {
console.info('Time taken to generate 1M items:', endItems - startItems)
const start = performance.now()
query(items, {
limit: 100,
sort: [
{ key: 'age', dir: 'asc' },
{ key: 'name', dir: 'asc' },
],
search: {
value: 'Item 1',
keys: ['name'],
},
filter: [{ key: 'value', matchMode: 'greaterThan', value: 50 }],
})
const end = performance.now()
console.info('Time taken to query 1M items:', end - start)
expect(end - start).toBeLessThan(50)
})
})