-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
index.test.ts
178 lines (153 loc) · 5.6 KB
/
index.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import { collection, doc } from 'firebase/firestore'
import type { Firestore } from 'firebase/firestore'
import { computed, effectScope, nextTick, ref } from 'vue-demi'
import { useFirestore } from './index'
const dummyFirestore = {} as Firestore
function getMockSnapFromRef(docRef: any) {
return {
id: `${docRef.path}-id`,
data: () => docRef.path === 'users/invalid' ? null : docRef,
}
}
function getData(docRef: any) {
const data = docRef.data()
if (data) {
Object.defineProperty(data, 'id', {
value: docRef.id.toString(),
writable: false,
})
}
return data
}
const unsubscribe = vi.fn()
console.error = vi.fn()
vi.mock('firebase/firestore', () => {
const doc = vi.fn((_: Firestore, path: string) => {
if (path.includes('//'))
throw new Error('Invalid segment')
return { path }
})
const collection = vi.fn((_: Firestore, path: string) => {
if (path.includes('//'))
throw new Error('Invalid segment')
return { path }
})
const onSnapshot = vi.fn((docRef: any, callbackFn: (payload: any) => {}, errorHandler: (err: Error) => void) => {
if (docRef.path === 'users/error') {
errorHandler(new Error('not found'))
return
}
callbackFn({
...getMockSnapFromRef(docRef),
docs: [getMockSnapFromRef(docRef)],
})
return unsubscribe
})
return { onSnapshot, collection, doc }
})
describe('useFirestore', () => {
beforeEach(() => {
vi.clearAllMocks()
vi.useFakeTimers()
})
afterEach(() => {
vi.clearAllTimers()
})
it('should get `users` collection data', () => {
const collectionRef = collection(dummyFirestore, 'users')
const data = useFirestore(collectionRef)
expect(data.value).toEqual([getData(getMockSnapFromRef(collectionRef))])
})
it('should get `users/userId` document data', () => {
let docRef = doc(dummyFirestore, 'users/userId')
let data = useFirestore(docRef)
expect(data.value).toEqual(getData(getMockSnapFromRef(docRef)))
docRef = doc(dummyFirestore, 'users/invalid')
data = useFirestore(docRef)
expect(data.value).toEqual(getData(getMockSnapFromRef(docRef)))
})
it('should get `posts` computed query data', () => {
const queryRef = collection(dummyFirestore, 'posts')
const data = useFirestore(computed(() => queryRef))
expect(data.value).toEqual([getData(getMockSnapFromRef(queryRef))])
})
it('should get initial value when pass falsy value', () => {
const collectionRef = collection(dummyFirestore, 'todos')
const falsy = computed(() => false as boolean && collectionRef)
const data = useFirestore(falsy, [{ id: 'default' }])
expect(data.value).toEqual([{ id: 'default' }])
})
it('should get reactive query data & unsubscribe previous query when re-querying', async () => {
const queryRef = collection(dummyFirestore, 'posts')
const reactiveQueryRef = ref(queryRef)
const data = useFirestore(reactiveQueryRef)
expect(data.value).toEqual([getData(getMockSnapFromRef(reactiveQueryRef.value))])
reactiveQueryRef.value = collection(dummyFirestore, 'todos')
await nextTick()
expect(unsubscribe).toHaveBeenCalled()
expect(data.value).toEqual([getData(getMockSnapFromRef(reactiveQueryRef.value))])
})
it('should get user data only when user id exists', async () => {
const userId = ref('')
const queryRef = computed(() => !!userId.value && collection(dummyFirestore, `users/${userId.value}/posts`))
const data = useFirestore(queryRef, [{ id: 'default' }])
expect(data.value).toEqual([{ id: 'default' }])
userId.value = 'userId'
await nextTick()
expect(data.value).toEqual([getData(getMockSnapFromRef(collection(dummyFirestore, `users/${userId.value}/posts`)))])
userId.value = ''
await nextTick()
expect(unsubscribe).toHaveBeenCalled()
expect(data.value).toEqual([{ id: 'default' }])
})
it('should call error handler', () => {
const docRef = doc(dummyFirestore, 'users/error')
useFirestore(docRef)
expect(console.error).toHaveBeenCalledWith(new Error('not found'))
})
it('should close when scope dispose', async () => {
const scope = effectScope()
let data: any
const userId = ref('')
const queryRef = computed(() => !!userId.value && collection(dummyFirestore, `users/${userId.value}/posts`))
scope.run(() => {
data = useFirestore(queryRef, [{ id: 'default' }])
expect(data.value).toEqual([{ id: 'default' }])
})
scope.stop()
userId.value = 'userId'
await nextTick()
expect(data.value).toEqual([{ id: 'default' }])
})
it('should get disposed without autoDispose option', async () => {
const scope = effectScope()
await scope.run(async () => {
const collectionRef = collection(dummyFirestore, 'users')
useFirestore(collectionRef)
await nextTick()
})
scope.stop()
expect(unsubscribe).toBeCalledTimes(1)
})
it('should not get disposed with explicit autoDispose option', async () => {
const scope = effectScope()
await scope.run(async () => {
const collectionRef = collection(dummyFirestore, 'users')
useFirestore(collectionRef, undefined, { autoDispose: false })
await nextTick()
})
scope.stop()
expect(unsubscribe).toBeCalledTimes(0)
})
it('should get disposed after autoDispose timeout', async () => {
const scope = effectScope()
await scope.run(async () => {
const collectionRef = collection(dummyFirestore, 'users')
useFirestore(collectionRef, undefined, { autoDispose: 1000 })
})
scope.stop()
expect(unsubscribe).toBeCalledTimes(0)
vi.advanceTimersByTime(2000)
expect(unsubscribe).toBeCalledTimes(1)
})
})