This repository has been archived by the owner on Jan 9, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
user-slice.test.ts
160 lines (136 loc) · 4.68 KB
/
user-slice.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
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
import configureMockStore from 'redux-mock-store'
import thunk from 'redux-thunk'
import { remoteDb } from '../../shared/config/pouchdb'
import Permissions from '../../shared/model/Permissions'
import { RootState } from '../../shared/store'
import user, {
fetchPermissions,
getCurrentSession,
login,
loginSuccess,
loginError,
logout,
logoutSuccess,
} from '../../user/user-slice'
const mockStore = configureMockStore<RootState, any>([thunk])
describe('user slice', () => {
describe('reducers', () => {
it('should handle the FETCH_PERMISSIONS action', () => {
const expectedPermissions = [Permissions.ReadPatients, Permissions.WritePatients]
const userStore = user(undefined, {
type: fetchPermissions.type,
payload: expectedPermissions,
})
expect(userStore.permissions).toEqual(expectedPermissions)
})
it('should handle the LOGIN_SUCCESS action', () => {
const expectedUser = {
familyName: 'firstName',
givenName: 'lastName',
id: 'id',
}
const expectedPermissions = [Permissions.WritePatients]
const userStore = user(undefined, {
type: loginSuccess.type,
payload: { user: expectedUser, permissions: expectedPermissions },
})
expect(userStore.user).toEqual(expectedUser)
})
it('should handle the login error', () => {
const expectedError = 'error'
const userStore = user(undefined, {
type: loginError.type,
payload: expectedError,
})
expect(userStore.loginError).toEqual(expectedError)
})
it('should handle the logout success', () => {
const userStore = user(
{ user: { givenName: 'given', familyName: 'family', id: 'id' }, permissions: [] },
{
type: logoutSuccess.type,
},
)
expect(userStore.user).toEqual(undefined)
expect(userStore.permissions).toEqual([])
})
})
describe('login', () => {
beforeEach(() => {
jest.resetAllMocks()
})
it('should login with the username and password', async () => {
jest.spyOn(remoteDb, 'logIn').mockResolvedValue({ name: 'test', ok: true })
jest.spyOn(remoteDb, 'getUser').mockResolvedValue({
_id: 'userId',
metadata: {
givenName: 'test',
familyName: 'user',
},
} as any)
const store = mockStore()
const expectedUsername = 'test'
const expectedPassword = 'password'
await store.dispatch(login(expectedUsername, expectedPassword))
expect(remoteDb.logIn).toHaveBeenCalledTimes(1)
expect(remoteDb.logIn).toHaveBeenLastCalledWith(expectedUsername, expectedPassword)
expect(remoteDb.getUser).toHaveBeenCalledWith(expectedUsername)
expect(store.getActions()[0]).toEqual({
type: loginSuccess.type,
payload: expect.objectContaining({
user: { familyName: 'user', givenName: 'test', id: 'userId' },
}),
})
})
it('should dispatch login error if login was not successful', async () => {
jest.spyOn(remoteDb, 'logIn').mockRejectedValue({ status: 401 })
jest.spyOn(remoteDb, 'getUser').mockResolvedValue({
_id: 'userId',
metadata: {
givenName: 'test',
familyName: 'user',
},
} as any)
const store = mockStore()
await store.dispatch(login('user', 'password'))
expect(remoteDb.getUser).not.toHaveBeenCalled()
expect(store.getActions()[0]).toEqual({
type: loginError.type,
payload: { message: 'user.login.error.message.incorrect' },
})
})
})
describe('logout', () => {
beforeEach(() => {
jest.resetAllMocks()
})
it('should logout the user', async () => {
jest.spyOn(remoteDb, 'logOut').mockImplementation(jest.fn())
const store = mockStore()
await store.dispatch(logout())
expect(remoteDb.logOut).toHaveBeenCalledTimes(1)
expect(store.getActions()[0]).toEqual({ type: logoutSuccess.type })
})
})
describe('getCurrentSession', () => {
it('should get the detail of the current user and update the store', async () => {
jest.spyOn(remoteDb, 'getUser').mockResolvedValue({
_id: 'userId',
metadata: {
givenName: 'test',
familyName: 'user',
},
} as any)
const store = mockStore()
const expectedUsername = 'test'
await store.dispatch(getCurrentSession(expectedUsername))
expect(remoteDb.getUser).toHaveBeenCalledWith(expectedUsername)
expect(store.getActions()[0]).toEqual({
type: loginSuccess.type,
payload: expect.objectContaining({
user: { familyName: 'user', givenName: 'test', id: 'userId' },
}),
})
})
})
})