Skip to content

Commit 1803ecb

Browse files
committed
tests: change mutations
- removeItem also removes clone - removeItems also removes clones - clearAll also clears Copies
1 parent b8bd0fd commit 1803ecb

File tree

1 file changed

+138
-3
lines changed

1 file changed

+138
-3
lines changed

test/service-module/service-module.mutations.test.ts

Lines changed: 138 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ function makeContext() {
8585
new ComicService({ store: makeStore() })
8686
)
8787
const { makeServicePlugin, BaseModel } = feathersVuex(feathersClient, {
88-
serverAlias: 'default'
88+
serverAlias: 'service-module-mutations'
8989
})
9090
class Comic extends BaseModel {
9191
public static modelName = 'Comic'
@@ -210,6 +210,37 @@ describe('Service Module - Mutations', function () {
210210
assert(Object.keys(state.keyedById).length === 0)
211211
})
212212

213+
it('removeItem also removes clone', function () {
214+
const state = this.state
215+
216+
const _id = 1
217+
218+
addItem(state, { _id, test: true })
219+
createCopy(state, _id)
220+
221+
assert(state.copiesById[_id], 'clone exists')
222+
223+
removeItem(state, _id)
224+
225+
assert(!state.copiesById[_id], 'clone is removed')
226+
})
227+
228+
it('removeItem also removes clone with keepCopiesInStore', function () {
229+
const context = makeContext()
230+
const { Comic, store } = context
231+
232+
const _id = 1
233+
234+
store.commit('comics/addItem', { _id, test: true })
235+
store.commit('comics/createCopy', _id)
236+
237+
assert(Comic.copiesById[_id], 'clone exists')
238+
239+
store.commit('comics/removeItem', _id)
240+
241+
assert(!Comic.copiesById[_id], 'clone is removed')
242+
})
243+
213244
it('removeItems with array of ids', function () {
214245
const state = this.state
215246
const items = [
@@ -251,8 +282,57 @@ describe('Service Module - Mutations', function () {
251282
)
252283
})
253284

285+
it('removeItems also removes clone', function () {
286+
const state = this.state
287+
288+
addItems(state, [
289+
{ _id: 1, test: true },
290+
{ _id: 2, test: true },
291+
{ _id: 3, test: true },
292+
{ _id: 4, test: true }
293+
])
294+
const itemsToRemove = [1, 2]
295+
createCopy(state, 1)
296+
createCopy(state, 3)
297+
298+
assert(state.copiesById[1], 'clone exists')
299+
300+
removeItems(state, itemsToRemove)
301+
302+
assert(!state.copiesById[1], 'clone is removed')
303+
assert(state.copiesById[3], 'other clone is not affected')
304+
})
305+
306+
it('removeItems also removes clone with keepCopiesInStore', function () {
307+
const context = makeContext()
308+
const { Comic, store } = context
309+
310+
store.commit('comics/addItems', [
311+
{ _id: 1, test: true },
312+
{ _id: 2, test: true },
313+
{ _id: 3, test: true },
314+
{ _id: 4, test: true }
315+
])
316+
317+
const itemsToRemove = [1, 2]
318+
store.commit('comics/createCopy', 1)
319+
store.commit('comics/createCopy', 3)
320+
321+
assert(Comic.copiesById[1], 'clone exists')
322+
323+
store.commit('comics/removeItems', itemsToRemove)
324+
325+
assert(!Comic.copiesById[1], 'clone is removed')
326+
assert(Comic.copiesById[3], 'other clone is not affected')
327+
})
328+
254329
it('clearAll', function () {
255330
const state = this.state
331+
332+
assert(state.ids.length === 0, 'initialy empty')
333+
assert(Object.keys(state.keyedById).length === 0, 'initialy empty')
334+
assert(Object.keys(state.copiesById).length === 0, 'initialy empty')
335+
256336
const item1 = {
257337
_id: 1,
258338
test: true
@@ -264,9 +344,61 @@ describe('Service Module - Mutations', function () {
264344
const items = [item1, item2]
265345
addItems(state, items)
266346

347+
createCopy(state, item1._id)
348+
349+
assert(state.ids.length === 2, 'ids are added correctly')
350+
assert(
351+
Object.keys(state.keyedById).length === 2,
352+
'items are added correctly'
353+
)
354+
assert(
355+
Object.keys(state.copiesById).length === 1,
356+
'clone is added correctly'
357+
)
358+
267359
clearAll(state)
268-
assert(state.ids.length === 0)
269-
assert(Object.keys(state.keyedById).length === 0)
360+
assert(state.ids.length === 0, 'ids empty again')
361+
assert(Object.keys(state.keyedById).length === 0, 'items empty again')
362+
assert(Object.keys(state.copiesById).length === 0, 'clones empty again')
363+
})
364+
365+
it('clearAll with keepCopiesInStore: false', function () {
366+
const context = makeContext()
367+
const { Comic, store } = context
368+
// @ts-ignore
369+
const state = store.state.comics
370+
371+
assert(state.ids.length === 0, 'initialy empty')
372+
assert(Object.keys(state.keyedById).length === 0, 'initialy empty')
373+
assert(Object.keys(Comic.copiesById).length === 0, 'initialy empty')
374+
375+
const item1 = {
376+
_id: 1,
377+
test: true
378+
}
379+
const item2 = {
380+
_id: 2,
381+
test: true
382+
}
383+
const items = [item1, item2]
384+
store.commit('comics/addItems', items)
385+
store.commit('comics/createCopy', item1._id)
386+
387+
assert(state.ids.length === 2, 'ids are added correctly')
388+
assert(
389+
Object.keys(state.keyedById).length === 2,
390+
'items are added correctly'
391+
)
392+
assert(
393+
Object.keys(Comic.copiesById).length === 1,
394+
'clone is added correctly'
395+
)
396+
397+
store.commit('comics/clearAll')
398+
399+
assert(state.ids.length === 0, 'ids empty again')
400+
assert(Object.keys(state.keyedById).length === 0, 'items empty again')
401+
assert(Object.keys(Comic.copiesById).length === 0, 'clones empty again')
270402
})
271403
})
272404

@@ -877,6 +1009,7 @@ describe('Service Module - Mutations', function () {
8771009
test: true
8781010
}
8791011
store.commit('comics/addItem', item1)
1012+
8801013
// @ts-ignore
8811014
const original = store.state.comics.keyedById[1]
8821015

@@ -931,6 +1064,7 @@ describe('Service Module - Mutations', function () {
9311064
test: true
9321065
}
9331066
store.commit('comics/addItem', item1)
1067+
9341068
// @ts-ignore
9351069
const original = store.state.comics.tempsById[item1.__id]
9361070

@@ -980,6 +1114,7 @@ describe('Service Module - Mutations', function () {
9801114
test: true
9811115
}
9821116
store.commit('comics/addItem', item1)
1117+
9831118
// @ts-ignore
9841119
const original = store.state.comics.keyedById[1]
9851120

0 commit comments

Comments
 (0)