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
/
groups.spec.js
206 lines (174 loc) · 5.71 KB
/
groups.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
const mockJoin = jest.fn()
const mockLeave = jest.fn()
const mockSave = jest.fn()
jest.mock('@/group/api/groups', () => ({
join: mockJoin,
leave: mockLeave,
save: mockSave,
}))
const mockFetchGroupsPreview = jest.fn()
jest.mock('@/groupInfo/api/groupsInfo', () => ({
list: mockFetchGroupsPreview,
}))
import { router } from '>/routerMocks'
import { createDatastore, createValidationError, throws, statusMocks } from '>/helpers'
import { enrichGroup } from '>/datastoreHelpers'
describe('groups', () => {
beforeEach(() => jest.resetModules())
let datastore
const userId = 5
let group1
let group2
let group3
beforeEach(() => {
group1 = { id: 1, name: 'group 1', memberCount: 0 }
group2 = { id: 2, name: 'group 2', memberCount: 1, isMember: true }
group3 = { id: 3, name: 'group 3', memberCount: 1, isMember: true }
})
// Reusable datastore mocks
const agreements = {
getters: {
get: () => () => {},
},
}
const auth = {
getters: {
userId: () => userId,
},
actions: {
update: jest.fn(),
maybeBackgroundSave: jest.fn(),
},
}
const banners = {
actions: {
create: jest.fn(),
},
}
const toasts = {
actions: {
show: jest.fn(),
},
}
const users = {
getters: {
get () {
return id => ({ id, name: `Some Name${id}` })
},
},
}
const currentGroup = {
actions: {
maybeUpdate: jest.fn(),
},
mutations: {
clear: jest.fn(),
},
}
describe('logged out', () => {
beforeEach(() => {
datastore = createDatastore({
groups: require('./groups').default,
agreements,
})
})
it('can fetch the group list', async () => {
mockFetchGroupsPreview.mockReturnValueOnce([group1])
await datastore.dispatch('groups/fetch')
expect(datastore.getters['groups/all']).toEqual([enrichGroup(group1)])
})
it('can not join a group', async () => {
mockJoin.mockImplementationOnce(throws(new Error('some error')))
datastore.commit('groups/set', [group1])
await expect(datastore.dispatch('groups/join', group1.id)).rejects.toHaveProperty('message', 'some error')
})
})
describe('logged in', () => {
beforeEach(() => {
datastore = createDatastore({
groups: require('./groups').default,
auth,
agreements,
banners,
currentGroup,
toasts,
})
})
beforeEach(() => {
datastore.commit('groups/set', [group1, group2, group3])
})
it('can fetch my group list', async () => {
expect(datastore.getters['groups/mineWithApplications'].map(e => e.id)).toEqual([group2.id, group3.id])
})
it('can join a group', async () => {
mockJoin.mockReturnValueOnce({})
expect(datastore.getters['groups/mineWithApplications'].map(e => e.id)).toEqual([group2.id, group3.id])
await datastore.dispatch('groups/join', group1.id)
expect(router.push).toBeCalledWith({ name: 'group', params: { groupId: group1.id } })
expect(mockJoin).toBeCalledWith(group1.id)
})
it('can leave a group', async () => {
mockLeave.mockReturnValueOnce({})
expect(datastore.getters['groups/mineWithApplications'].map(e => e.id)).toEqual([group2.id, group3.id])
await datastore.dispatch('groups/leave', group2.id)
expect(mockLeave).toBeCalledWith(group2.id)
expect(auth.actions.maybeBackgroundSave).toBeCalled()
expect(auth.actions.maybeBackgroundSave.mock.calls[0][1]).toEqual({ currentGroup: null })
expect(currentGroup.mutations.clear).toBeCalled()
expect(toasts.actions.show).toBeCalled()
})
it('can save a group', async () => {
mockSave.mockImplementationOnce(group => group)
await datastore.dispatch('groups/save', { id: group2.id, name: 'new name' })
})
it('adds save errors to the group', async () => {
const validationErrors = { foo: ['bar'] }
mockSave.mockImplementationOnce(throws(createValidationError(validationErrors)))
await datastore.dispatch('groups/save', { id: group2.id, name: 'new name' })
expect(datastore.getters['groups/get'](group2.id)).toEqual({
...enrichGroup(group2),
saveStatus: statusMocks.validationError('foo', 'bar'),
})
})
it('can update a group', async () => {
const changed = { ...group1, name: 'new name' }
datastore.commit('groups/update', [changed])
expect(datastore.getters['groups/get'](changed.id).name).toEqual(changed.name)
})
})
describe('getters', () => {
beforeEach(() => {
datastore = createDatastore({
groups: require('./groups').default,
agreements,
auth,
users,
})
})
beforeEach(() => {
datastore.commit('groups/set', [group1, group2, group3])
})
it('can get a group', () => {
expect(datastore.getters['groups/get'](group1.id)).toEqual(enrichGroup(group1))
})
it('can get myGroups', () => {
expect(datastore.getters['groups/mineWithApplications']).toEqual([group2, group3].map(enrichGroup))
})
it('can get otherGroups', () => {
expect(datastore.getters['groups/other']).toEqual([group1].map(enrichGroup))
})
})
describe('groupPreview', () => {
beforeEach(() => {
datastore = createDatastore({
groups: require('./groups').default,
})
})
it('throws routeError if group does not exist', async () => {
const select = datastore.dispatch('groups/selectPreview', { groupPreviewId: 9999 })
await expect(select).rejects.toHaveProperty('type', 'RouteError')
await expect(select).rejects.toHaveProperty(['data', 'translation'], 'NOT_FOUND.EXPLANATION')
expect(datastore.getters['groups/activePreview']).toBeUndefined()
})
})
})