test: fix unit tests for WebExtension build

This commit is contained in:
xenticore
2025-05-01 18:26:05 -04:00
parent 745087a1cb
commit 91f43295e8
3 changed files with 38 additions and 30 deletions

View File

@@ -57,7 +57,7 @@ describe('prepareExchangeRates', () => {
const rates = await prepareExchangeRates();
expect(rates).toEqual(mockRates);
expect(GM_fetch).not.toHaveBeenCalled();
expect(GM_fetch as jest.Mock).not.toHaveBeenCalled();
});
it('should fetch new rates when they are expired', async () => {
@@ -66,17 +66,18 @@ describe('prepareExchangeRates', () => {
if (key === storage_exchangerates_next) return new Date(Date.now() - 50000).toISOString();
return null;
});
const mockResponse = {
rates: mockRates,
time_next_update_utc: new Date(Date.now() + 100000).toISOString()
};
(GM_fetch as jest.Mock).mockResolvedValue({
ok: true,
json: async () => ({
rates: mockRates,
time_next_update_utc: new Date(Date.now() + 100000).toISOString()
})
json: async () => (mockResponse)
});
(chrome.runtime.sendMessage as jest.Fn).mockImplementation(() => mockResponse);
const rates = await prepareExchangeRates();
expect(rates).toEqual(mockRates);
expect(GM_fetch).toHaveBeenCalled();
expect(setStorageValue).toHaveBeenCalledWith(storage_exchangerates, mockRates);
});
@@ -86,6 +87,7 @@ describe('prepareExchangeRates', () => {
ok: false,
status: 500
} as Response);
(chrome.runtime.sendMessage as jest.Fn).mockImplementation(() => {});
const rates = await prepareExchangeRates();
expect(rates).toBeNull();