Skip to content

Commit

Permalink
test(gantt): test daylight savings in ganttdb
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
aloisklink committed Feb 26, 2023
1 parent 8b5cb75 commit 06640ab
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
34 changes: 34 additions & 0 deletions packages/mermaid/src/diagrams/gantt/ganttDb.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 06640ab

Please sign in to comment.