@@ -23,8 +23,9 @@ interface TodoState extends ServiceState {
23
23
isTrue : boolean
24
24
}
25
25
interface RootState {
26
- todos : TodoState
27
- tasks : ServiceState
26
+ [ 'model-methods-persons' ] : ServiceState
27
+ [ 'model-methods-todos' ] : TodoState
28
+ [ 'model-methods-tasks' ] : ServiceState
28
29
tests : ServiceState
29
30
blah : ServiceState
30
31
things : ServiceState
@@ -42,9 +43,9 @@ function makeContext() {
42
43
context . result = JSON . parse ( JSON . stringify ( context . result ) )
43
44
}
44
45
45
- feathersClient . use ( 'letters' , memory ( ) )
46
+ feathersClient . use ( 'model-methods- letters' , memory ( ) )
46
47
47
- const lettersService = feathersClient . service ( 'letters' )
48
+ const lettersService = feathersClient . service ( 'model-methods- letters' )
48
49
49
50
// Setup hooks on letters service to simulate toJSON serialization that occurs
50
51
// with a remote API request.
@@ -63,14 +64,14 @@ function makeContext() {
63
64
64
65
class Task extends BaseModel {
65
66
public static modelName = 'Task'
66
- public static servicePath : 'tasks'
67
+ public static servicePath : 'model-methods- tasks'
67
68
public constructor ( data ?, options ?) {
68
69
super ( data , options )
69
70
}
70
71
}
71
72
class Todo extends BaseModel {
72
73
public static modelName = 'Todo'
73
- public static servicePath : 'todos'
74
+ public static servicePath : 'model-methods- todos'
74
75
public constructor ( data ?, options ?) {
75
76
super ( data , options )
76
77
}
@@ -81,6 +82,7 @@ function makeContext() {
81
82
super ( data , options )
82
83
}
83
84
public static modelName = 'Letter'
85
+ public static servicePath = 'model-methods-letters'
84
86
public static instanceDefaults ( data , { models, store } ) {
85
87
return {
86
88
to : '' ,
@@ -100,7 +102,7 @@ function makeContext() {
100
102
101
103
class Person extends BaseModel {
102
104
public static modelName = 'Person'
103
- public static servicePath : ' persons'
105
+ public static servicePath = 'model-methods- persons'
104
106
public constructor ( data ?, options ?) {
105
107
super ( data , options )
106
108
}
@@ -111,26 +113,101 @@ function makeContext() {
111
113
plugins : [
112
114
makeServicePlugin ( {
113
115
Model : Task ,
114
- service : feathersClient . service ( 'tasks' ) ,
115
- preferUpdate : true
116
+ servicePath : 'model-methods-tasks' ,
117
+ service : feathersClient . service ( 'model-methods-tasks' ) ,
118
+ preferUpdate : true ,
119
+ namespace : 'model-methods-tasks'
116
120
} ) ,
117
121
makeServicePlugin ( {
118
122
Model : Todo ,
119
- service : feathersClient . service ( 'todos' )
123
+ servicePath : 'model-methods-todos' ,
124
+ service : feathersClient . service ( 'model-methods-todos' ) ,
125
+ namespace : 'model-methods-todos'
120
126
} ) ,
121
127
makeServicePlugin ( {
122
128
Model : Letter ,
123
- servicePath : 'letters' ,
124
- service : feathersClient . service ( 'letters' )
129
+ servicePath : 'model-methods-letters' ,
130
+ service : feathersClient . service ( 'model-methods-letters' ) ,
131
+ namespace : 'model-methods-letters'
125
132
} ) ,
126
133
makeServicePlugin ( {
127
134
Model : Person ,
128
- servicePath : 'persons' ,
129
- service : feathersClient . service ( 'persons' ) ,
130
- keepCopiesInStore : true
135
+ servicePath : 'model-methods-persons' ,
136
+ service : feathersClient . service ( 'model-methods-persons' ) ,
137
+ keepCopiesInStore : true ,
138
+ namespace : 'model-methods-persons'
131
139
} )
132
140
]
133
141
} )
142
+
143
+ // Fake server call
144
+ feathersClient . service ( 'model-methods-tasks' ) . hooks ( {
145
+ before : {
146
+ create : [
147
+ context => {
148
+ delete context . data . __id
149
+ delete context . data . __isTemp
150
+ } ,
151
+ context => {
152
+ context . result = { _id : 24 , ...context . data }
153
+ return context
154
+ }
155
+ ] ,
156
+ update : [
157
+ context => {
158
+ context . result = { ...context . data }
159
+ return context
160
+ }
161
+ ] ,
162
+ patch : [
163
+ context => {
164
+ context . result = { ...context . data }
165
+ return context
166
+ }
167
+ ] ,
168
+ remove : [
169
+ context => {
170
+ context . result = { }
171
+ return context
172
+ }
173
+ ]
174
+ }
175
+ } )
176
+
177
+ // Fake server call
178
+ feathersClient . service ( 'model-methods-persons' ) . hooks ( {
179
+ before : {
180
+ create : [
181
+ context => {
182
+ delete context . data . __id
183
+ delete context . data . __isTemp
184
+ } ,
185
+ context => {
186
+ context . result = { _id : 24 , ...context . data }
187
+ return context
188
+ }
189
+ ] ,
190
+ update : [
191
+ context => {
192
+ context . result = { ...context . data }
193
+ return context
194
+ }
195
+ ] ,
196
+ patch : [
197
+ context => {
198
+ context . result = { ...context . data }
199
+ return context
200
+ }
201
+ ] ,
202
+ remove : [
203
+ context => {
204
+ context . result = { }
205
+ return context
206
+ }
207
+ ]
208
+ }
209
+ } )
210
+
134
211
return {
135
212
BaseModel,
136
213
Task,
@@ -295,43 +372,94 @@ describe('Models - Methods', function () {
295
372
296
373
task . remove ( )
297
374
375
+ assert (
376
+ ! store . state [ 'model-methods-tasks' ] . tempsById [ tempId ] ,
377
+ 'temp was removed'
378
+ )
379
+ } )
380
+
381
+ it ( 'instance.remove removes cloned record from the store' , async function ( ) {
382
+ const { Person, store } = makeContext ( )
383
+ const person = new Person ( { _id : 1 , test : true } )
384
+ const id = person . _id
385
+
298
386
// @ts -ignore
299
- assert ( ! store . state . tasks . tempsById [ tempId ] , 'temp was removed' )
387
+ const { copiesById } = store . state [ 'model-methods-persons' ]
388
+
389
+ person . clone ( )
390
+
391
+ assert ( copiesById [ id ] , 'clone exists' )
392
+
393
+ await person . remove ( )
394
+
395
+ assert ( ! copiesById [ id ] , 'clone was removed' )
300
396
} )
301
397
302
- it . skip ( 'instance.remove removes cloned records from the store' , function ( ) {
398
+ it ( 'instance.remove removes cloned record from Model.copiesById' , async function ( ) {
399
+ const { Task } = makeContext ( )
400
+ const task = new Task ( { _id : 2 , test : true } )
401
+ const id = task . _id
402
+
403
+ task . clone ( )
404
+
405
+ assert ( Task . copiesById [ id ] , 'clone exists' )
406
+
407
+ await task . remove ( )
408
+
409
+ assert ( ! Task . copiesById [ id ] , 'clone was removed' )
410
+ } )
411
+
412
+ it ( 'instance.remove for temp record removes cloned record from the store' , function ( ) {
303
413
const { Person, store } = makeContext ( )
304
414
const person = new Person ( { test : true } )
305
415
const tempId = person . __id
306
416
417
+ // @ts -ignore
418
+ const { copiesById } = store . state [ 'model-methods-persons' ]
419
+
307
420
person . clone ( )
308
421
309
- // @ts -ignore
310
- assert ( store . state . persons . copiesById [ tempId ] , 'clone exists' )
422
+ assert ( copiesById [ tempId ] , 'clone exists' )
311
423
312
424
person . remove ( )
313
425
314
- // @ts -ignore
315
- assert ( ! store . state . persons . copiesById [ tempId ] , 'clone was removed' )
426
+ assert ( ! copiesById [ tempId ] , 'clone was removed' )
316
427
} )
317
428
318
- it . skip ( 'instance.remove removes cloned records from the Model.copiesById' , function ( ) {
319
- const { Task, store } = makeContext ( )
429
+ it ( 'instance.remove for temp record removes cloned record from the Model.copiesById' , function ( ) {
430
+ const { Task } = makeContext ( )
320
431
const task = new Task ( { test : true } )
321
432
const tempId = task . __id
322
433
323
434
task . clone ( )
324
435
325
- // @ts -ignore
326
436
assert ( Task . copiesById [ tempId ] , 'clone exists' )
327
437
328
438
task . remove ( )
329
439
330
- // @ts -ignore
331
440
assert ( ! Task . copiesById [ tempId ] , 'clone was removed' )
332
441
} )
333
442
334
- it . skip ( 'removes clone and original upon calling clone.remove()' , function ( ) { } )
443
+ it ( 'removes clone and original upon calling clone.remove()' , async function ( ) {
444
+ const { Person, store } = makeContext ( )
445
+ const person = new Person ( { _id : 1 , test : true } )
446
+ const id = person . _id
447
+
448
+ // @ts -ignore
449
+ const { copiesById, keyedById } = store . state [ 'model-methods-persons' ]
450
+
451
+ person . clone ( )
452
+
453
+ assert ( copiesById [ id ] , 'clone exists' )
454
+ assert ( keyedById [ id ] , 'original exists' )
455
+
456
+ const clone = copiesById [ id ]
457
+
458
+ await clone . remove ( )
459
+
460
+ assert ( ! copiesById [ id ] , 'clone was removed' )
461
+ assert ( ! keyedById [ id ] , 'original was removed' )
462
+ } )
335
463
336
464
it ( 'instance methods still available in store data after updateItem mutation (or socket event)' , async function ( ) {
337
465
const { Letter, store, lettersService } = makeContext ( )
@@ -345,7 +473,7 @@ describe('Models - Methods', function () {
345
473
'saved instance has a save method'
346
474
)
347
475
348
- store . commit ( 'letters/updateItem' , {
476
+ store . commit ( 'model-methods- letters/updateItem' , {
349
477
id : letter . id ,
350
478
name : 'Garmadon / Dad' ,
351
479
age : 1026
0 commit comments