Skip to content

Commit

Permalink
fix: bindFirestoreRef not reacting to getter (#1496)
Browse files Browse the repository at this point in the history
* fix: bindFirestoreRef not reacting to getter

* fix: checking if references typeof is function

* test: list/collection can be bound to getter
  • Loading branch information
ralacerda authored Feb 23, 2024
1 parent 69706b2 commit e459dd1
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/database/useDatabaseRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export function _useDatabaseRef(
}

let stopWatcher = noop
if (isRef(reference)) {
if (isRef(reference) || typeof reference === 'function') {
stopWatcher = watch(reference, bindDatabaseRef)
}
bindDatabaseRef()
Expand Down
2 changes: 1 addition & 1 deletion src/firestore/useFirestoreRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export function _useFirestoreRef(
}

let stopWatcher = noop
if (isRef(docOrCollectionRef)) {
if (isRef(docOrCollectionRef) || typeof docOrCollectionRef === 'function') {
stopWatcher = watch(docOrCollectionRef, bindFirestoreRef)
}

Expand Down
4 changes: 2 additions & 2 deletions src/storage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export function useStorageFileUrl(
}

refresh()
if (isRef(storageRef)) {
if (isRef(storageRef) || typeof storageRef === 'function') {
watch(storageRef, refresh)
}

Expand Down Expand Up @@ -255,7 +255,7 @@ export function useStorageFile(
return Promise.all([refreshUrl(), refreshMetadata()])
}

if (isRef(storageRef)) {
if (isRef(storageRef) || typeof storageRef === 'function') {
watch(storageRef, (storageSource) => {
if (!storageSource) {
if (uploadTask.value) {
Expand Down
27 changes: 27 additions & 0 deletions tests/database/list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down
31 changes: 28 additions & 3 deletions tests/firestore/collection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe(

function factoryQuery<
AppModelType = DocumentData,
DbModelType extends DocumentData = DocumentData
DbModelType extends DocumentData = DocumentData,
>({
options,
ref,
Expand Down Expand Up @@ -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 })
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e459dd1

Please sign in to comment.