Skip to content

Commit cca1c19

Browse files
committed
find getter: add test to verify duplicate IDs problem
1 parent 52bd112 commit cca1c19

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import feathersVuex from '../../src/index'
99
import { feathersRestClient as feathersClient } from '../fixtures/feathers-client'
1010
import { clearModels } from '../../src/service-module/global-models'
1111
import { Service as MemoryService } from 'feathers-memory'
12+
import Vue from 'vue/dist/vue'
1213
import Vuex from 'vuex'
1314
import { makeStore } from '../test-utils'
1415
import ObjectID from 'bson-objectid'
@@ -477,4 +478,68 @@ describe('Models - Temp Ids', function() {
477478
assert(response2 === thing, 'response2 is still thing')
478479
assert(thing.description === 'Thing 3', "thing got clone's new changes")
479480
})
481+
482+
it('find() getter does not return duplicates with temps: true', async function() {
483+
const { makeServicePlugin, BaseModel } = feathersVuex(feathersClient, {
484+
idField: '_id',
485+
serverAlias: 'temp-ids'
486+
})
487+
class FooModel extends BaseModel {
488+
public static modelName = 'FooModel'
489+
public constructor(data?, options?) {
490+
super(data, options)
491+
}
492+
}
493+
const store = new Vuex.Store<RootState>({
494+
plugins: [
495+
makeServicePlugin({
496+
Model: FooModel,
497+
service: feathersClient.service('foos'),
498+
servicePath: 'foos'
499+
})
500+
]
501+
})
502+
503+
// Fake server call
504+
feathersClient.service('foos').hooks({
505+
before: {
506+
create: [
507+
context => {
508+
context.result = { _id: 24, ...context.data }
509+
return context
510+
}
511+
]
512+
}
513+
})
514+
515+
// Create component with find() computed prop
516+
const watchEvents = []
517+
new Vue({
518+
template: `<div></div>`,
519+
computed: {
520+
things() {
521+
return store.getters['foos/find']({
522+
query: { test: true },
523+
temps: true
524+
}).data
525+
}
526+
},
527+
watch: {
528+
things(items) {
529+
watchEvents.push(items)
530+
}
531+
}
532+
}).$mount()
533+
534+
const item = new FooModel({ test: true })
535+
await item.save()
536+
537+
assert(watchEvents.length > 0, 'watch fired at least once')
538+
watchEvents.forEach(items => {
539+
if (items.length === 2) {
540+
assert(items[0]._id !== items[1]._id, 'no duplicate id')
541+
assert(items[0].__id !== items[1].__id, 'no duplicate tempId')
542+
}
543+
})
544+
})
480545
})

0 commit comments

Comments
 (0)