This repository has been archived by the owner on May 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 178
/
auth.spec.js
68 lines (57 loc) · 2.24 KB
/
auth.spec.js
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
const mockStatus = jest.fn()
const mockLogin = jest.fn()
jest.mock('@/authuser/api/auth', () => ({ login: mockLogin }))
jest.mock('@/authuser/api/authUser', () => ({ get: mockStatus }))
import { router } from '>/routerMocks'
import { createDatastore, createValidationError, throws } from '>/helpers'
describe('auth', () => {
let datastore
let datastoreMocks
beforeEach(() => jest.resetModules())
beforeEach(() => {
datastoreMocks = {
i18n: {
actions: {
setLocale: jest.fn(),
},
},
}
datastore = createDatastore({
auth: require('./auth').default,
...datastoreMocks,
})
})
const user = () => ({ displayName: 'Alex' })
it('can check login status', async () => {
mockStatus.mockReturnValueOnce(user())
await datastore.dispatch('auth/refresh')
expect(datastore.getters['auth/isLoggedIn']).toBe(true)
expect(datastore.getters['auth/user']).toBeDefined()
})
it('can login', async () => {
mockLogin.mockReturnValueOnce(user())
await datastore.dispatch('auth/login')
expect(datastore.getters['auth/loginStatus'].validationErrors).toEqual({})
expect(datastore.getters['auth/isLoggedIn']).toBe(true)
expect(datastore.getters['auth/user']).toBeDefined()
expect(router.push).toBeCalledWith('/')
})
it('can update user', () => {
const changed = { ...user(), displayName: 'Alex2' }
datastore.commit('auth/setUser', changed)
expect(datastore.getters['auth/user'].displayName).toEqual(changed.displayName)
})
it('will not be logged when status throws', async () => {
mockStatus.mockImplementation(throws(createValidationError({ foo: 'some error info' })))
await datastore.dispatch('auth/refresh')
expect(datastore.getters['auth/isLoggedIn']).toBe(false)
expect(datastore.getters['auth/user']).toBeNull()
})
it('will not be logged in when login throws', async () => {
mockLogin.mockImplementationOnce(throws(createValidationError({ foo: 'some error info' })))
await datastore.dispatch('auth/login')
expect(datastore.getters['auth/isLoggedIn']).toBe(false)
expect(datastore.getters['auth/loginStatus'].validationErrors).toEqual({ foo: 'some error info' })
expect(datastore.getters['auth/user']).toBeNull()
})
})