51 lines
1.5 KiB
TypeScript
51 lines
1.5 KiB
TypeScript
import { beforeEach, describe, expect, it, jest } from '@jest/globals';
|
|
import { sendToContentScript } from '@plasmohq/messaging';
|
|
|
|
import onErrorOccurred from '~background/utils/webRequest/onErrorOccurred';
|
|
|
|
describe('background/utils/webRequest/onErrorOcurred.ts', () => {
|
|
beforeEach(() => {
|
|
jest.clearAllMocks(); // Clear previous mocks before each test
|
|
});
|
|
|
|
it('should call sendToContentScript when error is "net::ERR_BLOCKED_BY_CLIENT" and tabId is valid', async () => {
|
|
const details = {
|
|
error: 'net::ERR_BLOCKED_BY_CLIENT',
|
|
tabId: 1,
|
|
} as chrome.webRequest.WebResponseErrorDetails;
|
|
|
|
await onErrorOccurred(details);
|
|
|
|
expect(sendToContentScript).toHaveBeenCalledTimes(1);
|
|
expect(sendToContentScript).toHaveBeenCalledWith({
|
|
body: {
|
|
value: 'net::ERR_BLOCKED_BY_CLIENT',
|
|
},
|
|
name: 'INCREASE_LOG_COUNT',
|
|
tabId: 1,
|
|
});
|
|
});
|
|
|
|
it('should NOT call sendToContentScript when error is not "net::ERR_BLOCKED_BY_CLIENT"', async () => {
|
|
const details = {
|
|
error: 'net::ERR_FAILED',
|
|
tabId: 1,
|
|
} as chrome.webRequest.WebResponseErrorDetails;
|
|
|
|
await onErrorOccurred(details);
|
|
|
|
expect(sendToContentScript).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('should NOT call sendToContentScript when tabId is invalid (less than 0)', async () => {
|
|
const details = {
|
|
error: 'net::ERR_BLOCKED_BY_CLIENT',
|
|
tabId: -1,
|
|
} as chrome.webRequest.WebResponseErrorDetails;
|
|
|
|
await onErrorOccurred(details);
|
|
|
|
expect(sendToContentScript).not.toHaveBeenCalled();
|
|
});
|
|
});
|