-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreducer.test.js
141 lines (132 loc) · 4.75 KB
/
reducer.test.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
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
import { reducer, actions, selectors } from '../src'
import { saveSessionState } from '../src/persistenceHelpers'
describe('reducer', () => {
beforeEach(() => {
localStorage.clear()
sessionStorage.clear()
})
it('loads latest state from local storage when reset', () => {
const initialState = { user: { token: 'first token', persist: true }}
saveSessionState(initialState)
// Passing `undefined` for state resets the reducer
expect(reducer(undefined, {})).toEqual(initialState)
const newInitialState = { user: { token: 'other token', persist: true }}
saveSessionState(newInitialState)
expect(reducer(undefined, {})).toEqual(newInitialState)
})
})
// Tests for actions and selectors
describe('actions.setToken()', () => {
it('sets token in the state', () => {
const token = 'foo'
const initialState = {}
const action = actions.setToken(token)
const newState = reducer(initialState, action)
expect(newState.user.token).toEqual(token)
})
it('can receive a custom user type', () => {
const token = 'foo'
const userType = 'bar'
const initialState = {}
const action = actions.setToken(token, { userType })
const newState = reducer(initialState, action)
expect(newState.bar.token).toEqual(token)
})
it('defaults persist to true', () => {
const token = 'foo'
const initialState = {}
const action = actions.setToken(token)
const newState = reducer(initialState, action)
expect(newState.user.persist).toEqual(true)
})
it('can receive a false persist argument', () => {
const token = 'foo'
const initialState = {}
const action = actions.setToken(token, { persist: false })
const newState = reducer(initialState, action)
expect(newState.user.persist).toEqual(false)
})
})
describe('actions.clearToken()', () => {
it('clears token in the state', () => {
const initialState = { user: { token: 'foo' }}
const action = actions.clearToken()
const newState = reducer(initialState, action)
expect(newState.user.token).toEqual(null)
})
it('sets persist to false in state', () => {
const initialState = { user: { persist: true }}
const action = actions.clearToken()
const newState = reducer(initialState, action)
expect(newState.user.persist).toEqual(false)
})
it('can receive a custom user type', () => {
const userType = 'bar'
const initialState = { user: { token: 'foo' } }
const action = actions.clearToken({ userType })
const newState = reducer(initialState, action)
expect(newState.user.token).toEqual('foo')
expect(newState.bar.token).toEqual(null)
})
})
// Selectors
describe('selectors.token()', () => {
it('throws when session state is not available', () => {
const state = {}
expect(() => selectors.token(state)).toThrow()
})
it('fetches default user token from state', () => {
const token = 'foo'
const state = { sessions: { user: { token }}}
expect(selectors.token(state)).toEqual(token)
})
it('can receive a custom user type', () => {
const token = 'foo'
const userType = 'bar'
const state = { sessions: { bar: { token }}}
expect(selectors.token(state, { userType })).toEqual(token)
})
})
describe('selectors.isAuthenticated()', () => {
it('throws when session state is not available', () => {
const state = {}
expect(() => selectors.isAuthenticated(state)).toThrow()
})
it('returns true when user has token in state', () => {
const token = 'foo'
const state = { sessions: { user: { token }}}
expect(selectors.isAuthenticated(state)).toEqual(true)
})
it('returns false when user does not have token in state', () => {
const state = { sessions: {}}
expect(selectors.isAuthenticated(state)).toEqual(false)
})
it('can receive a custom user type', () => {
const token = 'foo'
const userType = 'bar'
const state = { sessions: { bar: { token }}}
expect(selectors.isAuthenticated(state, { userType })).toEqual(true)
})
})
describe('selectors.isUnauthenticated()', () => {
it('throws when session state is not available', () => {
const state = {}
expect(() => selectors.isUnauthenticated(state)).toThrow()
})
it('returns false when user has token in state', () => {
const token = 'foo'
const state = { sessions: { user: { token }}}
expect(selectors.isUnauthenticated(state)).toEqual(false)
})
it('returns true when user does not have token in state', () => {
const state = { sessions: {}}
expect(selectors.isUnauthenticated(state)).toEqual(true)
})
it('can receive a custom user type', () => {
const token = 'foo'
const userType = 'bar'
const state = { sessions: { bar: { token }}}
expect(selectors.isUnauthenticated(state, { userType })).toEqual(false)
})
})
export { reducer, selectors }