-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
93 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters