From 06640aba06c9faab3adfbe0ed047883b419844b0 Mon Sep 17 00:00:00 2001 From: Alois Klink Date: Sat, 25 Feb 2023 20:14:09 +0000 Subject: [PATCH] test(gantt): test daylight savings in ganttdb Add a quick test that ensures `ganttDb` correctly adds `1d` (1 day), even on days with 25 hours. This test only runs if the test PC has the TZ='America/Los_Angeles' set (California has daylight savings). I've added a test to the GitHub Actions `test.yml` action too for this. It should only add about 5 seconds to each test, so it isn't too bad. --- .github/workflows/test.yml | 8 +++++ .../src/diagrams/gantt/ganttDb.spec.ts | 34 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 6397e53051..6c01ba1b98 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -33,6 +33,14 @@ jobs: run: | pnpm run ci --coverage + - name: Run ganttDb tests using California timezone + env: + # Makes sure that gantt db works even in a timezone that has daylight savings + # since some days have 25 hours instead of 24. + TZ: America/Los_Angeles + run: | + pnpm exec vitest run ./packages/mermaid/src/diagrams/gantt/ganttDb.spec.ts + - name: Upload Coverage to Coveralls # it feels a bit weird to use @master, but that's what the docs use # (coveralls also doesn't publish a @v1 we can use) diff --git a/packages/mermaid/src/diagrams/gantt/ganttDb.spec.ts b/packages/mermaid/src/diagrams/gantt/ganttDb.spec.ts index 09df96f12f..d4182b4719 100644 --- a/packages/mermaid/src/diagrams/gantt/ganttDb.spec.ts +++ b/packages/mermaid/src/diagrams/gantt/ganttDb.spec.ts @@ -371,6 +371,40 @@ describe('when using the ganttDb', function () { expect(tasks[1].task).toEqual('test2'); }); + /** + * Unfortunately, Vitest has no way of modifying the timezone at runtime, so + * in order to test this, please run this test with + * + * ```bash + * TZ='America/Los_Angeles' pnpm exec vitest run ganttDb + * ``` + */ + /* c8 ignore start */ // tell code-coverage to ignore this block of code + describe.skipIf(process.env.TZ != 'America/Los_Angeles')( + 'when using a timezone with daylight savings (only run if TZ="America/Los_Angeles")', + () => { + it('should add 1 day even on days with 25 hours', function () { + const startTime = new Date(2020, 10, 1); + expect(startTime.toISOString()).toBe('2020-11-01T07:00:00.000Z'); + + const endTime = new Date(2020, 10, 2); + expect(endTime.toISOString()).toBe('2020-11-02T08:00:00.000Z'); + + ganttDb.setDateFormat('YYYY-MM-DD'); + ganttDb.addSection('Task handles 25 hour day'); + ganttDb.addTask('daylight savings day', 'id1,2020-11-01,1d'); + const tasks = ganttDb.getTasks(); + expect(tasks[0].startTime).toEqual(startTime); + expect(tasks[0].endTime).toEqual(endTime); + + // In USA states that use daylight savings, 2020-11-01 had 25 hours + const millisecondsIn25Hours = 25 * 60 * 60 * 1000; + expect(endTime - startTime).toEqual(millisecondsIn25Hours); + }); + } + ); + /* c8 ignore stop */ + describe('when setting inclusive end dates', function () { beforeEach(function () { ganttDb.setDateFormat('YYYY-MM-DD');