2023-03-15 16:00:52 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*/
|
|
|
|
|
2022-02-17 23:52:06 +00:00
|
|
|
import { module, test } from 'qunit';
|
|
|
|
import { setupRenderingTest } from 'ember-qunit';
|
2023-03-22 18:19:11 +00:00
|
|
|
import { render, click } from '@ember/test-helpers';
|
2022-02-17 23:52:06 +00:00
|
|
|
import sinon from 'sinon';
|
|
|
|
import hbs from 'htmlbars-inline-precompile';
|
|
|
|
import calendarDropdown from 'vault/tests/pages/components/calendar-widget';
|
2022-04-20 15:35:57 +00:00
|
|
|
import { ARRAY_OF_MONTHS } from 'core/utils/date-formatters';
|
2023-01-27 02:21:12 +00:00
|
|
|
import { subMonths, subYears } from 'date-fns';
|
2023-03-22 18:19:11 +00:00
|
|
|
import timestamp from 'core/utils/timestamp';
|
2022-02-17 23:52:06 +00:00
|
|
|
|
|
|
|
module('Integration | Component | calendar-widget', function (hooks) {
|
|
|
|
setupRenderingTest(hooks);
|
|
|
|
|
2023-03-22 18:19:11 +00:00
|
|
|
hooks.before(function () {
|
|
|
|
sinon.stub(timestamp, 'now').callsFake(() => new Date('2018-04-03T14:15:30'));
|
|
|
|
});
|
2022-02-17 23:52:06 +00:00
|
|
|
hooks.beforeEach(function () {
|
2023-03-22 18:19:11 +00:00
|
|
|
const CURRENT_DATE = timestamp.now();
|
2023-01-27 02:21:12 +00:00
|
|
|
this.set('currentDate', CURRENT_DATE);
|
|
|
|
this.set('calendarStartDate', subMonths(CURRENT_DATE, 12));
|
|
|
|
this.set('calendarEndDate', CURRENT_DATE);
|
|
|
|
this.set('startTimestamp', subMonths(CURRENT_DATE, 12).toISOString());
|
|
|
|
this.set('endTimestamp', CURRENT_DATE.toISOString());
|
2022-02-17 23:52:06 +00:00
|
|
|
this.set('handleClientActivityQuery', sinon.spy());
|
|
|
|
});
|
2023-03-22 18:19:11 +00:00
|
|
|
hooks.after(function () {
|
|
|
|
timestamp.now.restore();
|
|
|
|
});
|
2022-02-17 23:52:06 +00:00
|
|
|
|
2023-01-27 02:21:12 +00:00
|
|
|
test('it renders and disables correct months when start date is 12 months ago', async function (assert) {
|
|
|
|
assert.expect(14);
|
2022-02-17 23:52:06 +00:00
|
|
|
await render(hbs`
|
|
|
|
<CalendarWidget
|
2023-01-27 02:21:12 +00:00
|
|
|
@startTimestamp={{this.startTimestamp}}
|
|
|
|
@endTimestamp={{this.endTimestamp}}
|
|
|
|
@selectMonth={{this.handleClientActivityQuery}}
|
2022-02-17 23:52:06 +00:00
|
|
|
/>
|
|
|
|
`);
|
2023-03-22 18:19:11 +00:00
|
|
|
assert
|
2023-05-03 01:36:15 +00:00
|
|
|
.dom('[data-test-calendar-widget-trigger]')
|
2023-03-22 18:19:11 +00:00
|
|
|
.hasText(`Apr 2017 - Apr 2018`, 'renders and formats start and end dates');
|
2022-02-17 23:52:06 +00:00
|
|
|
await calendarDropdown.openCalendar();
|
|
|
|
assert.ok(calendarDropdown.showsCalendar, 'renders the calendar component');
|
2023-01-27 02:21:12 +00:00
|
|
|
// assert months in current year are disabled/enabled correctly
|
2023-03-22 18:19:11 +00:00
|
|
|
const enabledMonths = ['January', 'February', 'March', 'April'];
|
|
|
|
ARRAY_OF_MONTHS.forEach(function (month) {
|
|
|
|
if (enabledMonths.includes(month)) {
|
|
|
|
assert
|
|
|
|
.dom(`[data-test-calendar-month="${month}"]`)
|
|
|
|
.doesNotHaveClass('is-readOnly', `${month} is enabled`);
|
2023-01-27 02:21:12 +00:00
|
|
|
} else {
|
2023-03-22 18:19:11 +00:00
|
|
|
assert.dom(`[data-test-calendar-month="${month}"]`).hasClass('is-readOnly', `${month} is read only`);
|
2023-01-27 02:21:12 +00:00
|
|
|
}
|
|
|
|
});
|
2022-02-17 23:52:06 +00:00
|
|
|
});
|
|
|
|
|
2023-01-27 02:21:12 +00:00
|
|
|
test('it renders and disables months before start timestamp', async function (assert) {
|
2022-02-17 23:52:06 +00:00
|
|
|
await render(hbs`
|
|
|
|
<CalendarWidget
|
2023-01-27 02:21:12 +00:00
|
|
|
@startTimestamp={{this.startTimestamp}}
|
|
|
|
@endTimestamp={{this.endTimestamp}}
|
|
|
|
@selectMonth={{this.handleClientActivityQuery}}
|
2022-02-17 23:52:06 +00:00
|
|
|
/>
|
|
|
|
`);
|
|
|
|
|
|
|
|
await calendarDropdown.openCalendar();
|
2023-01-27 02:21:12 +00:00
|
|
|
assert.dom('[data-test-next-year]').isDisabled('Future year is disabled');
|
2022-02-17 23:52:06 +00:00
|
|
|
await calendarDropdown.clickPreviousYear();
|
|
|
|
assert
|
2023-01-27 02:21:12 +00:00
|
|
|
.dom('[data-test-display-year]')
|
|
|
|
.hasText(`${subYears(this.currentDate, 1).getFullYear()}`, 'shows the previous year');
|
|
|
|
assert.dom('[data-test-previous-year]').isDisabled('disables previous year');
|
|
|
|
|
|
|
|
// assert months in previous year are disabled/enabled correctly
|
2023-03-22 18:19:11 +00:00
|
|
|
const disabledMonths = ['January', 'February', 'March'];
|
|
|
|
ARRAY_OF_MONTHS.forEach(function (month) {
|
|
|
|
if (disabledMonths.includes(month)) {
|
|
|
|
assert.dom(`[data-test-calendar-month="${month}"]`).hasClass('is-readOnly', `${month} is read only`);
|
2023-01-27 02:21:12 +00:00
|
|
|
} else {
|
2023-03-22 18:19:11 +00:00
|
|
|
assert
|
|
|
|
.dom(`[data-test-calendar-month="${month}"]`)
|
|
|
|
.doesNotHaveClass('is-readOnly', `${month} is enabled`);
|
2023-01-27 02:21:12 +00:00
|
|
|
}
|
|
|
|
});
|
2022-02-17 23:52:06 +00:00
|
|
|
});
|
|
|
|
|
2023-01-27 02:21:12 +00:00
|
|
|
test('it calls parent callback with correct arg when clicking "Current billing period"', async function (assert) {
|
2022-02-17 23:52:06 +00:00
|
|
|
await render(hbs`
|
|
|
|
<CalendarWidget
|
2023-01-27 02:21:12 +00:00
|
|
|
@startTimestamp={{this.startTimestamp}}
|
|
|
|
@endTimestamp={{this.endTimestamp}}
|
|
|
|
@selectMonth={{this.handleClientActivityQuery}}
|
2022-02-17 23:52:06 +00:00
|
|
|
/>
|
|
|
|
`);
|
2023-01-27 02:21:12 +00:00
|
|
|
await calendarDropdown.menuToggle();
|
|
|
|
await calendarDropdown.clickCurrentBillingPeriod();
|
|
|
|
assert.propEqual(
|
|
|
|
this.handleClientActivityQuery.args[0][0],
|
|
|
|
{ dateType: 'reset' },
|
|
|
|
'it calls parent function with reset dateType'
|
|
|
|
);
|
2022-02-17 23:52:06 +00:00
|
|
|
});
|
|
|
|
|
2023-01-27 02:21:12 +00:00
|
|
|
test('it calls parent callback with correct arg when clicking "Current month"', async function (assert) {
|
2022-02-17 23:52:06 +00:00
|
|
|
await render(hbs`
|
2023-01-27 02:21:12 +00:00
|
|
|
<CalendarWidget
|
|
|
|
@startTimestamp={{this.startTimestamp}}
|
|
|
|
@endTimestamp={{this.endTimestamp}}
|
|
|
|
@selectMonth={{this.handleClientActivityQuery}}
|
|
|
|
/>
|
|
|
|
`);
|
2022-02-17 23:52:06 +00:00
|
|
|
await calendarDropdown.menuToggle();
|
2023-01-27 02:21:12 +00:00
|
|
|
await calendarDropdown.clickCurrentMonth();
|
|
|
|
assert.propEqual(
|
|
|
|
this.handleClientActivityQuery.args[0][0],
|
|
|
|
{ dateType: 'currentMonth' },
|
|
|
|
'it calls parent function with currentMoth dateType'
|
|
|
|
);
|
2022-02-17 23:52:06 +00:00
|
|
|
});
|
|
|
|
|
2023-01-27 02:21:12 +00:00
|
|
|
test('it calls parent callback with correct arg when selecting a month', async function (assert) {
|
2022-02-17 23:52:06 +00:00
|
|
|
await render(hbs`
|
2023-01-27 02:21:12 +00:00
|
|
|
<CalendarWidget
|
|
|
|
@startTimestamp={{this.startTimestamp}}
|
|
|
|
@endTimestamp={{this.endTimestamp}}
|
|
|
|
@selectMonth={{this.handleClientActivityQuery}}
|
|
|
|
/>
|
|
|
|
`);
|
|
|
|
await calendarDropdown.openCalendar();
|
2023-03-22 18:19:11 +00:00
|
|
|
await click(`[data-test-calendar-month="April"`);
|
2023-01-27 02:21:12 +00:00
|
|
|
assert.propEqual(
|
|
|
|
this.handleClientActivityQuery.lastCall.lastArg,
|
|
|
|
{
|
|
|
|
dateType: 'endDate',
|
2023-03-22 18:19:11 +00:00
|
|
|
monthIdx: 3,
|
|
|
|
monthName: 'April',
|
|
|
|
year: 2018,
|
2023-01-27 02:21:12 +00:00
|
|
|
},
|
|
|
|
'it calls parent function with end date (current) month/year'
|
|
|
|
);
|
|
|
|
|
2022-02-17 23:52:06 +00:00
|
|
|
await calendarDropdown.openCalendar();
|
|
|
|
await calendarDropdown.clickPreviousYear();
|
2023-03-22 18:19:11 +00:00
|
|
|
await click(`[data-test-calendar-month="March"]`);
|
2023-01-27 02:21:12 +00:00
|
|
|
assert.propEqual(
|
|
|
|
this.handleClientActivityQuery.lastCall.lastArg,
|
|
|
|
{
|
|
|
|
dateType: 'endDate',
|
2023-03-22 18:19:11 +00:00
|
|
|
monthIdx: 2,
|
|
|
|
monthName: 'March',
|
|
|
|
year: 2017,
|
2023-01-27 02:21:12 +00:00
|
|
|
},
|
2023-03-22 18:19:11 +00:00
|
|
|
'it calls parent function with selected start date month/year'
|
2022-02-17 23:52:06 +00:00
|
|
|
);
|
|
|
|
});
|
2022-06-03 21:22:50 +00:00
|
|
|
|
2023-01-27 02:21:12 +00:00
|
|
|
test('it disables correct months when start date 6 months ago', async function (assert) {
|
2023-03-22 18:19:11 +00:00
|
|
|
this.set('calendarStartDate', subMonths(this.currentDate, 6)); // Nov 3, 2017
|
2023-01-27 02:21:12 +00:00
|
|
|
this.set('startTimestamp', subMonths(this.currentDate, 6).toISOString());
|
2022-06-03 21:22:50 +00:00
|
|
|
await render(hbs`
|
2023-01-27 02:21:12 +00:00
|
|
|
<CalendarWidget
|
|
|
|
@startTimestamp={{this.startTimestamp}}
|
|
|
|
@endTimestamp={{this.endTimestamp}}
|
|
|
|
@selectMonth={{this.handleClientActivityQuery}}
|
|
|
|
/>
|
|
|
|
`);
|
|
|
|
|
2022-06-03 21:22:50 +00:00
|
|
|
await calendarDropdown.openCalendar();
|
2023-01-27 02:21:12 +00:00
|
|
|
assert.dom('[data-test-next-year]').isDisabled('Future year is disabled');
|
|
|
|
|
2023-03-22 18:19:11 +00:00
|
|
|
// Check start year disables correct months
|
|
|
|
await calendarDropdown.clickPreviousYear();
|
2023-01-27 02:21:12 +00:00
|
|
|
assert.dom('[data-test-previous-year]').isDisabled('previous year is disabled');
|
2023-03-22 18:19:11 +00:00
|
|
|
const prevYearEnabled = ['October', 'November', 'December'];
|
|
|
|
ARRAY_OF_MONTHS.forEach(function (month) {
|
|
|
|
if (prevYearEnabled.includes(month)) {
|
|
|
|
assert
|
|
|
|
.dom(`[data-test-calendar-month="${month}"]`)
|
|
|
|
.doesNotHaveClass('is-readOnly', `${month} is enabled`);
|
|
|
|
} else {
|
|
|
|
assert.dom(`[data-test-calendar-month="${month}"]`).hasClass('is-readOnly', `${month} is read only`);
|
2023-01-27 02:21:12 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-03-22 18:19:11 +00:00
|
|
|
// Check end year disables correct months
|
|
|
|
await click('[data-test-next-year]');
|
|
|
|
const currYearEnabled = ['January', 'February', 'March', 'April'];
|
|
|
|
ARRAY_OF_MONTHS.forEach(function (month) {
|
|
|
|
if (currYearEnabled.includes(month)) {
|
|
|
|
assert
|
|
|
|
.dom(`[data-test-calendar-month="${month}"]`)
|
|
|
|
.doesNotHaveClass('is-readOnly', `${month} is enabled`);
|
|
|
|
} else {
|
|
|
|
assert.dom(`[data-test-calendar-month="${month}"]`).hasClass('is-readOnly', `${month} is read only`);
|
|
|
|
}
|
|
|
|
});
|
2023-01-27 02:21:12 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('it disables correct months when start date 36 months ago', async function (assert) {
|
2023-03-22 18:19:11 +00:00
|
|
|
this.set('calendarStartDate', subMonths(this.currentDate, 36)); // April 3 2015
|
2023-01-27 02:21:12 +00:00
|
|
|
this.set('startTimestamp', subMonths(this.currentDate, 36).toISOString());
|
|
|
|
await render(hbs`
|
|
|
|
<CalendarWidget
|
|
|
|
@startTimestamp={{this.startTimestamp}}
|
|
|
|
@endTimestamp={{this.endTimestamp}}
|
|
|
|
@selectMonth={{this.handleClientActivityQuery}}
|
|
|
|
/>
|
|
|
|
`);
|
|
|
|
|
|
|
|
await calendarDropdown.openCalendar();
|
|
|
|
assert.dom('[data-test-next-year]').isDisabled('Future year is disabled');
|
|
|
|
|
2023-03-22 18:19:11 +00:00
|
|
|
for (const year of [2017, 2016, 2015]) {
|
2023-01-27 02:21:12 +00:00
|
|
|
await calendarDropdown.clickPreviousYear();
|
2023-03-22 18:19:11 +00:00
|
|
|
assert.dom('[data-test-display-year]').hasText(year.toString());
|
2023-01-27 02:21:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
assert.dom('[data-test-previous-year]').isDisabled('previous year is disabled');
|
|
|
|
assert.dom('[data-test-next-year]').isEnabled('next year is enabled');
|
|
|
|
|
2023-03-22 18:19:11 +00:00
|
|
|
assert.dom('.calendar-widget .is-readOnly').exists('Some months disabled');
|
|
|
|
|
|
|
|
const disabledMonths = ['January', 'February', 'March'];
|
|
|
|
ARRAY_OF_MONTHS.forEach(function (month) {
|
|
|
|
if (disabledMonths.includes(month)) {
|
|
|
|
assert.dom(`[data-test-calendar-month="${month}"]`).hasClass('is-readOnly', `${month} is read only`);
|
|
|
|
} else {
|
|
|
|
assert
|
|
|
|
.dom(`[data-test-calendar-month="${month}"]`)
|
|
|
|
.doesNotHaveClass('is-readOnly', `${month} is enabled`);
|
2023-01-27 02:21:12 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
await click('[data-test-next-year]');
|
2023-03-22 18:19:11 +00:00
|
|
|
assert.dom('.calendar-widget .is-readOnly').doesNotExist('All months enabled for 2016');
|
2023-01-27 02:21:12 +00:00
|
|
|
await click('[data-test-next-year]');
|
2023-03-22 18:19:11 +00:00
|
|
|
assert.dom('.calendar-widget .is-readOnly').doesNotExist('All months enabled for 2017');
|
2023-01-27 02:21:12 +00:00
|
|
|
await click('[data-test-next-year]');
|
2023-03-22 18:19:11 +00:00
|
|
|
assert.dom('.calendar-widget .is-readOnly').exists('Some months disabled for 2018');
|
|
|
|
|
|
|
|
const enabledMonths = ['January', 'February', 'March', 'April'];
|
|
|
|
ARRAY_OF_MONTHS.forEach(function (month) {
|
|
|
|
if (enabledMonths.includes(month)) {
|
|
|
|
assert
|
|
|
|
.dom(`[data-test-calendar-month="${month}"]`)
|
|
|
|
.doesNotHaveClass('is-readOnly', `${month} is enabled`);
|
|
|
|
} else {
|
|
|
|
assert.dom(`[data-test-calendar-month="${month}"]`).hasClass('is-readOnly', `${month} is read only`);
|
|
|
|
}
|
|
|
|
});
|
2022-06-03 21:22:50 +00:00
|
|
|
});
|
2022-02-17 23:52:06 +00:00
|
|
|
});
|