Conversation
There was a problem hiding this comment.
📋 Review Summary
This pull request introduces new date and time variables for the custom payload and refactors the placeholder replacement logic to be more robust by parsing the JSON before replacement. The changes are well-implemented, and the new variables are a great addition.
🔍 General Feedback
- The refactoring of the payload processing is a significant improvement in terms of robustness.
- The new date/time variables are comprehensive.
- The tests have been updated to cover the new functionality.
I have provided a few suggestions to improve code readability and test robustness. Overall, this is a great pull request.
|
|
||
| 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.
🟢 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();
});Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Fixes #19