unit test dayJSAddWithDSTFixed (#4192)

# What this PR does
unit test dayJSAddWithDSTFixed utility function


## Checklist

- [x] Unit, integration, and e2e (if applicable) tests updated
- [x] Documentation added (or `pr:no public docs` PR label added if not
required)
- [x] Added the relevant release notes label (see labels prefixed w/
`release:`). These labels dictate how your PR will
    show up in the autogenerated release notes.
This commit is contained in:
Dominik Broj 2024-04-09 17:06:11 +02:00 committed by GitHub
parent 45d0390f5e
commit 8ac07aae2c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 100 additions and 9 deletions

View file

@ -1,7 +1,3 @@
// force timezone to UTC to allow tests to work regardless of local timezone
// generally used by snapshots, but can affect specific tests
process.env.TZ = 'UTC';
const esModules = ['@grafana', 'uplot', 'ol', 'd3', 'react-colorful', 'uuid', 'openapi-fetch'].join('|');
module.exports = {

View file

@ -11,9 +11,11 @@
"build:dev": "webpack -c ./webpack.config.ts --env development",
"labels:link": "yarn --cwd ../../gops-labels/frontend link && yarn link \"@grafana/labels\" && yarn --cwd ../../gops-labels/frontend watch",
"labels:unlink": "yarn --cwd ../../gops-labels/frontend unlink",
"test": "jest --verbose",
"test:report": "HTML_REPORT_ENABLED=true jest",
"test:silent": "jest --silent",
"test-utc": "TZ=UTC jest --verbose --testNamePattern '^((?!@london-tz).)*$'",
"test-london-tz": "TZ=Europe/London jest --verbose --testNamePattern '@london-tz'",
"test": "yarn test-utc && yarn test-london-tz",
"test:report": "HTML_REPORT_ENABLED=true yarn test",
"test:silent": "yarn test --silent",
"test:e2e": "yarn playwright test --grep-invert @expensive",
"test:e2e-expensive": "yarn playwright test",
"test:e2e:watch": "yarn test:e2e --ui",

View file

@ -1,6 +1,11 @@
import dayjs from 'dayjs';
import dayjs, { Dayjs } from 'dayjs';
import timezone from 'dayjs/plugin/timezone';
import utc from 'dayjs/plugin/utc';
import { getDateForDatePicker } from './RotationForm.helpers';
import { dayJSAddWithDSTFixed, getDateForDatePicker } from './RotationForm.helpers';
dayjs.extend(timezone);
dayjs.extend(utc);
describe('RotationForm helpers', () => {
describe('getDateForDatePicker()', () => {
@ -21,4 +26,92 @@ describe('RotationForm helpers', () => {
expect(result.toString()).toContain('Tue Apr 30 2024');
});
});
describe('dayJSAddWithDSTFixed() @london-tz', () => {
it(`corrects resulting hour to be the same as in input if start date is before London DST (GMT + 0)
and resulting date is within London DST (GMT + 1)`, () => {
// Base date is out of DST: 20th Mar 3:00 (GMT + 0)
const baseDate = dayjs('2018-03-20 3:00');
// Result is within DST (GMT + 1)
const result = dayJSAddWithDSTFixed({
baseDate,
addParams: [2, 'weeks'],
});
// Check that although DST change happened, hours are the same in UTC
expect(baseDate.utc().hour()).toBe(result.utc().hour());
expect(result.utc().format()).toBe('2018-04-03T03:00:00Z');
});
it(`corrects resulting hour to be the same as in input if start date is within London DST (GMT + 1)
and resulting date is after London DST (GMT + 0)`, () => {
// Base date is within DST: 20th Oct 3:00 (GMT + 1)
const baseDate = dayjs('2018-10-20 3:00');
// Result is out of DST change (GMT + 0)
const result = dayJSAddWithDSTFixed({
baseDate,
addParams: [2, 'weeks'],
});
// Check that although DST change happened, hours are the same in UTC
expect(baseDate.utc().hour()).toBe(result.utc().hour());
expect(result.utc().format()).toBe('2018-11-03T02:00:00Z');
});
it('does nothing with hours if both start date and resulting date are within London DST', () => {
// Base date is within DST: 20th May 3:00 (GMT + 1)
const baseDate = dayjs('2018-5-20 3:00');
[
[24, 'hours'],
[2, 'weeks'],
[1, 'months'],
].forEach((addParams: Parameters<Dayjs['add']>) => {
expect(
dayJSAddWithDSTFixed({
baseDate,
addParams,
})
.utc()
.hour()
).toBe(baseDate.utc().hour());
});
});
it('does nothing with hours if both start date and resulting date are out of London DST', () => {
// Base date is out of DST: 20th Jan 3:00 (GMT + 0)
const baseDate = dayjs('2018-1-20 3:00');
[
[24, 'hours'],
[2, 'weeks'],
[1, 'months'],
].forEach((addParams: Parameters<Dayjs['add']>) => {
expect(
dayJSAddWithDSTFixed({
baseDate,
addParams,
})
.utc()
.hour()
).toBe(baseDate.utc().hour());
});
});
it('adds hours correctly within the same day', () => {
// Base date is out of DST: 20th Jan 3:00 (GMT + 0)
const baseDate = dayjs('2018-1-20 3:00');
expect(
dayJSAddWithDSTFixed({
baseDate,
addParams: [8, 'hours'],
})
.utc()
.hour()
).toBe(baseDate.utc().hour() + 8);
});
});
});