Skip to content

Commit 29e189b

Browse files
committed
tests: add model-methods tests for remove copies
1 parent 9990659 commit 29e189b

File tree

1 file changed

+155
-27
lines changed

1 file changed

+155
-27
lines changed

test/service-module/model-methods.test.ts

Lines changed: 155 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ interface TodoState extends ServiceState {
2323
isTrue: boolean
2424
}
2525
interface RootState {
26-
todos: TodoState
27-
tasks: ServiceState
26+
['model-methods-persons']: ServiceState
27+
['model-methods-todos']: TodoState
28+
['model-methods-tasks']: ServiceState
2829
tests: ServiceState
2930
blah: ServiceState
3031
things: ServiceState
@@ -42,9 +43,9 @@ function makeContext() {
4243
context.result = JSON.parse(JSON.stringify(context.result))
4344
}
4445

45-
feathersClient.use('letters', memory())
46+
feathersClient.use('model-methods-letters', memory())
4647

47-
const lettersService = feathersClient.service('letters')
48+
const lettersService = feathersClient.service('model-methods-letters')
4849

4950
// Setup hooks on letters service to simulate toJSON serialization that occurs
5051
// with a remote API request.
@@ -63,14 +64,14 @@ function makeContext() {
6364

6465
class Task extends BaseModel {
6566
public static modelName = 'Task'
66-
public static servicePath: 'tasks'
67+
public static servicePath: 'model-methods-tasks'
6768
public constructor(data?, options?) {
6869
super(data, options)
6970
}
7071
}
7172
class Todo extends BaseModel {
7273
public static modelName = 'Todo'
73-
public static servicePath: 'todos'
74+
public static servicePath: 'model-methods-todos'
7475
public constructor(data?, options?) {
7576
super(data, options)
7677
}
@@ -81,6 +82,7 @@ function makeContext() {
8182
super(data, options)
8283
}
8384
public static modelName = 'Letter'
85+
public static servicePath = 'model-methods-letters'
8486
public static instanceDefaults(data, { models, store }) {
8587
return {
8688
to: '',
@@ -100,7 +102,7 @@ function makeContext() {
100102

101103
class Person extends BaseModel {
102104
public static modelName = 'Person'
103-
public static servicePath: 'persons'
105+
public static servicePath = 'model-methods-persons'
104106
public constructor(data?, options?) {
105107
super(data, options)
106108
}
@@ -111,26 +113,101 @@ function makeContext() {
111113
plugins: [
112114
makeServicePlugin({
113115
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'
116120
}),
117121
makeServicePlugin({
118122
Model: Todo,
119-
service: feathersClient.service('todos')
123+
servicePath: 'model-methods-todos',
124+
service: feathersClient.service('model-methods-todos'),
125+
namespace: 'model-methods-todos'
120126
}),
121127
makeServicePlugin({
122128
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'
125132
}),
126133
makeServicePlugin({
127134
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'
131139
})
132140
]
133141
})
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+
134211
return {
135212
BaseModel,
136213
Task,
@@ -295,43 +372,94 @@ describe('Models - Methods', function () {
295372

296373
task.remove()
297374

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+
298386
// @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')
300396
})
301397

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 () {
303413
const { Person, store } = makeContext()
304414
const person = new Person({ test: true })
305415
const tempId = person.__id
306416

417+
// @ts-ignore
418+
const { copiesById } = store.state['model-methods-persons']
419+
307420
person.clone()
308421

309-
// @ts-ignore
310-
assert(store.state.persons.copiesById[tempId], 'clone exists')
422+
assert(copiesById[tempId], 'clone exists')
311423

312424
person.remove()
313425

314-
// @ts-ignore
315-
assert(!store.state.persons.copiesById[tempId], 'clone was removed')
426+
assert(!copiesById[tempId], 'clone was removed')
316427
})
317428

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()
320431
const task = new Task({ test: true })
321432
const tempId = task.__id
322433

323434
task.clone()
324435

325-
// @ts-ignore
326436
assert(Task.copiesById[tempId], 'clone exists')
327437

328438
task.remove()
329439

330-
// @ts-ignore
331440
assert(!Task.copiesById[tempId], 'clone was removed')
332441
})
333442

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+
})
335463

336464
it('instance methods still available in store data after updateItem mutation (or socket event)', async function () {
337465
const { Letter, store, lettersService } = makeContext()
@@ -345,7 +473,7 @@ describe('Models - Methods', function () {
345473
'saved instance has a save method'
346474
)
347475

348-
store.commit('letters/updateItem', {
476+
store.commit('model-methods-letters/updateItem', {
349477
id: letter.id,
350478
name: 'Garmadon / Dad',
351479
age: 1026

0 commit comments

Comments
 (0)