-
Notifications
You must be signed in to change notification settings - Fork 2
Pull request for issue #19 #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -69,8 +69,25 @@ describe('popup script', () => { | |
| expect(fetchMock.mock.calls[0][0]).toBe('https://hook.test'); | ||
| }); | ||
|
|
||
| test('uses custom payload when available', async () => { | ||
| const customPayload = '{"message": "Custom message with {{tab.title}}"}'; | ||
| test('uses custom payload with all datetime variables', async () => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟢 The test currently only covers the UTC timezone. To make the test more robust, you should consider mocking the timezone to a non-UTC value (e.g., UTC+2). This will ensure that the local date/time variables are calculated correctly for all users. Here is an example of how you could modify the test: test('uses custom payload with all datetime variables', async () => {
const customPayload = JSON.stringify({
"triggeredAt": "{{triggeredAt}}",
"now.iso": "{{now.iso}}",
"now.date": "{{now.date}}",
"now.time": "{{now.time}}",
"now.unix": "{{now.unix}}",
"now.unix_ms": "{{now.unix_ms}}",
"now.year": "{{now.year}}",
"now.month": "{{now.month}}",
"now.day": "{{now.day}}",
"now.hour": "{{now.hour}}",
"now.minute": "{{now.minute}}",
"now.second": "{{now.second}}",
"now.millisecond": "{{now.millisecond}}",
"now.local.iso": "{{now.local.iso}}",
"now.local.date": "{{now.local.date}}",
"now.local.time": "{{now.local.time}}",
});
const hook = {
id: '1',
label: 'Send',
url: 'https://hook.test',
customPayload: customPayload
};
global.browser.storage.local.get.mockResolvedValue({ webhooks: [hook] });
global.browser.tabs.query.mockResolvedValue([{
id: 1,
url: 'https://example.com',
title: 'Test Page',
status: 'complete'
}]);
// Mock Date and Timezone
const mockDate = new Date('2025-08-07T10:20:30.123Z');
const OriginalDate = global.Date;
global.Date = jest.fn((...args) => {
if (args.length) {
return new OriginalDate(...args);
}
return mockDate;
});
global.Date.now = jest.fn(() => mockDate.getTime());
global.Date.toISOString = jest.fn(() => mockDate.toISOString());
jest.spyOn(Date.prototype, 'getTimezoneOffset').mockReturnValue(-120); // Mock timezone to UTC+2
require('../popup/popup.js');
document.dispatchEvent(new dom.window.Event('DOMContentLoaded'));
await new Promise(setImmediate);
const sendButton = document.getElementById('send-button-1');
sendButton.click();
await new Promise(setImmediate);
expect(fetchMock).toHaveBeenCalled();
const fetchOptions = fetchMock.mock.calls[0][1];
const sentPayload = JSON.parse(fetchOptions.body);
const expectedPayload = {
"triggeredAt": "2025-08-07T10:20:30.123Z",
"now.iso": "2025-08-07T10:20:30.123Z",
"now.date": "2025-08-07",
"now.time": "10:20:30",
"now.unix": Math.floor(mockDate.getTime() / 1000),
"now.unix_ms": mockDate.getTime(),
"now.year": 2025,
"now.month": 8,
"now.day": 7,
"now.hour": 10,
"now.minute": 20,
"now.second": 30,
"now.millisecond": 123,
"now.local.iso": "2025-08-07T12:20:30.123+02:00",
"now.local.date": "2025-08-07",
"now.local.time": "12:20:30"
};
expect(sentPayload).toEqual(expectedPayload);
// Restore Date mock and timezone mock
global.Date = OriginalDate;
jest.restoreAllMocks();
}); |
||
| const customPayload = JSON.stringify({ | ||
| "triggeredAt": "{{triggeredAt}}", | ||
| "now.iso": "{{now.iso}}", | ||
| "now.date": "{{now.date}}", | ||
| "now.time": "{{now.time}}", | ||
| "now.unix": "{{now.unix}}", | ||
| "now.unix_ms": "{{now.unix_ms}}", | ||
| "now.year": "{{now.year}}", | ||
| "now.month": "{{now.month}}", | ||
| "now.day": "{{now.day}}", | ||
| "now.hour": "{{now.hour}}", | ||
| "now.minute": "{{now.minute}}", | ||
| "now.second": "{{now.second}}", | ||
| "now.millisecond": "{{now.millisecond}}", | ||
| "now.local.iso": "{{now.local.iso}}", | ||
| "now.local.date": "{{now.local.date}}", | ||
| "now.local.time": "{{now.local.time}}", | ||
| }); | ||
| const hook = { | ||
| id: '1', | ||
| label: 'Send', | ||
|
|
@@ -91,6 +108,18 @@ describe('popup script', () => { | |
| status: 'complete' | ||
| }]); | ||
|
|
||
| // Mock Date | ||
| const mockDate = new Date('2025-08-07T10:20:30.123Z'); | ||
| const OriginalDate = global.Date; | ||
| global.Date = jest.fn((...args) => { | ||
| if (args.length) { | ||
| return new OriginalDate(...args); | ||
| } | ||
| return mockDate; | ||
| }); | ||
| global.Date.now = jest.fn(() => mockDate.getTime()); | ||
| global.Date.toISOString = jest.fn(() => mockDate.toISOString()); | ||
|
|
||
| require('../popup/popup.js'); | ||
| document.dispatchEvent(new dom.window.Event('DOMContentLoaded')); | ||
| await new Promise(setImmediate); | ||
|
|
@@ -102,10 +131,32 @@ describe('popup script', () => { | |
|
|
||
| expect(fetchMock).toHaveBeenCalled(); | ||
|
|
||
| // Check that the custom payload was used with the placeholder replaced | ||
| const fetchOptions = fetchMock.mock.calls[0][1]; | ||
| const sentPayload = JSON.parse(fetchOptions.body); | ||
| expect(sentPayload).toEqual({ message: 'Custom message with Test Page' }); | ||
|
|
||
| const expectedPayload = { | ||
| "triggeredAt": "2025-08-07T10:20:30.123Z", | ||
| "now.iso": "2025-08-07T10:20:30.123Z", | ||
| "now.date": "2025-08-07", | ||
| "now.time": "10:20:30", | ||
| "now.unix": Math.floor(mockDate.getTime() / 1000), | ||
| "now.unix_ms": mockDate.getTime(), | ||
| "now.year": 2025, | ||
| "now.month": 8, | ||
| "now.day": 7, | ||
| "now.hour": 10, | ||
| "now.minute": 20, | ||
| "now.second": 30, | ||
| "now.millisecond": 123, | ||
| "now.local.iso": "2025-08-07T10:20:30.123+00:00", | ||
| "now.local.date": "2025-08-07", | ||
| "now.local.time": "10:20:30" | ||
| }; | ||
|
|
||
| expect(sentPayload).toEqual(expectedPayload); | ||
|
|
||
| // Restore Date mock | ||
| global.Date = OriginalDate; | ||
| }); | ||
|
|
||
| test('filters webhooks based on urlFilter', async () => { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.