-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
index.test.ts
101 lines (87 loc) · 3.36 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
import { collection, doc } from 'firebase/firestore'
import type { Firestore } from 'firebase/firestore'
import { computed, nextTick, ref } from 'vue-demi'
import { useFirestore } from './index'
const dummyFirestore = {} as Firestore
const getMockSnapFromRef = (docRef: any) => ({
id: `${docRef.path}-id`,
data: () => (docRef),
})
const getData = (docRef: any) => {
const data = docRef.data()
Object.defineProperty(data, 'id', {
value: docRef.id.toString(),
writable: false,
})
return data
}
const unsubscribe = 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) => {}) => {
callbackFn({
...getMockSnapFromRef(docRef),
docs: [getMockSnapFromRef(docRef)],
})
return unsubscribe
})
return { onSnapshot, collection, doc }
})
describe('useFirestore', () => {
beforeEach(() => {
unsubscribe.mockClear()
})
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', () => {
const docRef = doc(dummyFirestore, 'users/userId')
const 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' }])
})
})