57 lines
2.2 KiB
TypeScript
57 lines
2.2 KiB
TypeScript
import { beforeEach, describe, expect, it, jest } from '@jest/globals';
|
|
|
|
import handler from '~background/messages/extension/updateBadge';
|
|
|
|
describe('background/messages/extension/updateBadge.ts', () => {
|
|
const req = { name: 'extension/updateBadge' as const };
|
|
const res = { send: jest.fn() };
|
|
|
|
beforeEach(() => {
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
it('should set badge text and color when frameId is 0 and tabId exists', async () => {
|
|
const body = { value: 50 };
|
|
const sender = { frameId: 0, tab: { id: 123 } } as chrome.runtime.MessageSender;
|
|
|
|
await handler({ ...req, body, sender }, res);
|
|
|
|
expect(chrome.action.setBadgeBackgroundColor).toHaveBeenCalledWith({ color: '#6b7280' });
|
|
expect(chrome.action.setBadgeText).toHaveBeenCalledWith({ tabId: 123, text: '50' });
|
|
expect(res.send).toHaveBeenCalledWith({ success: true });
|
|
});
|
|
|
|
it('should clear badge text if value is zero', async () => {
|
|
const body = { value: 0 };
|
|
const sender = { frameId: 0, tab: { id: 456 } } as chrome.runtime.MessageSender;
|
|
|
|
await handler({ ...req, body, sender }, res);
|
|
|
|
expect(chrome.action.setBadgeBackgroundColor).toHaveBeenCalledWith({ color: '#6b7280' });
|
|
expect(chrome.action.setBadgeText).toHaveBeenCalledWith({ tabId: 456, text: '' });
|
|
expect(res.send).toHaveBeenCalledWith({ success: true });
|
|
});
|
|
|
|
it('should return success: false if frameId is not 0', async () => {
|
|
const body = { value: 50 };
|
|
const sender = { frameId: 1, tab: { id: 123 } } as chrome.runtime.MessageSender;
|
|
|
|
await handler({ ...req, body, sender }, res);
|
|
|
|
expect(chrome.action.setBadgeBackgroundColor).not.toHaveBeenCalledWith();
|
|
expect(chrome.action.setBadgeText).not.toHaveBeenCalledWith();
|
|
expect(res.send).toHaveBeenCalledWith({ success: false });
|
|
});
|
|
|
|
it('should return success: false if tabId is undefined', async () => {
|
|
const body = { value: 50 };
|
|
const sender = { frameId: 1, tab: undefined } as chrome.runtime.MessageSender;
|
|
|
|
await handler({ ...req, body, sender }, res);
|
|
|
|
expect(chrome.action.setBadgeBackgroundColor).not.toHaveBeenCalledWith();
|
|
expect(chrome.action.setBadgeText).not.toHaveBeenCalledWith();
|
|
expect(res.send).toHaveBeenCalledWith({ success: false });
|
|
});
|
|
});
|