Newer
Older
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
import mockedAxios from 'axios'
import expect from 'expect' // You can use any testing library
import configureMockStore from 'redux-mock-store'
import thunk from 'redux-thunk'
import { UserFilterParams } from './../interfaces/FilterParams'
import { getSearchUsers, setFilterParams } from './searchUser'
import Types from './types'
const middlewares = [thunk]
const mockStore = configureMockStore(middlewares)
it('dispatches correct actions when getting users', async () => {
const userRes: any = {
data: {
items: [
{
id: 21,
name: 'ggff',
email: 'email@test.com',
year: 2021,
role_id: 1,
city_id: 0,
},
{
id: 22,
name: 'sssss',
email: 'email@test.com',
year: 2021,
role_id: 1,
city_id: 0,
},
],
count: 2,
total_count: 3,
},
}
;(mockedAxios.get as jest.Mock).mockImplementation((path: string, params?: any) => {
return Promise.resolve(userRes)
})
const expectedActions = [
{ type: Types.SET_SEARCH_USERS, payload: userRes.data.items },
{ type: Types.SET_SEARCH_USERS_TOTAL_COUNT, payload: userRes.data.total_count },
{ type: Types.SET_SEARCH_USERS_COUNT, payload: userRes.data.count },
]
const store = mockStore({ searchUsers: { filterParams: [] } })
await getSearchUsers()(store.dispatch, store.getState as any)
expect(store.getActions()).toEqual(expectedActions)
})
it('dispatches correct actions when setting filterParams', () => {
const testFilterParams: UserFilterParams = {
page: 0,
pageSize: 3,
name: 'name',
cityId: 0,
email: 'email@test.com',
roleId: 0,
}
const expectedActions = [{ type: Types.SET_SEARCH_USERS_FILTER_PARAMS, payload: testFilterParams }]
const store = mockStore({})
setFilterParams(testFilterParams)(store.dispatch)
expect(store.getActions()).toEqual(expectedActions)
})
it('dispatches no actions when failing to get users', async () => {
console.log = jest.fn()
;(mockedAxios.get as jest.Mock).mockImplementation((path: string, params?: any) => {
return Promise.reject(new Error('getting users failed'))
})
const store = mockStore({ searchUsers: { filterParams: [] } })
await getSearchUsers()(store.dispatch, store.getState as any)
expect(store.getActions()).toEqual([])
expect(console.log).toHaveBeenCalled()
})