@@ -85,7 +85,7 @@ function makeContext() {
85
85
new ComicService ( { store : makeStore ( ) } )
86
86
)
87
87
const { makeServicePlugin, BaseModel } = feathersVuex ( feathersClient , {
88
- serverAlias : 'default '
88
+ serverAlias : 'service-module-mutations '
89
89
} )
90
90
class Comic extends BaseModel {
91
91
public static modelName = 'Comic'
@@ -210,6 +210,37 @@ describe('Service Module - Mutations', function () {
210
210
assert ( Object . keys ( state . keyedById ) . length === 0 )
211
211
} )
212
212
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
+
213
244
it ( 'removeItems with array of ids' , function ( ) {
214
245
const state = this . state
215
246
const items = [
@@ -251,8 +282,57 @@ describe('Service Module - Mutations', function () {
251
282
)
252
283
} )
253
284
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
+
254
329
it ( 'clearAll' , function ( ) {
255
330
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
+
256
336
const item1 = {
257
337
_id : 1 ,
258
338
test : true
@@ -264,9 +344,61 @@ describe('Service Module - Mutations', function () {
264
344
const items = [ item1 , item2 ]
265
345
addItems ( state , items )
266
346
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
+
267
359
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' )
270
402
} )
271
403
} )
272
404
@@ -877,6 +1009,7 @@ describe('Service Module - Mutations', function () {
877
1009
test : true
878
1010
}
879
1011
store . commit ( 'comics/addItem' , item1 )
1012
+
880
1013
// @ts -ignore
881
1014
const original = store . state . comics . keyedById [ 1 ]
882
1015
@@ -931,6 +1064,7 @@ describe('Service Module - Mutations', function () {
931
1064
test : true
932
1065
}
933
1066
store . commit ( 'comics/addItem' , item1 )
1067
+
934
1068
// @ts -ignore
935
1069
const original = store . state . comics . tempsById [ item1 . __id ]
936
1070
@@ -980,6 +1114,7 @@ describe('Service Module - Mutations', function () {
980
1114
test : true
981
1115
}
982
1116
store . commit ( 'comics/addItem' , item1 )
1117
+
983
1118
// @ts -ignore
984
1119
const original = store . state . comics . keyedById [ 1 ]
985
1120
0 commit comments