Skip to content

Commit

Permalink
Merge pull request #49 from 21CSM/firebase-segregation
Browse files Browse the repository at this point in the history
Firebase segregation
  • Loading branch information
21CSM authored Sep 24, 2024
2 parents 1ae2f99 + 3cadaea commit 3c9f7ad
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 27 deletions.
39 changes: 39 additions & 0 deletions src/lib/firebase/app.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';

const mockInitializeApp = vi.fn(() => ({ name: 'mocked-app' }));

vi.mock('./config', () => ({
firebaseConfig: {
apiKey: 'mock-api-key',
authDomain: 'mock-auth-domain',
projectId: "mock-project-id",
storageBucket: "mock-storage-bucket",
messagingSenderId: "mock-messaging-sender-id",
appId: "mock-app-id",
measurementId: "mock-measurement-id"
}
}));

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

describe('Firebase app initialization', () => {
beforeEach(() => {
vi.clearAllMocks();
});
it('initializes Firebase with the correct config', async () => {
const { app } = await import('./app');
expect(mockInitializeApp).toHaveBeenCalledTimes(1);
expect(mockInitializeApp).toHaveBeenCalledWith(expect.objectContaining({
apiKey: 'mock-api-key',
authDomain: 'mock-auth-domain',
projectId: "mock-project-id",
storageBucket: "mock-storage-bucket",
messagingSenderId: "mock-messaging-sender-id",
appId: "mock-app-id",
measurementId: "mock-measurement-id"
}));
expect(app).toEqual({ name: 'mocked-app' });
});
});
4 changes: 4 additions & 0 deletions src/lib/firebase/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { initializeApp } from 'firebase/app';
import { firebaseConfig } from './config';

export const app = initializeApp(firebaseConfig);
14 changes: 14 additions & 0 deletions src/lib/firebase/auth.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { describe, it, expect, vi } from 'vitest';
import { getAuth } from 'firebase/auth';

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

describe('Firebase Auth Initialization', () => {
it('should call getAuth and initialize auth', async () => {
const { auth } = await import('./auth');
expect(getAuth).toHaveBeenCalled();
expect(auth).toBeDefined();
});
});
3 changes: 3 additions & 0 deletions src/lib/firebase/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { getAuth } from "firebase/auth";

export const auth = getAuth();
34 changes: 14 additions & 20 deletions src/lib/firebase/config.test.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,28 @@
import { describe, it, expect, vi } from 'vitest';

// Mock Firebase initialization
vi.mock('firebase/app', () => {
const mockApp = {};
return {
initializeApp: vi.fn(() => mockApp)
};
});

// Import after mocking
import { initializeApp } from 'firebase/app';
import { app } from './config';
import { describe, it, expect } from 'vitest';
import { firebaseConfig } from './config';

describe('Firebase Configuration', () => {
it('initializes Firebase with all required fields', () => {
expect(initializeApp).toHaveBeenCalledWith(
it('has all required fields', () => {
expect(firebaseConfig).toEqual(
expect.objectContaining({
apiKey: expect.any(String),
authDomain: expect.any(String),
projectId: expect.any(String),
storageBucket: expect.any(String),
messagingSenderId: expect.any(String),
appId: expect.any(String)
// Remove measurementId if it's not in your config
appId: expect.any(String),
measurementId: expect.any(String)
})
);
});

it('exports the initialized app', () => {
expect(app).toBeDefined();
expect(app).toBe(vi.mocked(initializeApp).mock.results[0].value);
it('has valid configuration values', () => {
expect(firebaseConfig.apiKey).toMatch(/^AIza[0-9A-Za-z-_]{35}$/);
expect(firebaseConfig.authDomain).toBe('ars-antiqua.firebaseapp.com');
expect(firebaseConfig.projectId).toBe('ars-antiqua');
expect(firebaseConfig.storageBucket).toBe('ars-antiqua.appspot.com');
expect(firebaseConfig.messagingSenderId).toMatch(/^\d+$/);
expect(firebaseConfig.appId).toMatch(/^1:\d+:web:[a-f0-9]+$/);
expect(firebaseConfig.measurementId).toMatch(/^G-[A-Z0-9]+$/);
});
});
8 changes: 2 additions & 6 deletions src/lib/firebase/config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { initializeApp } from "firebase/app";

const firebaseConfig = {
export const firebaseConfig = {
apiKey: "AIzaSyBLIu54l_MFzG4G7QO34tUl6LhHUzfsEx4",
authDomain: "ars-antiqua.firebaseapp.com",
projectId: "ars-antiqua",
storageBucket: "ars-antiqua.appspot.com",
messagingSenderId: "42107927354",
appId: "1:42107927354:web:4ce8583ae1a6cb8539ae03",
measurementId: "G-9L07RXZCZD"
};

export const app = initializeApp(firebaseConfig);
};
14 changes: 14 additions & 0 deletions src/lib/firebase/db.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { describe, it, expect, vi } from 'vitest';
import { getFirestore } from 'firebase/firestore';

vi.mock('firebase/firestore', () => ({
getFirestore: vi.fn(() => ({})),
}));

describe('Firestore DB Initialization', () => {
it('should call getFirestore and initialize db', async () => {
const { db } = await import('./db');
expect(getFirestore).toHaveBeenCalled();
expect(db).toBeDefined();
});
});
3 changes: 3 additions & 0 deletions src/lib/firebase/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { getFirestore } from "firebase/firestore";

export const db = getFirestore();
1 change: 1 addition & 0 deletions src/lib/firebase/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { app } from './app';
14 changes: 14 additions & 0 deletions src/lib/firebase/storage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { describe, it, expect, vi } from 'vitest';
import { getStorage } from 'firebase/storage';

vi.mock('firebase/storage', () => ({
getStorage: vi.fn(() => ({})),
}));

describe('Firebase Storage Initialization', () => {
it('should call getStorage and initialize storage', async () => {
const { storage } = await import('./storage');
expect(getStorage).toHaveBeenCalled();
expect(storage).toBeDefined();
});
});
3 changes: 3 additions & 0 deletions src/lib/firebase/storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { getStorage } from "firebase/storage";

export const storage = getStorage();
6 changes: 5 additions & 1 deletion src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
export * from './firebase/config';
export * from './firebase/app';
export * from './firebase/auth';
export * from './firebase/config';
export * from './firebase/db';
export * from './firebase/storage';

0 comments on commit 3c9f7ad

Please sign in to comment.