/
searchkit.test.tsx
165 lines (146 loc) · 5.65 KB
/
searchkit.test.tsx
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { renderHook, act } from '@testing-library/react-hooks'
import React from 'react'
import { useLazyQuery } from '@apollo/client'
import {
SearchkitClient,
SearchkitContext,
SearchkitProvider,
useSearchkit,
useSearchkitQuery
} from '../searchkit'
jest.mock('@apollo/client', () => ({
useLazyQuery: jest.fn().mockReturnValue([jest.fn(), {}])
}))
describe('Searchkit Client', () => {
it('should be exportable', () => {
expect(SearchkitClient).not.toBeNull()
expect(SearchkitProvider).not.toBeNull()
expect(SearchkitContext).not.toBeNull()
expect(useSearchkit).not.toBeNull()
})
it('default state to be empty', () => {
const api = new SearchkitClient()
expect(api.getQuery()).toBe('')
expect(api.getFilters()).toEqual([])
expect(api.getFiltersByIdentifier('anId')).toEqual([])
expect(api.canResetSearch()).toBe(false)
})
it('should be able to manipulate query', () => {
const api = new SearchkitClient()
expect(api.getQuery()).toBe('')
api.setQuery('heat')
expect(api.getQuery()).toBe('heat')
expect(api.canResetSearch()).toBeTruthy()
api.setQuery('')
expect(api.canResetSearch()).toBeFalsy()
})
it('should be able to manipulate filters', () => {
const api = new SearchkitClient()
expect(api.getFilters()).toEqual([])
api.addFilter({ identifier: 'type', value: 'Movie' })
expect(api.getFilters()).toEqual([{ identifier: 'type', value: 'Movie' }])
expect(api.getFiltersByIdentifier('type')).toEqual([{ identifier: 'type', value: 'Movie' }])
expect(api.getFiltersByIdentifier('missing')).toEqual([])
expect(api.canResetSearch()).toBeTruthy()
// setting the query should reset filters
api.setQuery('new query')
expect(api.getFilters()).toEqual([])
expect(api.canResetSearch()).toBeTruthy()
})
it('should be able to manage pagination', () => {
const api = new SearchkitClient()
api.setPage({ from: 10, size: 10 })
})
it('should toggle filter and keep order', () => {
const api = new SearchkitClient()
api.addFilter({ identifier: 'type', value: 'Movies' })
api.addFilter({ identifier: 'type', value: 'Games' })
expect(api.getFilters()).toEqual([
{ identifier: 'type', value: 'Games' },
{ identifier: 'type', value: 'Movies' }
])
api.toggleFilter({ identifier: 'type', value: 'Movies' })
expect(api.getFilters()).toEqual([{ identifier: 'type', value: 'Games' }])
api.toggleFilter({ identifier: 'type', value: 'Movies' })
expect(api.getFilters()).toEqual([
{ identifier: 'type', value: 'Movies' },
{ identifier: 'type', value: 'Games' }
])
})
it('should toggle range filters', () => {
const api = new SearchkitClient()
api.addFilter({ identifier: 'type', min: 0, max: 100 })
expect(api.getFilters()).toEqual([{ identifier: 'type', min: 0, max: 100 }])
api.toggleFilter({ identifier: 'type', min: 0, max: 100 })
expect(api.getFilters()).toEqual([])
})
it('should remove multiple filters by id', () => {
const api = new SearchkitClient()
api.addFilter({ identifier: 'type', value: 'Movies' })
api.addFilter({ identifier: 'type', value: 'Games' })
api.removeFiltersByIdentifier('type')
expect(api.getFilters()).toEqual([])
})
it('should determine whether a filter has been applied or not', () => {
const api = new SearchkitClient()
api.addFilter({ identifier: 'type', value: 'Movies' })
api.addFilter({ identifier: 'type', value: 'Games' })
expect(api.isFilterSelected({ identifier: 'type', value: 'Movies' })).toBeTruthy()
expect(api.isFilterSelected({ identifier: 'type', value: 'no match' })).toBeFalsy()
})
it('should pass state within callback', () => {
const callback = jest.fn()
const api = new SearchkitClient()
api.setCallbackFn(callback)
api.setQuery('test')
api.addFilter({ identifier: 'type', value: 'Movies' })
api.setPage({ from: 10, size: 20 })
api.setSortBy('released')
api.search()
expect(callback).toBeCalledWith({
filters: [{ identifier: 'type', value: 'Movies' }],
page: { from: 10, size: 20 },
query: 'test',
sortBy: 'released'
})
})
it('Searchkit Provider + Hook', () => {
const api = new SearchkitClient()
const wrapper = ({ children }) => <SearchkitProvider client={api}>{children}</SearchkitProvider>
const { result } = renderHook(() => useSearchkit(), { wrapper })
act(() => {
result.current.setQuery('test')
})
expect(api.getQuery()).toBe('test')
})
it('useSearchQuery', () => {
const api = new SearchkitClient({ searchOnLoad: true })
api.setQuery('test')
api.addFilter({ identifier: 'type', value: 'Movies' })
api.setSortBy('released')
api.search = jest.fn()
const wrapper = ({ children }) => <SearchkitProvider client={api}>{children}</SearchkitProvider>
renderHook(() => useSearchkitQuery('gqlQuery'), { wrapper })
expect(useLazyQuery).toHaveBeenCalledWith('gqlQuery', {
variables: {
filters: [{ identifier: 'type', value: 'Movies' }],
page: { from: 0, size: 10 },
query: 'test',
sortBy: 'released'
}
})
expect(api.search).toBeCalled()
})
it('searchOnLoad', () => {
;(useLazyQuery as any).mockReset()
const api = new SearchkitClient({ searchOnLoad: false })
api.setQuery('test')
api.addFilter({ identifier: 'type', value: 'Movies' })
api.setSortBy('released')
api.search = jest.fn()
const wrapper = ({ children }) => <SearchkitProvider client={api}>{children}</SearchkitProvider>
renderHook(() => useSearchkitQuery('gqlQuery'), { wrapper })
expect(useLazyQuery).toHaveBeenCalled()
expect(api.search).not.toBeCalled()
})
})