Skip to content

Commit a574394

Browse files
committed
refactor(tests): properly configure test runner from fixtures
1 parent e8f36b8 commit a574394

1 file changed

Lines changed: 52 additions & 17 deletions

File tree

test/index.test.ts

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,62 @@
11
import { describe, expect, it } from 'vitest'
22
import { query } from '../src'
3-
import { generateUsers } from './fixtures/user.fixture'
3+
import PaginationFixtures from './fixtures/pagination.fixture.json'
4+
import SortingFixtures from './fixtures/sorting.fixture.json'
5+
import FilteringFixtures from './fixtures/filtering.fixture.json'
6+
import SearchFixtures from './fixtures/search.fixture.json'
47

5-
describe('should filter users', () => {
6-
const users = generateUsers(100)
7-
const activeUsers = users.filter(user => user.status === 'active')
8-
it('should filter active users', () => {
9-
const { totalPages, totalRows } = query(users, {
8+
const fixtures = [
9+
{
10+
key: 'pagination',
11+
tests: PaginationFixtures,
12+
},
13+
{
14+
key: 'sorting',
15+
tests: SortingFixtures,
16+
},
17+
{
18+
key: 'search',
19+
tests: SearchFixtures,
20+
},
21+
{
22+
key: 'filtering',
23+
tests: FilteringFixtures,
24+
},
25+
]
26+
27+
for (const fixture of fixtures) {
28+
describe(`${fixture.key} tests`, () => {
29+
for (const test of fixture.tests) {
30+
it(test.title, () => {
31+
const { unpaginatedRows, ...result } = (query as any)(test.data, test.query)
32+
expect(result).toEqual(test.result)
33+
})
34+
}
35+
})
36+
}
37+
38+
describe('performance check', () => {
39+
// VAL BETWEEN 1 & 100
40+
const getValue = () => Math.floor(Math.random() * 100)
41+
const items = Array.from({ length: 1000000 }, (_, i) => ({ id: i, name: `Item ${i}`, value: getValue() }))
42+
it('query 1M rows - paginate + sort + search + filter in less than 500ms', () => {
43+
const start = performance.now()
44+
query(items, {
1045
limit: 10,
1146
sort: {
12-
key: 'createdAt',
13-
dir: 'desc',
47+
key: 'id',
48+
dir: 'asc',
49+
},
50+
search: {
51+
value: 'Item 1',
52+
keys: ['name'],
1453
},
15-
filter: [
16-
{
17-
key: 'status',
18-
matchMode: 'equals',
19-
value: 'active',
20-
},
21-
],
54+
filter: [{ key: 'value', matchMode: 'greaterThan', value: 50 }],
2255
})
2356

24-
expect(totalRows).toEqual(activeUsers.length)
25-
expect(totalPages).toEqual(Math.ceil(activeUsers.length / 10))
57+
const end = performance.now()
58+
// eslint-disable-next-line no-console
59+
console.info('Time taken:', end - start)
60+
expect(end - start).toBeLessThan(500)
2661
})
2762
})

0 commit comments

Comments
 (0)