Skip to content

Commit

Permalink
lib-user: Add getDateRangeSelectOptions tests before and after UTC (#…
Browse files Browse the repository at this point in the history
…6186)

* Add getDateRangeSelectOptions tests before and after UTC

* Edit UTC

* Refactor tests to reflect user before and after UTC

* Refactor getDateRangeSelectOptions and how it uses UTC
  • Loading branch information
mcbouslog authored Jul 24, 2024
1 parent e299c17 commit 094daf5
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ 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',
value: new Date(new Date().setUTCDate(endDate.getUTCDate() - 90)).toISOString().substring(0, 10)
},
{
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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
])
})
})
})

0 comments on commit 094daf5

Please sign in to comment.