diff --git a/packages/lib-user/src/components/shared/MainContent/helpers/getDateRangeSelectOptions.js b/packages/lib-user/src/components/shared/MainContent/helpers/getDateRangeSelectOptions.js index 376889bda6..7e3afecafd 100644 --- a/packages/lib-user/src/components/shared/MainContent/helpers/getDateRangeSelectOptions.js +++ b/packages/lib-user/src/components/shared/MainContent/helpers/getDateRangeSelectOptions.js @@ -16,7 +16,7 @@ export function getDateRangeSelectOptions(created_at = '2015-07-01') { }, { label: 'THIS MONTH', - value: new Date(endDate.getUTCFullYear(), endDate.getUTCMonth(), 1).toISOString().substring(0, 10) + value: new Date(Date.UTC(endDate.getUTCFullYear(), endDate.getUTCMonth(), 1)).toISOString().substring(0, 10) }, { label: 'LAST 3 MONTHS', @@ -24,11 +24,11 @@ export function getDateRangeSelectOptions(created_at = '2015-07-01') { }, { label: 'THIS YEAR', - value: new Date(endDate.getUTCFullYear(), 0, 1).toISOString().substring(0, 10) + value: new Date(Date.UTC(endDate.getUTCFullYear(), 0, 1)).toISOString().substring(0, 10) }, { label: 'LAST 12 MONTHS', - value: new Date((endDate.getUTCFullYear() - 1), getNextMonth(endDate.getUTCMonth()), 1).toISOString().substring(0, 10) + value: new Date(Date.UTC((endDate.getUTCFullYear() - 1), getNextMonth(endDate.getUTCMonth()), 1)).toISOString().substring(0, 10) }, { label: 'ALL TIME', diff --git a/packages/lib-user/src/components/shared/MainContent/helpers/getDateRangeSelectOptions.spec.js b/packages/lib-user/src/components/shared/MainContent/helpers/getDateRangeSelectOptions.spec.js index 900a7253e8..e8dcdb94fe 100644 --- a/packages/lib-user/src/components/shared/MainContent/helpers/getDateRangeSelectOptions.spec.js +++ b/packages/lib-user/src/components/shared/MainContent/helpers/getDateRangeSelectOptions.spec.js @@ -5,45 +5,101 @@ import { getDateRangeSelectOptions } from './getDateRangeSelectOptions' describe('utils > getDateRangeSelectOptions', function () { let clock - beforeEach(function () { - clock = sinon.useFakeTimers(new Date(2023, 3, 15)) - }) + describe('when the user\'s date is the day after UTC', function () { + + beforeEach(function () { + // Set the user's clock April 16, 1AM, in a timezone 2 hours ahead of UTC, + // so that the UTC date is April 15, 11PM + clock = sinon.useFakeTimers(new Date('2023-04-16T01:00:00+02:00')) + }) + + afterEach(function () { + clock.restore() + }) + + it('should return the expected date range select options in UTC', function () { + const dateRangeSelectOptions = getDateRangeSelectOptions('2015-07-01') - afterEach(function () { - clock.restore() + // the following expected values are based on the UTC date April 15, 11PM, **NOT** the user's date of April 16, 1AM + expect(dateRangeSelectOptions).to.deep.equal([ + { + label: 'LAST 7 DAYS', + value: '2023-04-09' + }, + { + label: 'LAST 30 DAYS', + value: '2023-03-17' + }, + { + label: 'THIS MONTH', + value: '2023-04-01' + }, + { + label: 'LAST 3 MONTHS', + value: '2023-01-15' + }, + { + label: 'THIS YEAR', + value: '2023-01-01' + }, + { + label: 'LAST 12 MONTHS', + value: '2022-05-01' + }, + { + label: 'ALL TIME', + value: '2015-07-01' + } + ]) + }) }) - it('should return the expected date range select options', function () { - const dateRangeSelectOptions = getDateRangeSelectOptions('2015-07-01') - expect(dateRangeSelectOptions).to.deep.equal([ - { - label: 'LAST 7 DAYS', - value: '2023-04-09' - }, - { - label: 'LAST 30 DAYS', - value: '2023-03-17' - }, - { - label: 'THIS MONTH', - value: '2023-04-01' - }, - { - label: 'LAST 3 MONTHS', - value: '2023-01-15' - }, - { - label: 'THIS YEAR', - value: '2023-01-01' - }, - { - label: 'LAST 12 MONTHS', - value: '2022-05-01' - }, - { - label: 'ALL TIME', - value: '2015-07-01' - } - ]) + describe('when the user\'s date is the day before UTC', function () { + + beforeEach(function () { + // Set the user's clock April 14, 11PM, in a timezone 2 hours behind UTC, + // so that the UTC date is April 15, 1AM + clock = sinon.useFakeTimers(new Date('2023-04-14T23:00:00-02:00')) + }) + + afterEach(function () { + clock.restore() + }) + + it('should return the expected date range select options in UTC', function () { + const dateRangeSelectOptions = getDateRangeSelectOptions('2015-07-01') + + // the following expected values are based on the UTC date April 15, 1AM, **NOT** the user's date of April 14, 11PM + expect(dateRangeSelectOptions).to.deep.equal([ + { + label: 'LAST 7 DAYS', + value: '2023-04-09' + }, + { + label: 'LAST 30 DAYS', + value: '2023-03-17' + }, + { + label: 'THIS MONTH', + value: '2023-04-01' + }, + { + label: 'LAST 3 MONTHS', + value: '2023-01-15' + }, + { + label: 'THIS YEAR', + value: '2023-01-01' + }, + { + label: 'LAST 12 MONTHS', + value: '2022-05-01' + }, + { + label: 'ALL TIME', + value: '2015-07-01' + } + ]) + }) }) })