Skip to content

Commit b470248

Browse files
committed
#4793 - add tests for slices
1 parent c6e58ce commit b470248

File tree

1 file changed

+145
-0
lines changed

1 file changed

+145
-0
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
import { cloneDeep } from 'lodash'
2+
import { cleanup, initialStateDefault, mockedStore, } from 'uiSrc/utils/test-utils'
3+
import { resourcesService } from 'uiSrc/services'
4+
import { MOCK_EXPLORE_GUIDES } from 'uiSrc/constants/mocks/mock-explore-guides'
5+
import reducer, {
6+
initialState,
7+
getGuideLinks,
8+
getGuideLinksSuccess,
9+
getGuideLinksFailure,
10+
fetchGuideLinksAction,
11+
guideLinksSelector
12+
} from '../../content/guide-links'
13+
14+
let store: typeof mockedStore
15+
beforeEach(() => {
16+
cleanup()
17+
store = cloneDeep(mockedStore)
18+
store.clearActions()
19+
})
20+
21+
describe('slices', () => {
22+
describe('reducer, actions and selectors', () => {
23+
it('should return the initial state on first run', () => {
24+
// Arrange
25+
const nextState = initialState
26+
27+
// Act
28+
const result = reducer(undefined, {})
29+
30+
// Assert
31+
expect(result).toEqual(nextState)
32+
})
33+
})
34+
35+
describe('getGuideLinks', () => {
36+
it('should properly set loading', () => {
37+
// Arrange
38+
const loading = true
39+
const state = {
40+
...initialState,
41+
loading
42+
}
43+
44+
// Act
45+
const nextState = reducer(initialState, getGuideLinks())
46+
47+
// Assert
48+
const rootState = Object.assign(initialStateDefault, {
49+
content: { guideLinks: nextState },
50+
})
51+
52+
expect(guideLinksSelector(rootState)).toEqual(state)
53+
})
54+
})
55+
56+
describe('getGuideLinksSuccess', () => {
57+
it('should properly set state after success', () => {
58+
// Arrange
59+
const data = MOCK_EXPLORE_GUIDES
60+
const state = {
61+
...initialState,
62+
data,
63+
loading: false,
64+
}
65+
66+
// Act
67+
const nextState = reducer(initialState, getGuideLinksSuccess(data))
68+
69+
// Assert
70+
const rootState = Object.assign(initialStateDefault, {
71+
content: { guideLinks: nextState },
72+
})
73+
74+
expect(guideLinksSelector(rootState)).toEqual(state)
75+
})
76+
})
77+
78+
describe('getGuideLinksFailure', () => {
79+
it('should properly set error', () => {
80+
// Arrange
81+
const error = 'error'
82+
const state = {
83+
...initialState,
84+
loading: false,
85+
error
86+
}
87+
88+
// Act
89+
const nextState = reducer(initialState, getGuideLinksFailure(error))
90+
91+
// Assert
92+
const rootState = Object.assign(initialStateDefault, {
93+
content: { guideLinks: nextState },
94+
})
95+
96+
expect(guideLinksSelector(rootState)).toEqual(state)
97+
})
98+
})
99+
100+
// thunks
101+
102+
describe('fetchGuideLinksAction', () => {
103+
it('succeed to fetch content', async () => {
104+
// Arrange
105+
const data = MOCK_EXPLORE_GUIDES
106+
const responsePayload = { status: 200, data }
107+
108+
resourcesService.get = jest.fn().mockResolvedValue(responsePayload)
109+
110+
// Act
111+
await store.dispatch<any>(fetchGuideLinksAction())
112+
113+
// Assert
114+
const expectedActions = [
115+
getGuideLinks(),
116+
getGuideLinksSuccess(data),
117+
]
118+
119+
expect(mockedStore.getActions()).toEqual(expectedActions)
120+
})
121+
122+
it('failed to fetch content', async () => {
123+
// Arrange
124+
const errorMessage = 'Something was wrong!'
125+
const responsePayload = {
126+
response: {
127+
status: 500,
128+
data: { message: errorMessage },
129+
},
130+
}
131+
resourcesService.get = jest.fn().mockRejectedValue(responsePayload)
132+
133+
// Act
134+
await store.dispatch<any>(fetchGuideLinksAction())
135+
136+
// Assert
137+
const expectedActions = [
138+
getGuideLinks(),
139+
getGuideLinksFailure(errorMessage),
140+
]
141+
142+
expect(mockedStore.getActions()).toEqual(expectedActions)
143+
})
144+
})
145+
})

0 commit comments

Comments
 (0)