177 lines
5.1 KiB
TypeScript
177 lines
5.1 KiB
TypeScript
import { beforeEach, describe, expect, it, jest } from '@jest/globals';
|
|
|
|
import handler from '~background/messages/domain/config';
|
|
import { API_URL } from '~utils/constants';
|
|
import type { DomainConfig } from '~utils/types';
|
|
|
|
describe('background/messages/domain/config.ts', () => {
|
|
const body = { domain: 'example.com' };
|
|
const req = { name: 'domain/config' as const };
|
|
const res = { send: jest.fn() };
|
|
|
|
beforeEach(() => {
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
it('should return success: false when domain is missing in request', async () => {
|
|
await handler(req, res);
|
|
|
|
expect(res.send).toHaveBeenCalledWith({ success: false });
|
|
});
|
|
|
|
it('should return cached data when issue is still valid', async () => {
|
|
const config: DomainConfig = {
|
|
issue: {
|
|
expiresAt: Date.now() + 8 * 60 * 60 * 1000,
|
|
flags: ['jest'],
|
|
url: 'https://jestjs.io/',
|
|
},
|
|
on: false,
|
|
};
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
// @ts-ignore
|
|
global.fetch = jest.fn(() =>
|
|
Promise.resolve({
|
|
json: () =>
|
|
Promise.resolve({
|
|
data: {
|
|
flags: config.issue?.flags,
|
|
url: config.issue?.url,
|
|
},
|
|
success: true,
|
|
}),
|
|
status: 200,
|
|
})
|
|
);
|
|
|
|
await chrome.storage.local.set({ 'example.com': config });
|
|
await handler({ ...req, body }, res);
|
|
|
|
expect(chrome.storage.local.get).toHaveBeenCalledWith('example.com');
|
|
expect(res.send).toHaveBeenCalledWith({ data: config, success: true });
|
|
expect(global.fetch).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('should fetch new issue data if issue is expired', async () => {
|
|
const previous: DomainConfig = {
|
|
issue: {
|
|
expiresAt: Date.now() - 8 * 60 * 60 * 1000,
|
|
flags: ['jest'],
|
|
url: 'https://jestjs.io/',
|
|
},
|
|
on: false,
|
|
};
|
|
const next: DomainConfig = {
|
|
issue: {
|
|
expiresAt: Date.now() + 8 * 60 * 60 * 1000,
|
|
flags: ['jest'],
|
|
url: 'https://jestjs.io/',
|
|
},
|
|
on: false,
|
|
};
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
// @ts-ignore
|
|
global.fetch = jest.fn(() =>
|
|
Promise.resolve({
|
|
json: () =>
|
|
Promise.resolve({
|
|
data: {
|
|
flags: previous.issue?.flags,
|
|
url: previous.issue?.url,
|
|
},
|
|
success: true,
|
|
}),
|
|
status: 200,
|
|
})
|
|
);
|
|
|
|
await chrome.storage.local.set({ 'example.com': previous });
|
|
await handler({ ...req, body }, res);
|
|
|
|
expect(chrome.storage.local.get).toHaveBeenCalledWith('example.com');
|
|
expect(global.fetch).toHaveBeenCalledWith(`${API_URL}/issues/example.com/`);
|
|
expect(chrome.storage.local.set).toHaveBeenCalledWith({ 'example.com': next });
|
|
expect(res.send).toHaveBeenCalledWith({ data: next, success: true });
|
|
});
|
|
|
|
it('should handle rate-limiting gracefully (HTTP 429)', async () => {
|
|
const previous: DomainConfig = {
|
|
issue: {
|
|
expiresAt: Date.now() - 8 * 60 * 60 * 1000,
|
|
flags: ['jest'],
|
|
url: 'https://jestjs.io/',
|
|
},
|
|
on: false,
|
|
};
|
|
const next: DomainConfig = {
|
|
issue: {
|
|
expiresAt: Date.now() + 8 * 60 * 60 * 1000,
|
|
flags: ['jest'],
|
|
url: 'https://jestjs.io/',
|
|
},
|
|
on: false,
|
|
};
|
|
|
|
// 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 chrome.storage.local.set({ 'example.com': previous });
|
|
await handler({ ...req, body }, res);
|
|
|
|
expect(chrome.storage.local.get).toHaveBeenCalledWith('example.com');
|
|
expect(global.fetch).toHaveBeenCalledWith(`${API_URL}/issues/example.com/`);
|
|
expect(chrome.storage.local.set).not.toHaveBeenCalledWith({ 'example.com': next });
|
|
expect(res.send).toHaveBeenCalledWith({ data: previous, success: true });
|
|
});
|
|
|
|
it('should fallback to a 24-hour expiration if API returns non-success response', async () => {
|
|
const previous: DomainConfig = {
|
|
issue: {
|
|
expiresAt: Date.now() - 8 * 60 * 60 * 1000,
|
|
flags: ['jest'],
|
|
url: 'https://jestjs.io/',
|
|
},
|
|
on: false,
|
|
};
|
|
const next: DomainConfig = {
|
|
issue: {
|
|
expiresAt: Date.now() + 24 * 60 * 60 * 1000,
|
|
},
|
|
on: false,
|
|
};
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
// @ts-ignore
|
|
global.fetch = jest.fn(() =>
|
|
Promise.resolve({
|
|
json: () =>
|
|
Promise.resolve({
|
|
errors: [],
|
|
success: false,
|
|
}),
|
|
status: 200,
|
|
})
|
|
);
|
|
|
|
await chrome.storage.local.set({ 'example.com': previous });
|
|
await handler({ ...req, body }, res);
|
|
|
|
expect(chrome.storage.local.get).toHaveBeenCalledWith('example.com');
|
|
expect(global.fetch).toHaveBeenCalledWith(`${API_URL}/issues/example.com/`);
|
|
expect(chrome.storage.local.set).toHaveBeenCalledWith({ 'example.com': next });
|
|
expect(res.send).toHaveBeenCalledWith({ data: next, success: true });
|
|
});
|
|
});
|