feat: add Firebase client SDK setup
- Install firebase package - Add environment variable configuration (.env.example) - Create firebase.ts module that initializes Firebase app, Firestore, Auth, and Storage - Add comprehensive test coverage for Firebase initialization with mocked Firebase modules - Update .gitignore to exclude local environment files
This commit is contained in:
42
src/lib/firebase.test.ts
Normal file
42
src/lib/firebase.test.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
|
||||
const initializeAppMock = vi.fn(() => ({ name: 'mock-app' }))
|
||||
const getFirestoreMock = vi.fn(() => ({ type: 'mock-firestore' }))
|
||||
const getAuthMock = vi.fn(() => ({ type: 'mock-auth' }))
|
||||
const getStorageMock = vi.fn(() => ({ type: 'mock-storage' }))
|
||||
|
||||
vi.mock('firebase/app', () => ({ initializeApp: initializeAppMock }))
|
||||
vi.mock('firebase/firestore', () => ({ getFirestore: getFirestoreMock }))
|
||||
vi.mock('firebase/auth', () => ({ getAuth: getAuthMock }))
|
||||
vi.mock('firebase/storage', () => ({ getStorage: getStorageMock }))
|
||||
|
||||
describe('firebase client setup', () => {
|
||||
beforeEach(() => {
|
||||
vi.resetModules()
|
||||
initializeAppMock.mockClear()
|
||||
})
|
||||
|
||||
it('initializes the app with config read from Vite env vars and exports db/auth/storage', async () => {
|
||||
vi.stubEnv('VITE_FIREBASE_API_KEY', 'test-api-key')
|
||||
vi.stubEnv('VITE_FIREBASE_AUTH_DOMAIN', 'test.firebaseapp.com')
|
||||
vi.stubEnv('VITE_FIREBASE_PROJECT_ID', 'test-project')
|
||||
vi.stubEnv('VITE_FIREBASE_STORAGE_BUCKET', 'test-project.appspot.com')
|
||||
vi.stubEnv('VITE_FIREBASE_MESSAGING_SENDER_ID', '123')
|
||||
vi.stubEnv('VITE_FIREBASE_APP_ID', 'app-id')
|
||||
|
||||
const { app, db, auth, storage } = await import('./firebase')
|
||||
|
||||
expect(initializeAppMock).toHaveBeenCalledWith({
|
||||
apiKey: 'test-api-key',
|
||||
authDomain: 'test.firebaseapp.com',
|
||||
projectId: 'test-project',
|
||||
storageBucket: 'test-project.appspot.com',
|
||||
messagingSenderId: '123',
|
||||
appId: 'app-id',
|
||||
})
|
||||
expect(app).toEqual({ name: 'mock-app' })
|
||||
expect(db).toEqual({ type: 'mock-firestore' })
|
||||
expect(auth).toEqual({ type: 'mock-auth' })
|
||||
expect(storage).toEqual({ type: 'mock-storage' })
|
||||
})
|
||||
})
|
||||
18
src/lib/firebase.ts
Normal file
18
src/lib/firebase.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { initializeApp } from 'firebase/app'
|
||||
import { getFirestore } from 'firebase/firestore'
|
||||
import { getAuth } from 'firebase/auth'
|
||||
import { getStorage } from 'firebase/storage'
|
||||
|
||||
const firebaseConfig = {
|
||||
apiKey: import.meta.env.VITE_FIREBASE_API_KEY,
|
||||
authDomain: import.meta.env.VITE_FIREBASE_AUTH_DOMAIN,
|
||||
projectId: import.meta.env.VITE_FIREBASE_PROJECT_ID,
|
||||
storageBucket: import.meta.env.VITE_FIREBASE_STORAGE_BUCKET,
|
||||
messagingSenderId: import.meta.env.VITE_FIREBASE_MESSAGING_SENDER_ID,
|
||||
appId: import.meta.env.VITE_FIREBASE_APP_ID,
|
||||
}
|
||||
|
||||
export const app = initializeApp(firebaseConfig)
|
||||
export const db = getFirestore(app)
|
||||
export const auth = getAuth(app)
|
||||
export const storage = getStorage(app)
|
||||
Reference in New Issue
Block a user