97 lines
2.9 KiB
TypeScript
97 lines
2.9 KiB
TypeScript
|
import { beforeEach, describe, expect, it, jest } from '@jest/globals';
|
||
|
|
||
|
import handler from '~background/messages/extension/updateAvailable';
|
||
|
import { API_URL } from '~utils/constants';
|
||
|
|
||
|
describe('background/messages/extension/updateAvailable.ts', () => {
|
||
|
const req = { name: 'extension/updateAvailable' as const };
|
||
|
const res = { send: jest.fn() };
|
||
|
|
||
|
beforeEach(() => {
|
||
|
jest.clearAllMocks();
|
||
|
});
|
||
|
|
||
|
it('should set updateAvailable if next version differs from current version', async () => {
|
||
|
// const current = '1.0.0';
|
||
|
const next = '2.0.0';
|
||
|
|
||
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||
|
// @ts-ignore
|
||
|
global.fetch = jest.fn(() =>
|
||
|
Promise.resolve({
|
||
|
json: () =>
|
||
|
Promise.resolve({
|
||
|
data: next,
|
||
|
success: true,
|
||
|
}),
|
||
|
status: 200,
|
||
|
})
|
||
|
);
|
||
|
|
||
|
await handler(req, res);
|
||
|
|
||
|
expect(global.fetch).toHaveBeenCalledWith(`${API_URL}/version/`);
|
||
|
expect(chrome.storage.local.set).toHaveBeenCalledWith({ updateAvailable: next });
|
||
|
expect(res.send).toHaveBeenCalledWith({ success: true });
|
||
|
});
|
||
|
|
||
|
it('should remove updateAvailable if next version matches current version', async () => {
|
||
|
// const current = '1.0.0';
|
||
|
const next = '1.0.0';
|
||
|
|
||
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||
|
// @ts-ignore
|
||
|
global.fetch = jest.fn(() =>
|
||
|
Promise.resolve({
|
||
|
json: () =>
|
||
|
Promise.resolve({
|
||
|
data: next,
|
||
|
success: true,
|
||
|
}),
|
||
|
status: 200,
|
||
|
})
|
||
|
);
|
||
|
|
||
|
await handler(req, res);
|
||
|
|
||
|
expect(global.fetch).toHaveBeenCalledWith(`${API_URL}/version/`);
|
||
|
expect(chrome.storage.local.remove).toHaveBeenCalledWith('updateAvailable');
|
||
|
expect(res.send).toHaveBeenCalledWith({ success: false });
|
||
|
});
|
||
|
|
||
|
it('should return success: false if server responds with HTTP 429', async () => {
|
||
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||
|
// @ts-ignore
|
||
|
global.fetch = jest.fn(() =>
|
||
|
Promise.resolve({
|
||
|
json: () =>
|
||
|
Promise.resolve({
|
||
|
errors: [],
|
||
|
success: false,
|
||
|
}),
|
||
|
status: 429,
|
||
|
})
|
||
|
);
|
||
|
|
||
|
await handler(req, res);
|
||
|
|
||
|
expect(global.fetch).toHaveBeenCalledWith(`${API_URL}/version/`);
|
||
|
expect(chrome.storage.local.set).not.toHaveBeenCalled();
|
||
|
expect(chrome.storage.local.remove).not.toHaveBeenCalled();
|
||
|
expect(res.send).toHaveBeenCalledWith({ success: false });
|
||
|
});
|
||
|
|
||
|
it('should return success: false if an error occurs during fetch', async () => {
|
||
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||
|
// @ts-ignore
|
||
|
global.fetch = jest.fn(() => Promise.resolve(new Error()));
|
||
|
|
||
|
await handler(req, res);
|
||
|
|
||
|
expect(global.fetch).toHaveBeenCalledWith(`${API_URL}/version/`);
|
||
|
expect(chrome.storage.local.set).not.toHaveBeenCalled();
|
||
|
expect(chrome.storage.local.remove).not.toHaveBeenCalled();
|
||
|
expect(res.send).toHaveBeenCalledWith({ success: false });
|
||
|
});
|
||
|
});
|