Skip to content

Commit 9abcbf3

Browse files
committed
test: document firestore
1 parent f94e262 commit 9abcbf3

File tree

1 file changed

+35
-32
lines changed

1 file changed

+35
-32
lines changed

__tests__/core/firestore/document.spec.ts

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import { bindDocument } from '../../../src/core'
22
import { db, spyUnbind, createOps } from '../../src'
33
import { firestore } from 'firebase'
44
import { OperationsType } from '../../../src/shared'
5+
import { ref, Ref } from 'vue'
56

67
describe('documents', () => {
78
let collection: firestore.CollectionReference,
89
document: firestore.DocumentReference,
9-
vm: Record<string, any>,
10+
target: Ref<Record<string, any>>,
1011
resolve: (data: any) => void,
1112
reject: (error: any) => void,
1213
ops: OperationsType
@@ -17,49 +18,50 @@ describe('documents', () => {
1718
// @ts-ignore
1819
document = collection.doc()
1920
ops = createOps()
20-
vm = {}
21+
target = ref({})
2122
await new Promise((res, rej) => {
2223
resolve = jest.fn(res)
2324
reject = jest.fn(rej)
24-
bindDocument({ vm, key: 'item', document, resolve, reject, ops })
25+
bindDocument({ target, document, resolve, reject, ops })
2526
})
2627
})
2728

2829
it('does not call anything if document does not exist', () => {
2930
expect(ops.add).not.toHaveBeenCalled()
3031
expect(ops.set).toHaveBeenCalled()
31-
expect(ops.set).toHaveBeenCalledWith(vm, 'item', null)
32+
expect(ops.set).toHaveBeenCalledWith(target, 'value', null)
3233
expect(ops.remove).not.toHaveBeenCalled()
3334
expect(reject).not.toHaveBeenCalled()
3435
})
3536

3637
it('binding to a non-existant document sets the property to null', async () => {
37-
vm.item = 'foo'
38+
// @ts-ignore
39+
target.value = 'foo'
3840
await new Promise((res, rej) => {
3941
resolve = jest.fn(res)
4042
reject = jest.fn(rej)
4143
bindDocument({
42-
vm,
43-
key: 'item',
44+
target,
45+
4446
document: collection.doc(),
4547
resolve,
4648
reject,
4749
ops,
4850
})
4951
})
50-
expect(vm.item).toBe(null)
52+
expect(target.value).toBe(null)
5153
expect(resolve).toHaveBeenCalledWith(null)
5254
})
5355

5456
it('updates a document', async () => {
5557
await document.update({ foo: 'foo' })
5658
expect(ops.add).not.toHaveBeenCalled()
5759
expect(ops.set).toHaveBeenCalledTimes(2)
58-
expect(ops.set).toHaveBeenLastCalledWith(vm, 'item', { foo: 'foo' })
60+
expect(ops.set).toHaveBeenLastCalledWith(target, 'value', { foo: 'foo' })
5961
expect(ops.remove).not.toHaveBeenCalled()
6062
await document.update({ bar: 'bar' })
6163
expect(ops.set).toHaveBeenCalledTimes(3)
62-
expect(ops.set).toHaveBeenLastCalledWith(vm, 'item', {
64+
expect(ops.set).toHaveBeenLastCalledWith(target, 'value', {
6365
bar: 'bar',
6466
foo: 'foo',
6567
})
@@ -68,14 +70,14 @@ describe('documents', () => {
6870
it('sets to null when deleted', async () => {
6971
await document.update({ foo: 'foo' })
7072
await document.delete()
71-
expect(vm.item).toBe(null)
73+
expect(target.value).toBe(null)
7274
})
7375

7476
it('adds non-enumerable id', async () => {
7577
document = collection.doc('some-id')
76-
bindDocument({ vm, document, key: 'item', resolve, reject, ops })
78+
bindDocument({ target, document, resolve, reject, ops })
7779
await document.update({ foo: 'foo' })
78-
expect(Object.getOwnPropertyDescriptor(vm.item, 'id')).toEqual({
80+
expect(Object.getOwnPropertyDescriptor(target.value, 'id')).toEqual({
7981
configurable: false,
8082
enumerable: false,
8183
writable: false,
@@ -91,18 +93,19 @@ describe('documents', () => {
9193
throw new Error('Promise was not called')
9294
}
9395
await new Promise((resolve, reject) => {
94-
unbind = bindDocument({ vm, document, key: 'item', resolve, reject, ops })
96+
unbind = bindDocument({ target, document, resolve, reject, ops })
9597
})
9698

9799
expect(unbindSpy).not.toHaveBeenCalled()
98-
expect(vm.item).toEqual({ foo: 'foo' })
100+
expect(target.value).toEqual({ foo: 'foo' })
99101
unbind()
100102
expect(unbindSpy).toHaveBeenCalled()
101103

102104
// reset data manually
103-
vm.item = null
105+
// @ts-ignore
106+
target.value = null
104107
await document.update({ foo: 'foo' })
105-
expect(vm.item).toEqual(null)
108+
expect(target.value).toEqual(null)
106109
unbindSpy.mockRestore()
107110
})
108111

@@ -115,7 +118,7 @@ describe('documents', () => {
115118
document.onSnapshot = jest.fn(fakeOnSnapshot)
116119
await expect(
117120
new Promise((resolve, reject) => {
118-
bindDocument({ vm, document, key: 'item', resolve, reject, ops })
121+
bindDocument({ target, document, resolve, reject, ops })
119122
})
120123
).rejects.toThrow()
121124
// @ts-ignore
@@ -125,10 +128,10 @@ describe('documents', () => {
125128
it('resolves when the document is set', async () => {
126129
await document.update({ foo: 'foo' })
127130
const promise = new Promise((resolve, reject) => {
128-
bindDocument({ vm, document, key: 'item', resolve, reject, ops })
131+
bindDocument({ target, document, resolve, reject, ops })
129132
})
130133
await promise
131-
expect(vm.item).toEqual({ foo: 'foo' })
134+
expect(target.value).toEqual({ foo: 'foo' })
132135
})
133136

134137
it('resets the value when unbinding', async () => {
@@ -137,12 +140,12 @@ describe('documents', () => {
137140
throw new Error('Promise was not called')
138141
}
139142
const promise = new Promise((resolve, reject) => {
140-
unbind = bindDocument({ vm, document, key: 'item', resolve, reject, ops })
143+
unbind = bindDocument({ target, document, resolve, reject, ops })
141144
})
142145
await promise
143-
expect(vm.item).toEqual({ foo: 'foo' })
146+
expect(target.value).toEqual({ foo: 'foo' })
144147
unbind()
145-
expect(vm.item).toEqual(null)
148+
expect(target.value).toEqual(null)
146149
})
147150

148151
it('can be left as is with reset: false', async () => {
@@ -151,12 +154,12 @@ describe('documents', () => {
151154
throw new Error('Promise was not called')
152155
}
153156
const promise = new Promise((resolve, reject) => {
154-
unbind = bindDocument({ vm, document, key: 'item', resolve, reject, ops })
157+
unbind = bindDocument({ target, document, resolve, reject, ops })
155158
})
156159
await promise
157-
expect(vm.item).toEqual({ foo: 'foo' })
160+
expect(target.value).toEqual({ foo: 'foo' })
158161
unbind(false)
159-
expect(vm.item).toEqual({ foo: 'foo' })
162+
expect(target.value).toEqual({ foo: 'foo' })
160163
})
161164

162165
it('can be reset to a specific value', async () => {
@@ -165,12 +168,12 @@ describe('documents', () => {
165168
throw new Error('Promise was not called')
166169
}
167170
const promise = new Promise((resolve, reject) => {
168-
unbind = bindDocument({ vm, document, key: 'item', resolve, reject, ops })
171+
unbind = bindDocument({ target, document, resolve, reject, ops })
169172
})
170173
await promise
171-
expect(vm.item).toEqual({ foo: 'foo' })
174+
expect(target.value).toEqual({ foo: 'foo' })
172175
unbind(() => ({ bar: 'bar' }))
173-
expect(vm.item).toEqual({ bar: 'bar' })
176+
expect(target.value).toEqual({ bar: 'bar' })
174177
})
175178

176179
it('ignores reset option in bind when calling unbind', async () => {
@@ -180,13 +183,13 @@ describe('documents', () => {
180183
}
181184
const promise = new Promise((resolve, reject) => {
182185
unbind = bindDocument(
183-
{ vm, document, key: 'item', resolve, reject, ops },
186+
{ target, document, resolve, reject, ops },
184187
{ reset: false }
185188
)
186189
})
187190
await promise
188-
expect(vm.item).toEqual({ foo: 'foo' })
191+
expect(target.value).toEqual({ foo: 'foo' })
189192
unbind()
190-
expect(vm.item).toEqual(null)
193+
expect(target.value).toEqual(null)
191194
})
192195
})

0 commit comments

Comments
 (0)