From 0f447fe5ee6a2675e4993e6e20e8dc4e20ce2909 Mon Sep 17 00:00:00 2001 From: Renato Lacerda Date: Thu, 22 Feb 2024 19:29:50 -0300 Subject: [PATCH] test: list/collection can be bound to getter --- tests/database/list.spec.ts | 27 ++++++++++++++++++++++++++ tests/firestore/collection.spec.ts | 31 +++++++++++++++++++++++++++--- 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/tests/database/list.spec.ts b/tests/database/list.spec.ts index f981c1af..da5ded6e 100644 --- a/tests/database/list.spec.ts +++ b/tests/database/list.spec.ts @@ -287,6 +287,33 @@ describe('Database lists', () => { expect(data.value).toContainEqual({ text: 'task 3' }) }) + it('can be bound to a getter', async () => { + const listA = databaseRef() + const listB = databaseRef() + await push(listA, { text: 'task 1' }) + await push(listA, { text: 'task 2' }) + await push(listB, { text: 'task 3' }) + await push(listA, { text: 'task 4' }) + const showFinished = ref(true) + + const { wrapper, data, promise } = factory({ + ref: () => (showFinished.value ? listA : listB), + }) + + await promise.value + expect(data.value).toHaveLength(3) + expect(data.value).toContainEqual({ text: 'task 1' }) + expect(data.value).toContainEqual({ text: 'task 2' }) + expect(data.value).toContainEqual({ text: 'task 4' }) + + showFinished.value = false + await nextTick() + await promise.value + await nextTick() + expect(data.value).toHaveLength(1) + expect(data.value).toContainEqual({ text: 'task 3' }) + }) + it('reorders items in the array', async () => { const listRef = databaseRef() const orderedListRef = query(listRef, orderByChild('n')) diff --git a/tests/firestore/collection.spec.ts b/tests/firestore/collection.spec.ts index c0927b0b..a1ec8292 100644 --- a/tests/firestore/collection.spec.ts +++ b/tests/firestore/collection.spec.ts @@ -74,7 +74,7 @@ describe( function factoryQuery< AppModelType = DocumentData, - DbModelType extends DocumentData = DocumentData + DbModelType extends DocumentData = DocumentData, >({ options, ref, @@ -358,8 +358,8 @@ describe( showFinished.value ? finishedListRef : showFinished.value === false - ? unfinishedListRef - : null + ? unfinishedListRef + : null ) await addDoc(listRef, { text: 'task 1', finished: false }) await addDoc(listRef, { text: 'task 2', finished: false }) @@ -398,6 +398,31 @@ describe( expect(data.value).toContainEqual({ text: 'task 3', finished: true }) }) + it('can be bound to a getter', async () => { + const listCollectionRef = collection<{ name: string }>( + 'populatedCollection' + ) + const collectionName = ref('populatedCollection') + + const { wrapper, promise } = factory({ + ref: () => collection(collectionName.value), + }) + + await promise.value + await addDoc(listCollectionRef, { name: 'a' }) + await addDoc(listCollectionRef, { name: 'b' }) + + expect(wrapper.vm.list).toHaveLength(2) + expect(wrapper.vm.list).toContainEqual({ name: 'a' }) + expect(wrapper.vm.list).toContainEqual({ name: 'b' }) + + collectionName.value = 'emptyCollection' + await nextTick() + await promise.value + await nextTick() + expect(wrapper.vm.list).toHaveLength(0) + }) + it('can be bound to a null ref', async () => { const { showFinished, listToDisplay } = await createFilteredLists() showFinished.value = null