Skip to content

Commit

Permalink
test: add tests for isInCurrentMonth
Browse files Browse the repository at this point in the history
  • Loading branch information
motss committed Sep 17, 2021
1 parent e0c70d5 commit dc95435
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
10 changes: 5 additions & 5 deletions src/date-picker/date-picker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { adjustOutOfRangeValue } from '../helpers/adjust-out-of-range-value.js';
import { dateValidator } from '../helpers/date-validator.js';
import { dispatchCustomEvent } from '../helpers/dispatch-custom-event.js';
import { focusElement } from '../helpers/focus-element.js';
import { isInTargetMonth } from '../helpers/is-in-current-month.js';
import { isInCurrentMonth } from '../helpers/is-in-current-month.js';
import { splitString } from '../helpers/split-string.js';
import { toDateString } from '../helpers/to-date-string.js';
import { toFormatters } from '../helpers/to-formatters.js';
Expand Down Expand Up @@ -190,8 +190,8 @@ export class DatePicker extends DatePickerMixin(DatePickerMinMaxMixin(LitElement
if (changedProperties.has('_currentDate') && this.#shouldUpdateFocusInNavigationButtons) {
const currentDate = this._currentDate;

isInTargetMonth(this._min, currentDate) && focusElement(this._navigationNext);
isInTargetMonth(this._max, currentDate) && focusElement(this._navigationPrevious);
isInCurrentMonth(this._min, currentDate) && focusElement(this._navigationNext);
isInCurrentMonth(this._max, currentDate) && focusElement(this._navigationPrevious);

this.#shouldUpdateFocusInNavigationButtons = false;
}
Expand Down Expand Up @@ -234,8 +234,8 @@ export class DatePicker extends DatePickerMixin(DatePickerMinMaxMixin(LitElement
nothing :
html`
<div class=month-pagination>
${this.#renderNavigationButton('previous', !isInTargetMonth(min, currentDate))}
${this.#renderNavigationButton('next', !isInTargetMonth(max, currentDate))}
${this.#renderNavigationButton('previous', !isInCurrentMonth(min, currentDate))}
${this.#renderNavigationButton('next', !isInCurrentMonth(max, currentDate))}
</div>
`
}
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/is-in-current-month.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function isInTargetMonth(targetDate: Date, sourceDate: Date): boolean {
export function isInCurrentMonth(targetDate: Date, sourceDate: Date): boolean {
const targetDateFy = targetDate.getUTCFullYear();
const targetDateM = targetDate.getUTCMonth();
const sourceDateFY = sourceDate.getUTCFullYear();
Expand Down
4 changes: 2 additions & 2 deletions src/month-calendar/month-calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { navigationKeySetGrid } from '../constants.js';
import { computeNextSelectedDate } from '../helpers/compute-next-selected-date.js';
import { dispatchCustomEvent } from '../helpers/dispatch-custom-event.js';
import { focusElement } from '../helpers/focus-element.js';
import { isInTargetMonth } from '../helpers/is-in-current-month.js';
import { isInCurrentMonth } from '../helpers/is-in-current-month.js';
import { toClosestTarget } from '../helpers/to-closest-target.js';
import { toResolvedDate } from '../helpers/to-resolved-date.js';
import { keyHome } from '../key-values.js';
Expand Down Expand Up @@ -106,7 +106,7 @@ export class MonthCalendar extends LitElement implements MonthCalendarProperties
* When there is a selected date in the current month, tab to focus on selected date.
* Otherwise, set the first day of month tabbable so that tapping on Tab focuses that.
*/
const tabbableDate = isInTargetMonth(date, currentDate) ?
const tabbableDate = isInCurrentMonth(date, currentDate) ?
date :
computeNextSelectedDate({
currentDate,
Expand Down
34 changes: 34 additions & 0 deletions src/tests/helpers/is-in-current-month.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { expect } from '@open-wc/testing';

import { isInCurrentMonth } from '../../helpers/is-in-current-month';
import { messageFormatter } from '../test-utils/message-formatter';

type A = [Date, Date, boolean];

const cases: A[] = [
[
new Date('2020-02-02'),
new Date('2020-02-12'),
true,
],
[
new Date('2020-02-02'),
new Date('2020-03-12'),
false,
],
];

describe(isInCurrentMonth.name, () => {
cases.forEach(a => {
it(
messageFormatter('returns if %s is current month of %s', a),
() => {
const [testTarget, testSource, expected] = a;

const result = isInCurrentMonth(testTarget, testSource);

expect(result).equal(expected);
}
);
});
});

0 comments on commit dc95435

Please sign in to comment.