Skip to content

Commit 13c945b

Browse files
committed
test: verify Model class pending getters
1 parent 1290128 commit 13c945b

File tree

2 files changed

+161
-0
lines changed

2 files changed

+161
-0
lines changed

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

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,4 +352,113 @@ describe('Models - Methods', function () {
352352

353353
assert(json, 'got json')
354354
})
355+
356+
it('Model pending status sets/clears for create/update/patch/remove', async function() {
357+
const { makeServicePlugin, BaseModel } = feathersVuex(feathersClient, {
358+
idField: '_id',
359+
serverAlias: 'model-methods'
360+
})
361+
class PendingThing extends BaseModel {
362+
public static modelName = 'PendingThing'
363+
public constructor(data?, options?) {
364+
super(data, options)
365+
}
366+
}
367+
const store = new Vuex.Store<RootState>({
368+
plugins: [
369+
makeServicePlugin({
370+
Model: PendingThing,
371+
service: feathersClient.service('methods-pending-things')
372+
})
373+
]
374+
})
375+
376+
// Create instance
377+
const thing = new PendingThing({ description: 'pending test' })
378+
const clone = thing.clone()
379+
assert(!!thing.__id, "thing has a tempId")
380+
assert(clone.__id === thing.__id, "clone has thing's tempId")
381+
382+
// Manually set the result in a hook to simulate the server request.
383+
feathersClient.service('methods-pending-things').hooks({
384+
before: {
385+
create: [
386+
context => {
387+
context.result = { _id: 42, ...context.data }
388+
// Check pending status
389+
assert(thing.isCreatePending === true, 'isCreatePending set')
390+
assert(thing.isPending === true, 'isPending set')
391+
// Check clone's pending status
392+
assert(clone.isCreatePending === true, 'isCreatePending set on clone')
393+
assert(clone.isPending === true, 'isPending set on clone')
394+
return context
395+
}
396+
],
397+
update: [
398+
context => {
399+
context.result = { ...context.data }
400+
// Check pending status
401+
assert(thing.isUpdatePending === true, 'isUpdatePending set')
402+
assert(thing.isPending === true, 'isPending set')
403+
// Check clone's pending status
404+
assert(clone.isUpdatePending === true, 'isUpdatePending set on clone')
405+
assert(clone.isPending === true, 'isPending set on clone')
406+
return context
407+
}
408+
],
409+
patch: [
410+
context => {
411+
context.result = { ...context.data }
412+
// Check pending status
413+
assert(thing.isPatchPending === true, 'isPatchPending set')
414+
assert(thing.isPending === true, 'isPending set')
415+
// Check clone's pending status
416+
assert(clone.isPatchPending === true, 'isPatchPending set on clone')
417+
assert(clone.isPending === true, 'isPending set on clone')
418+
return context
419+
}
420+
],
421+
remove: [
422+
context => {
423+
context.result = { ...context.data }
424+
// Check pending status
425+
assert(thing.isRemovePending === true, 'isRemovePending set')
426+
assert(thing.isPending === true, 'isPending set')
427+
// Check clone's pending status
428+
assert(clone.isRemovePending === true, 'isRemovePending set on clone')
429+
assert(clone.isPending === true, 'isPending set on clone')
430+
return context
431+
}
432+
]
433+
}
434+
})
435+
436+
// Create and verify status
437+
await thing.create()
438+
assert(thing.isCreatePending === false, 'isCreatePending cleared')
439+
assert(thing.isPending === false, 'isPending cleared')
440+
assert(clone.isCreatePending === false, 'isCreatePending cleared on clone')
441+
assert(clone.isPending === false, 'isPending cleared on clone')
442+
443+
// Update and verify status
444+
await thing.update()
445+
assert(thing.isUpdatePending === false, 'isUpdatePending cleared')
446+
assert(thing.isPending === false, 'isPending cleared')
447+
assert(clone.isUpdatePending === false, 'isUpdatePending cleared on clone')
448+
assert(clone.isPending === false, 'isPending cleared on clone')
449+
450+
// Patch and verify status
451+
await thing.patch()
452+
assert(thing.isPatchPending === false, 'isPatchPending cleared')
453+
assert(thing.isPending === false, 'isPending cleared')
454+
assert(clone.isPatchPending === false, 'isPatchPending cleared on clone')
455+
assert(clone.isPending === false, 'isPending cleared on clone')
456+
457+
// Remove and verify status
458+
await thing.remove()
459+
assert(thing.isRemovePending === false, 'isRemovePending cleared')
460+
assert(thing.isPending === false, 'isPending cleared')
461+
assert(clone.isRemovePending === false, 'isRemovePending cleared on clone')
462+
assert(clone.isPending === false, 'isPending cleared on clone')
463+
})
355464
})

test/service-module/model-temp-ids.test.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,4 +577,56 @@ describe('Models - Temp Ids', function() {
577577
}
578578
})
579579
})
580+
581+
it('Model pending status updated for tempIds and clones', async function() {
582+
const { makeServicePlugin, BaseModel } = feathersVuex(feathersClient, {
583+
idField: '_id',
584+
serverAlias: 'temp-ids'
585+
})
586+
class PendingThing extends BaseModel {
587+
public static modelName = 'PendingThing'
588+
public constructor(data?, options?) {
589+
super(data, options)
590+
}
591+
}
592+
const store = new Vuex.Store<RootState>({
593+
plugins: [
594+
makeServicePlugin({
595+
Model: PendingThing,
596+
service: feathersClient.service('pending-things')
597+
})
598+
]
599+
})
600+
601+
// Create instance
602+
const thing = new PendingThing({ description: 'PendingThing 1' })
603+
const clone = thing.clone()
604+
assert(!!thing.__id, "thing has a tempId")
605+
assert(clone.__id === thing.__id, "clone has thing's tempId")
606+
607+
// Manually set the result in a hook to simulate the server request.
608+
feathersClient.service('pending-things').hooks({
609+
before: {
610+
create: [
611+
context => {
612+
context.result = { _id: 42, ...context.data }
613+
// Check pending status
614+
assert(thing.isCreatePending === true, 'isCreatePending set')
615+
assert(thing.isPending === true, 'isPending set')
616+
// Check clone's pending status
617+
assert(clone.isCreatePending === true, 'isCreatePending set')
618+
assert(clone.isPending === true, 'isPending set')
619+
return context
620+
}
621+
]
622+
}
623+
})
624+
625+
// Save and verify status
626+
await thing.save()
627+
assert(thing.isCreatePending === false, 'isCreatePending cleared')
628+
assert(thing.isPending === false, 'isPending cleared')
629+
assert(clone.isCreatePending === false, 'isCreatePending cleared')
630+
assert(clone.isPending === false, 'isPending cleared')
631+
})
580632
})

0 commit comments

Comments
 (0)