Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(useFirestore): support dependent queries (#2103)
- Loading branch information
Showing
3 changed files
with
124 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,101 @@ | ||
import { ref } from 'vue-demi' | ||
import { onSnapshot } from 'firebase/firestore' | ||
import type { Mock } from 'vitest' | ||
import { collection, doc } from 'firebase/firestore' | ||
import type { Firestore } from 'firebase/firestore' | ||
import { computed, nextTick, ref } from 'vue-demi' | ||
import { useFirestore } from './index' | ||
|
||
vi.mock('firebase/firestore', () => ({ | ||
onSnapshot: vi.fn(), | ||
})) | ||
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(() => { | ||
(onSnapshot as Mock).mockClear() | ||
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 call onSnapshot with document reference', () => { | ||
const docRef = { path: 'users' } as any | ||
useFirestore(docRef) | ||
expect((onSnapshot as Mock).mock.calls[0][0]).toStrictEqual(docRef) | ||
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 call onSnapshot with ref value of document reference', () => { | ||
const docRef = { path: 'posts' } as any | ||
const refOfDocRef = ref(docRef) | ||
useFirestore(refOfDocRef) | ||
expect((onSnapshot as Mock).mock.calls[0][0]).toStrictEqual(docRef) | ||
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' }]) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters