Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions services/firebase/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ export const db = getDatabase(app)
export const storage = getStorage(app)
export const auth = getAuth(app)

export function getDataOnce(path) {
return new Promise((resolve) => {
get(child(ref(db), path))
.then((snapshot) => {
if (snapshot)
return resolve(snapshot.val())
resolve(false)
})
.catch((error) => {
console.log(error.message)
resolve()
})
})
export async function getDataOnce(path) {
try {
const snapshot = await get(child(ref(db), path))

if (!snapshot?.exists())
return false

return snapshot.val()
}
catch (error) {
console.error(error?.message ?? error)
throw error
Comment on lines +12 to +23

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Avoid rejecting from getDataOnce without caller handling

The new catch block rethrows Firebase errors (getDataOnce now rejects instead of resolving a falsy value). Existing callers such as store/currencies/actions.ts:initCurrencies and store/lang/actions.js:initDbLang rely on the old behaviour where failures simply returned undefined and the code fell back to defaults or alternate data sources. Because those dispatches are not awaited or wrapped in try/catch, any transient database error will now short‑circuit the actions and surface as unhandled promise rejections, leaving currencies/lang uninitialized. Either revert to resolving a sentinel value or update the call sites to catch and handle the rejection.

Useful? React with 👍 / 👎.

}
}

export function getDataAndWatch(path, callback) {
Expand Down
79 changes: 79 additions & 0 deletions services/firebase/api.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'

const mockRef = vi.fn(() => ({}))
const mockChild = vi.fn(() => ({}))
const mockGet = vi.fn()

vi.mock('~/services/firebase/config', () => ({
config: {},
}))

vi.mock('firebase/app', () => ({
initializeApp: vi.fn(() => ({})),
}))

vi.mock('firebase/database', () => ({
getDatabase: vi.fn(() => ({})),
ref: (...args) => mockRef(...args),
child: (...args) => mockChild(...args),
get: (...args) => mockGet(...args),
off: vi.fn(),
onValue: vi.fn(),
remove: vi.fn(),
set: vi.fn(),
update: vi.fn(),
}))

vi.mock('firebase/auth', () => ({
getAuth: vi.fn(() => ({})),
}))

vi.mock('firebase/storage', () => ({
getStorage: vi.fn(() => ({})),
getDownloadURL: vi.fn(),
getBlob: vi.fn(),
deleteObject: vi.fn(),
ref: vi.fn(),
uploadBytes: vi.fn(),
}))

let getDataOnce

beforeAll(async () => {
({ getDataOnce } = await import('./api'))
})

describe('getDataOnce', () => {
beforeEach(() => {
mockRef.mockClear()
mockChild.mockClear()
mockGet.mockReset()
})

it('resolves the snapshot value when data exists', async () => {
const value = { foo: 'bar' }
mockGet.mockResolvedValue({
exists: () => true,
val: () => value,
})

await expect(getDataOnce('path/to/data')).resolves.toEqual(value)
expect(mockRef).toHaveBeenCalled()
expect(mockChild).toHaveBeenCalled()
})

it('returns false when the snapshot does not exist', async () => {
mockGet.mockResolvedValue({
exists: () => false,
})

await expect(getDataOnce('path/to/missing')).resolves.toBe(false)
})

it('rejects when firebase get throws an error', async () => {
const error = new Error('boom')
mockGet.mockRejectedValue(error)

await expect(getDataOnce('path/to/error')).rejects.toThrow('boom')
})
})