Skip to content

Commit

Permalink
test: add tests for storage.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
21CSM committed Oct 1, 2024
1 parent ba6610d commit ce3b221
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 9 deletions.
14 changes: 14 additions & 0 deletions src/lib/firebase/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,18 @@ describe('App', () => {
expect(storageInstance).toEqual(mockStorage);
expect(getStorage).toHaveBeenCalledTimes(1);
});

it('should use existing Firebase app if apps already exist', () => {
const existingApp = { name: 'existingApp' };
(getApps as any).mockReturnValueOnce([existingApp]);

App.getInstance();

expect(initializeApp).not.toHaveBeenCalled();
expect(getAuth).toHaveBeenCalledTimes(1);
expect(getStorage).toHaveBeenCalledTimes(1);
// We're not checking the exact argument here, just that they were called
expect(getAuth).toHaveBeenCalled();
expect(getStorage).toHaveBeenCalled();
});
});
87 changes: 78 additions & 9 deletions src/lib/firebase/storage.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,83 @@
import { describe, it, expect, vi } from 'vitest';
import { getStorage } from 'firebase/storage';
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { FirebaseStorageModule } from './storage';
import { ref, getDownloadURL } from 'firebase/storage';

vi.mock('firebase/storage', () => ({
getStorage: vi.fn(() => ({})),
ref: vi.fn(),
getDownloadURL: 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();
vi.mock('./app', () => ({
App: {
getInstance: vi.fn(() => ({
getStorageInstance: vi.fn(() => ({}))
}))
}
}));

global.fetch = vi.fn();

describe('FirebaseStorageModule', () => {
let storageModule: FirebaseStorageModule;

beforeEach(() => {
storageModule = new FirebaseStorageModule();
vi.clearAllMocks();
});

it('should fetch file successfully', async () => {
const mockFileContent = 'Mock file content';
const mockUrl = 'https://example.com/file.txt';
const mockFilePath = 'path/to/file.txt';

(ref as any).mockReturnValueOnce({});
(getDownloadURL as any).mockResolvedValueOnce(mockUrl);
(global.fetch as any).mockResolvedValueOnce({
ok: true,
text: () => Promise.resolve(mockFileContent)
});

const result = await storageModule.fetchFile(mockFilePath);

expect(ref).toHaveBeenCalledWith({}, mockFilePath);
expect(getDownloadURL).toHaveBeenCalledTimes(1);
expect(global.fetch).toHaveBeenCalledWith(mockUrl);
expect(result).toBe(mockFileContent);
});

it('should handle HTTP error during file fetch', async () => {
const mockUrl = 'https://example.com/file.txt';
const mockFilePath = 'path/to/file.txt';

(ref as any).mockReturnValueOnce({});
(getDownloadURL as any).mockResolvedValueOnce(mockUrl);
(global.fetch as any).mockResolvedValueOnce({
ok: false,
status: 404
});

await expect(storageModule.fetchFile(mockFilePath)).rejects.toThrow(
`Failed to fetch file "${mockFilePath}" from the database. Please try again later.`
);

expect(ref).toHaveBeenCalledWith({}, mockFilePath);
expect(getDownloadURL).toHaveBeenCalledTimes(1);
expect(global.fetch).toHaveBeenCalledWith(mockUrl);
});

it('should handle error during getDownloadURL', async () => {
const mockFilePath = 'path/to/file.txt';
const mockError = new Error('Download URL error');

(ref as any).mockReturnValueOnce({});
(getDownloadURL as any).mockRejectedValueOnce(mockError);

await expect(storageModule.fetchFile(mockFilePath)).rejects.toThrow(
`Failed to fetch file "${mockFilePath}" from the database. Please try again later.`
);

expect(ref).toHaveBeenCalledWith({}, mockFilePath);
expect(getDownloadURL).toHaveBeenCalledTimes(1);
expect(global.fetch).not.toHaveBeenCalled();
});
});
});
1 change: 1 addition & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export default defineConfig({
'**/*.svelte',
'**/index.ts',
'**/lib/stores/**', // TODO: Fix coverage for stores
'**/lib/score/verovioWorker.ts', // Worker files are not included in coverage
],
include: ['src/**/*.ts', 'src/**/*.js'],
},
Expand Down

0 comments on commit ce3b221

Please sign in to comment.