Skip to content

Commit

Permalink
fix: unwrap promise $databaseBind()
Browse files Browse the repository at this point in the history
Fix #1275
  • Loading branch information
posva committed Jan 6, 2023
1 parent bf076cb commit 4e6d32f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/database/optionsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export function databasePlugin(
;(this.$firebaseRefs as Mutable<Record<string, DatabaseReference>>)[key] =
source.ref

return promise
return promise.value
}

// handle firebase option
Expand Down
1 change: 1 addition & 0 deletions src/firestore/optionsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ export const firestorePlugin = function firestorePlugin(
this.$firestoreRefs[key] =
// ts
docOrCollectionRef

return promise.value
}

Expand Down
11 changes: 11 additions & 0 deletions tests/database/options.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,24 @@ describe('RTDB: plugin options', () => {
expect(typeof (wrapper.vm as any).$myUnbind).toBe('function')
})

it('returns a promise', async () => {
const serialize = vi.fn(() => ({ id: '2', foo: 'bar' }))
const { vm } = factory({ serialize })
const itemListRef = databaseRef()

const p = vm.$databaseBind('itemList', itemListRef)
expect(p).toBeInstanceOf(Promise)
await expect(p).resolves.toHaveLength(0)
})

it('calls custom serialize function with a ref', async () => {
const serialize = vi.fn(() => ({ id: '2', foo: 'bar' }))
const { vm } = factory({ serialize })

const itemListRef = databaseRef()

const p = vm.$databaseBind('itemList', itemListRef)
await p
await push(itemListRef, { text: 'foo' })

expect(serialize).toHaveBeenCalledTimes(1)
Expand Down
15 changes: 12 additions & 3 deletions tests/firestore/options.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,19 @@ describe('Firestore: Options API', () => {
expect(wrapper.vm.$myUnbind).toBeTypeOf('function')
})

it('returns a promise', async () => {
const { vm } = factory()

const itemListRef = collection()
await addDoc(itemListRef, {})

const p = vm.$firestoreBind('itemList', itemListRef)
expect(p).toBeInstanceOf(Promise)
await expect(p).resolves.toHaveLength(1)
})

it('calls custom serialize function with collection', async () => {
const fromFirestore = vi.fn(() => ({
foo: 'bar',
}))
const fromFirestore = vi.fn(() => ({ foo: 'bar' }))
const wrapper = factory({
converter: {
fromFirestore,
Expand Down

0 comments on commit 4e6d32f

Please sign in to comment.