Skip to content

Commit

Permalink
test(ids): make sure id is kept in documents
Browse files Browse the repository at this point in the history
Also make sure id is non enumerable, writable, nor configurable
  • Loading branch information
posva committed Dec 24, 2017
1 parent 14a6c55 commit 5b06306
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ function isObject (o) {
}

export function extractRefs (doc, path = '', result = [{}, {}]) {
const idDescriptor = Object.getOwnPropertyDescriptor(doc, 'id')
if (idDescriptor && !idDescriptor.enumerable) {
Object.defineProperty(result[0], 'id', idDescriptor)
}
return Object.keys(doc).reduce((tot, key) => {
const ref = doc[key]
// if it's a ref
Expand Down
13 changes: 13 additions & 0 deletions test/document.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Vuefire from '../src'
import {
db,
tick,
Key,
Vue
} from './helpers'

Expand Down Expand Up @@ -41,3 +42,15 @@ test('updates a document', async () => {
await document.update({ bar: 'bar' })
expect(vm.item).toEqual({ foo: 'foo', bar: 'bar' })
})

test('adds non-enumerable id', async () => {
document = collection.doc(new Key('some-id'))
await document.update({ foo: 'foo' })
await vm.$bind('item', document)
expect(Object.getOwnPropertyDescriptor(vm.item, 'id')).toEqual({
configurable: false,
enumerable: false,
writable: false,
value: 'some-id'
})
})

0 comments on commit 5b06306

Please sign in to comment.