From 8ac07aae2cd572552b0b9937e145c67808b426e2 Mon Sep 17 00:00:00 2001 From: Dominik Broj Date: Tue, 9 Apr 2024 17:06:11 +0200 Subject: [PATCH] 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. --- grafana-plugin/jest.config.js | 4 - grafana-plugin/package.json | 8 +- .../RotationForm/RotationForm.helpers.test.ts | 97 ++++++++++++++++++- 3 files changed, 100 insertions(+), 9 deletions(-) diff --git a/grafana-plugin/jest.config.js b/grafana-plugin/jest.config.js index 2c261cb4..ba7f8e43 100644 --- a/grafana-plugin/jest.config.js +++ b/grafana-plugin/jest.config.js @@ -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 = { diff --git a/grafana-plugin/package.json b/grafana-plugin/package.json index cfe84c23..1beff700 100644 --- a/grafana-plugin/package.json +++ b/grafana-plugin/package.json @@ -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", diff --git a/grafana-plugin/src/containers/RotationForm/RotationForm.helpers.test.ts b/grafana-plugin/src/containers/RotationForm/RotationForm.helpers.test.ts index 05474824..6814710f 100644 --- a/grafana-plugin/src/containers/RotationForm/RotationForm.helpers.test.ts +++ b/grafana-plugin/src/containers/RotationForm/RotationForm.helpers.test.ts @@ -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) => { + 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) => { + 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); + }); + }); });