File size: 1,799 Bytes
1e92f2d |
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 |
import { createSelector } from 'reselect'
import { expect, test } from 'vitest'
interface RootState {
todos: { id: number; completed: boolean }[]
alerts: { id: number; read: boolean }[]
}
const state: RootState = {
todos: [
{ id: 0, completed: false },
{ id: 1, completed: true }
],
alerts: [
{ id: 0, read: false },
{ id: 1, read: true }
]
}
// With `Vitest` or `Jest`
test('selector unit test', () => {
const selectTodoIds = createSelector(
[(state: RootState) => state.todos],
todos => todos.map(({ id }) => id)
)
const firstResult = selectTodoIds(state)
const secondResult = selectTodoIds(state)
// Reference equality should pass.
expect(firstResult).toBe(secondResult)
// Deep equality should also pass.
expect(firstResult).toStrictEqual(secondResult)
selectTodoIds(state)
selectTodoIds(state)
selectTodoIds(state)
// The result function should not recalculate.
expect(selectTodoIds.recomputations()).toBe(1)
// input selectors should not recalculate.
expect(selectTodoIds.dependencyRecomputations()).toBe(1)
})
// With `Chai`
test('selector unit test', () => {
const selectTodoIds = createSelector(
[(state: RootState) => state.todos],
todos => todos.map(({ id }) => id)
)
const firstResult = selectTodoIds(state)
const secondResult = selectTodoIds(state)
// Reference equality should pass.
expect(firstResult).to.equal(secondResult)
// Deep equality should also pass.
expect(firstResult).to.deep.equal(secondResult)
selectTodoIds(state)
selectTodoIds(state)
selectTodoIds(state)
// The result function should not recalculate.
expect(selectTodoIds.recomputations()).to.equal(1)
// input selectors should not recalculate.
expect(selectTodoIds.dependencyRecomputations()).to.equal(1)
})
|