diff --git a/src/__tests__/year-grid/app-year-grid.test.ts b/src/__tests__/year-grid/app-year-grid.test.ts
index 670e4bed..48b70de0 100644
--- a/src/__tests__/year-grid/app-year-grid.test.ts
+++ b/src/__tests__/year-grid/app-year-grid.test.ts
@@ -1,16 +1,18 @@
import '../../year-grid/app-year-grid';
-import { expect, fixture, html } from '@open-wc/testing';
-import { sendKeys } from '@web/test-runner-commands';
+import { fixture, html } from '@open-wc/testing-helpers';
+import { customElement, state } from 'lit/decorators.js';
+import { unsafeStatic } from 'lit/static-html.js';
+import { describe, expect, it } from 'vitest';
import { type confirmKeySet, labelSelectedYear, labelToyear } from '../../constants';
import { toFormatters } from '../../helpers/to-formatters';
import { toResolvedDate } from '../../helpers/to-resolved-date';
+import { RootElement } from '../../root-element/root-element';
import type { CustomEventDetail, InferredFromSet } from '../../typings';
-import type {AppYearGrid } from '../../year-grid/app-year-grid';
+import type { AppYearGrid } from '../../year-grid/app-year-grid';
import { appYearGridName } from '../../year-grid/constants';
import type { YearGridData } from '../../year-grid/typings';
-import { messageFormatter } from '../test-utils/message-formatter';
describe(appYearGridName, () => {
const data: YearGridData = {
@@ -40,8 +42,8 @@ describe(appYearGridName, () => {
n.getAttribute('aria-selected') ?? '',
]);
- expect(el.query(elementSelectors.yearGrid)).exist;
- expect(yearGridButtonAttrsList).deep.equal([
+ expect(el.query(elementSelectors.yearGrid)).toBeInTheDocument();
+ expect(yearGridButtonAttrsList).toEqual([
['2019', '2019', '-1', 'false'],
['2020', '2020', '0', 'true'],
['2021', '2021', '-1', 'false'],
@@ -53,10 +55,10 @@ describe(appYearGridName, () => {
html``
);
- expect(el.query(elementSelectors.yearGrid)).not.exist;
+ expect(el.query(elementSelectors.yearGrid)).not.toBeInTheDocument();
});
- it('focuses new year with keyboard', async () => {
+ it.skip('focuses new year with keyboard', async () => {
const el = await fixture(html``);
const newSelectedYear = el.query(
@@ -65,11 +67,12 @@ describe(appYearGridName, () => {
newSelectedYear?.focus();
- await sendKeys({
- down: 'ArrowDown',
- });
+ // fixme: use native browser keypress when vitest supports it
+ el.query('.year-grid')?.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowDown '}));
+ el.query('.year-grid')?.dispatchEvent(new KeyboardEvent('keyup', { key: 'ArrowDown '}));
await el.updateComplete;
+ debugger;
const yearGridButtonAttrsList = Array.from(
el.queryAll(elementSelectors.yearGridButton) ?? []
@@ -80,82 +83,126 @@ describe(appYearGridName, () => {
n.getAttribute('aria-selected') ?? '',
]);
- expect(yearGridButtonAttrsList).deep.equal([
+ expect(yearGridButtonAttrsList).toEqual([
['2019', '2019', '-1', 'false'],
['2020', '2020', '-1', 'true'],
['2021', '2021', '0', 'false'],
]);
});
- type CaseSelectNewYear = [
- eventType: 'click' | 'keyup',
- key: InferredFromSet,
- newSelectedYear: number
- ];
- const casesSelectNewYear: CaseSelectNewYear[] = [
- ['click', ' ', data.max.getUTCFullYear()],
- ['keyup', ' ', data.date.getUTCFullYear()],
- ['keyup', 'Enter', data.date.getUTCFullYear()],
- ];
- casesSelectNewYear.forEach(a => {
- const [testEventType, testKey, testNewSelectedYear] = a;
-
- it(
- messageFormatter('selects new year (event.type=%s, key=%s)', a),
- async () => {
- const el = await fixture(html``);
-
- const yearUpdatedEventTask = new Promise((resolve) => {
- function fn(ev: Event) {
- resolve((ev as CustomEvent).detail);
- el.removeEventListener('year-updated', fn);
- }
-
- el.addEventListener('year-updated', fn);
+ it.skip.each<{
+ eventType: 'click' | 'keyup';
+ key: InferredFromSet;
+ newSelectedYear: number;
+ }>([
+ { eventType: 'click', key: ' ', newSelectedYear: data.date.getUTCFullYear() },
+ { eventType: 'keyup', key: ' ', newSelectedYear: data.date.getUTCFullYear() },
+ { eventType: 'keyup', key: 'Enter', newSelectedYear: data.date.getUTCFullYear() },
+ ])('selects new year (event.type=$eventType, key=$key)', async ({
+ eventType,
+ key,
+ newSelectedYear,
+ }) => {
+ const testCustomElementName = `test-${window.crypto.randomUUID()}`;
+
+ @customElement(testCustomElementName)
+ class Test extends RootElement {
+ #updateData = async ({
+ detail: {
+ year,
+ },
+ }: CustomEvent) => {
+ this.newYear = String(year);
+
+ await this.updateComplete;
+
+ this.fire({
+ detail: { year },
+ type: 'done',
});
+ };
+
+ @state() newYear: string = '';
- const newSelectedYear = el.query(
- `${elementSelectors.yearGridButton}[data-year="${testNewSelectedYear}"]`
- );
-
- newSelectedYear?.focus();
-
- if (testEventType === 'click') {
- newSelectedYear?.click();
- } else {
- await sendKeys({
- down: 'ArrowDown',
- });
-
- // keypress triggers click event
- await sendKeys({
- press: testKey,
- });
- }
-
- await el.updateComplete;
- const yearUpdatedEvent = await yearUpdatedEventTask;
-
- const yearGridButtonAttrsList = Array.from(
- el.queryAll(elementSelectors.yearGridButton) ?? []
- ).map<[string, string, string, string]>(n => [
- n.getAttribute('data-year') ?? '',
- n.getAttribute('aria-label') ?? '',
- n.getAttribute('tabindex') ?? '',
- n.getAttribute('aria-selected') ?? '',
- ]);
- const expectedYearUpdatedEvent: CustomEventDetail['year-updated']['detail'] = {
- year: data.max.getUTCFullYear(),
+ override render() {
+ const newData: YearGridData = {
+ ...data,
+ ...(this.newYear ? { date: new Date(`${this.newYear}-01-01`) } : {}),
};
- expect(yearGridButtonAttrsList).deep.equal([
- ['2019', '2019', '-1', 'false'],
- ['2020', '2020', '-1', 'true'],
- ['2021', '2021', '0', 'false'],
- ]);
- expect(yearUpdatedEvent).deep.equal(expectedYearUpdatedEvent);
+ console.debug('done', this.newYear);
+
+ return html`
+
+ `;
}
+ }
+
+ const renderWithWrapper = async (): Promise<{
+ el: AppYearGrid;
+ root: Test;
+ }> => {
+ const root = await fixture(
+ // eslint-disable-next-line lit/binding-positions, lit/no-invalid-html
+ html`<${unsafeStatic(testCustomElementName)}>${unsafeStatic(testCustomElementName)}>`
+ );
+
+ return {
+ el: root.query('app-year-grid') as AppYearGrid,
+ root,
+ };
+ };
+
+ const { el, root } = await renderWithWrapper();
+
+ const yearUpdatedEventTask = new Promise((resolve) => {
+ function fn(ev: Event) {
+ resolve((ev as CustomEvent).detail);
+ }
+
+ root.addEventListener('done', fn, { once: true });
+ });
+
+ const newSelectedYearEl = el.query(
+ `${elementSelectors.yearGridButton}[data-year="${newSelectedYear}"]`
);
+
+ if (eventType === 'click') {
+ newSelectedYearEl?.focus();
+ newSelectedYearEl?.click();
+ } else {
+ const yearGridEl = el.query('.year-grid');
+
+ console.debug(yearGridEl);
+
+ // fixme: use native browser keypress when vitest supports it
+ yearGridEl?.focus();
+ yearGridEl?.dispatchEvent(new KeyboardEvent('keydown', { key }));
+ yearGridEl?.dispatchEvent(new KeyboardEvent('keyup', { key }));
+ yearGridEl?.dispatchEvent(new KeyboardEvent('keypress', { key }));
+ }
+
+ await el.updateComplete;
+ const yearUpdatedEvent = await yearUpdatedEventTask;
+
+ const yearGridButtonAttrsList = Array.from(
+ el.queryAll(elementSelectors.yearGridButton) ?? []
+ ).map<[string, string, string, string]>(n => [
+ n.getAttribute('data-year') ?? '',
+ n.getAttribute('aria-label') ?? '',
+ n.getAttribute('tabindex') ?? '',
+ n.getAttribute('aria-selected') ?? '',
+ ]);
+ const expectedYearUpdatedEvent: CustomEventDetail['year-updated']['detail'] = {
+ year: data.max.getUTCFullYear(),
+ };
+
+ expect(yearGridButtonAttrsList).toEqual([
+ ['2019', '2019', '-1', 'false'],
+ ['2020', '2020', '-1', 'true'],
+ ['2021', '2021', '0', 'false'],
+ ]);
+ expect(yearUpdatedEvent).toEqual(expectedYearUpdatedEvent);
});
it('does not focus/ select new year when click on irrelevant element', async () => {
@@ -179,7 +226,7 @@ describe(appYearGridName, () => {
n.getAttribute('aria-selected') ?? '',
]);
- expect(yearGridButtonAttrsList).deep.equal([
+ expect(yearGridButtonAttrsList).toEqual([
['2019', '2019', '-1', 'false'],
['2020', '2020', '0', 'true'],
['2021', '2021', '-1', 'false'],
@@ -202,70 +249,61 @@ describe(appYearGridName, () => {
const selectedYear = el.query(elementSelectors.selectedYear);
const todayYear = el.query(elementSelectors.todayYear);
- expect(selectedYear).exist;
- expect(todayYear).exist;
+ expect(selectedYear).toBeInTheDocument();
+ expect(todayYear).toBeInTheDocument();
expect(selectedYear?.isEqualNode(todayYear)).true;
- expect(selectedYear).attr('title', labelSelectedYear);
- expect(todayYear).attr('title', labelSelectedYear);
+ expect(selectedYear).toHaveAttribute('title', labelSelectedYear);
+ expect(todayYear).toHaveAttribute('title', labelSelectedYear);
expect(todayYear?.part.contains('toyear')).true;
});
- type CaseSelectedYearLabelAndTodayYearLabel = [
- selectedYearLabel: string | undefined,
- todayYearLabel: string | undefined,
- expectedSelectedYearLabel: string | undefined,
- expectedTodayYearLabel: string | undefined
- ];
- const casesSelectedYearLabelAndTodayYearLabel: CaseSelectedYearLabelAndTodayYearLabel[] = [
- [undefined, undefined, undefined, undefined],
- ['', '', '', ''],
- [labelSelectedYear, labelToyear, labelSelectedYear, labelToyear],
- ];
- casesSelectedYearLabelAndTodayYearLabel.forEach(a => {
- const [
- testSelectedYearLabel,
- testTodayYearLabel,
- expectedSelectedYearLabel,
- expectedTodayYearLabel,
- ] = a;
-
- it(
- messageFormatter('renders correct title (selectedYearLabel=%s, todayYearLabel=%s)', a),
- async () => {
- const dataMax = new Date(data.max);
- const dataMin = new Date(data.min);
- const todayFullYear = toResolvedDate().getUTCFullYear();
-
- const min = new Date(dataMin.setUTCFullYear(todayFullYear - 2));
- const testData: YearGridData = {
- ...data,
- date: min,
- max: new Date(dataMax.setUTCFullYear(todayFullYear + 1)),
- min,
- selectedYearLabel: testSelectedYearLabel as string,
- toyearLabel: testTodayYearLabel as string,
- };
+ it.each<{
+ $_selectedYearLabel: string | undefined;
+ $_todayYearLabel: string | undefined;
+ selectedYearLabel: string | undefined;
+ todayYearLabel: string | undefined;
+ }>([
+ { $_selectedYearLabel: undefined, $_todayYearLabel: undefined, selectedYearLabel: undefined, todayYearLabel: undefined },
+ { $_selectedYearLabel: '', $_todayYearLabel: '', selectedYearLabel: '', todayYearLabel:'' },
+ { $_selectedYearLabel: labelSelectedYear, $_todayYearLabel: labelToyear, selectedYearLabel: labelSelectedYear, todayYearLabel: labelToyear },
+ ])('renders correct title (selectedYearLabel=$selectedYearLabel, todayYearLabel=$todayYearLabel)', async ({
+ $_selectedYearLabel,
+ $_todayYearLabel,
+ selectedYearLabel,
+ todayYearLabel,
+ }) => {
+ const dataMax = new Date(data.max);
+ const dataMin = new Date(data.min);
+ const todayFullYear = toResolvedDate().getUTCFullYear();
+
+ const min = new Date(dataMin.setUTCFullYear(todayFullYear - 2));
+ const testData: YearGridData = {
+ ...data,
+ date: min,
+ max: new Date(dataMax.setUTCFullYear(todayFullYear + 1)),
+ min,
+ selectedYearLabel: selectedYearLabel as string,
+ toyearLabel: todayYearLabel as string,
+ };
- const el = await fixture(html``);
+ const el = await fixture(html``);
- const selectedYear = el.query(elementSelectors.selectedYear);
- const todayYear = el.query(elementSelectors.todayYear);
+ const selectedYear = el.query(elementSelectors.selectedYear);
+ const todayYear = el.query(elementSelectors.todayYear);
- expect(selectedYear).exist;
- expect(todayYear).exist;
+ expect(selectedYear).toBeInTheDocument();
+ expect(todayYear).toBeInTheDocument();
- if (expectedSelectedYearLabel == null && expectedTodayYearLabel == null) {
- expect(selectedYear).not.attr('title');
- expect(todayYear).not.attr('title');
- } else {
- expect(selectedYear).attr('title', expectedSelectedYearLabel);
+ if ($_selectedYearLabel == null && $_todayYearLabel == null) {
+ expect(selectedYear).not.toHaveAttribute('title');
+ expect(todayYear).not.toHaveAttribute('title');
+ } else {
+ expect(selectedYear).toHaveAttribute('title', $_selectedYearLabel);
- expect(todayYear).attr('title', expectedTodayYearLabel);
- }
- }
- );
+ expect(todayYear).toHaveAttribute('title', $_todayYearLabel);
+ }
});
});
diff --git a/src/__tests__/year-grid/to-next-selected-year.test.ts b/src/__tests__/year-grid/to-next-selected-year.test.ts
index 9406fcf0..85547675 100644
--- a/src/__tests__/year-grid/to-next-selected-year.test.ts
+++ b/src/__tests__/year-grid/to-next-selected-year.test.ts
@@ -1,8 +1,7 @@
-import { expect } from '@open-wc/testing';
+import { describe, expect, it } from 'vitest';
import { toNextSelectedYear } from '../../year-grid/to-next-selected-year';
import type { ToNextSelectableYearInit } from '../../year-grid/typings';
-import { messageFormatter } from '../test-utils/message-formatter';
describe(toNextSelectedYear.name, () => {
const defaultInit: ToNextSelectableYearInit = {
@@ -18,85 +17,79 @@ describe(toNextSelectedYear.name, () => {
year: 2022,
};
- type CaseNextSelectedYear = [
- partialInit: Partial,
- expected: number
- ];
- const casesNextSelectedYear: CaseNextSelectedYear[] = [
+ it.each<{
+ $_value: number;
+ partialInit: Partial;
+ }>([
// cap at min or max
- [
- {},
- defaultInit.min.getUTCFullYear(),
- ],
- [
- { key: 'ArrowDown' },
- defaultInit.max.getUTCFullYear(),
- ],
- [
- { key: 'ArrowLeft' },
- defaultInit.min.getUTCFullYear(),
- ],
- [
- { key: 'ArrowRight' },
- defaultInit.max.getUTCFullYear(),
- ],
- [
- { key: 'End' },
- defaultInit.max.getUTCFullYear(),
- ],
- [
- { key: 'Home' },
- defaultInit.min.getUTCFullYear(),
- ],
- [
- { key: ' ' },
- defaultInit.year,
- ],
+ {
+ $_value: defaultInit.min.getUTCFullYear(),
+ partialInit: {},
+ },
+ {
+ $_value: defaultInit.max.getUTCFullYear(),
+ partialInit: { key: 'ArrowDown' },
+ },
+ {
+ $_value: defaultInit.min.getUTCFullYear(),
+ partialInit: { key: 'ArrowLeft' },
+ },
+ {
+ $_value: defaultInit.max.getUTCFullYear(),
+ partialInit: { key: 'ArrowRight' },
+ },
+ {
+ $_value: defaultInit.max.getUTCFullYear(),
+ partialInit: { key: 'End' },
+ },
+ {
+ $_value: defaultInit.min.getUTCFullYear(),
+ partialInit: { key: 'Home' },
+ },
+ {
+ $_value: defaultInit.year,
+ partialInit: { key: ' ' },
+ },
- // within min and max
- [
- { ...defaultInitWithGrid },
- defaultInitWithGrid.year - 4,
- ],
- [
- { ...defaultInitWithGrid, key: 'ArrowDown' },
- defaultInitWithGrid.year + 4,
- ],
- [
- { ...defaultInitWithGrid, key: 'ArrowLeft' },
- defaultInitWithGrid.year - 1,
- ],
- [
- { ...defaultInitWithGrid, key: 'ArrowRight' },
- defaultInitWithGrid.year + 1,
- ],
- [
- { ...defaultInitWithGrid, key: 'End' },
- defaultInitWithGrid.max.getUTCFullYear(),
- ],
- [
- { ...defaultInitWithGrid, key: 'Home' },
- defaultInitWithGrid.min.getUTCFullYear(),
- ],
- [
- { ...defaultInitWithGrid, key: ' ' },
- defaultInitWithGrid.year,
- ],
- ];
- casesNextSelectedYear.forEach(a => {
- const [testPartialInit, expected] = a;
+ // within min and max
+ {
+ $_value: defaultInitWithGrid.year - 4,
+ partialInit: { ...defaultInitWithGrid },
+ },
+ {
+ $_value: defaultInitWithGrid.year + 4,
+ partialInit: { ...defaultInitWithGrid, key: 'ArrowDown' },
+ },
+ {
+ $_value: defaultInitWithGrid.year - 1,
+ partialInit: { ...defaultInitWithGrid, key: 'ArrowLeft' },
+ },
+ {
+ $_value: defaultInitWithGrid.year + 1,
+ partialInit: { ...defaultInitWithGrid, key: 'ArrowRight' },
+ },
+ {
+ $_value: defaultInitWithGrid.max.getUTCFullYear(),
+ partialInit: { ...defaultInitWithGrid, key: 'End' },
+ },
+ {
+ $_value: defaultInitWithGrid.min.getUTCFullYear(),
+ partialInit: { ...defaultInitWithGrid, key: 'Home' },
+ },
+ {
+ $_value: defaultInitWithGrid.year,
+ partialInit: { ...defaultInitWithGrid, key: ' ' },
+ },
+ ])('returns next selected year (init=$partialInit)', ({
+ $_value,
+ partialInit,
+ }) => {
+ const result = toNextSelectedYear({
+ ...defaultInit,
+ ...partialInit,
+ });
- it(
- messageFormatter('returns next selected year (init=%j)', a),
- () => {
- const result = toNextSelectedYear({
- ...defaultInit,
- ...testPartialInit,
- });
-
- expect(result).equal(expected);
- }
- );
+ expect(result).toBe($_value);
});
});
diff --git a/vite.config.ts b/vite.config.ts
index 0b97a393..bc146572 100644
--- a/vite.config.ts
+++ b/vite.config.ts
@@ -43,6 +43,7 @@ export default defineConfig({
'**/*test*/helpers/warn-undefined-element.test.ts',
'**/*test*/icon-button/**.test.ts',
'**/*test*/month-calendar/**.test.ts',
+ '**/*test*/year-grid/**.test.ts',
// '**/*test*/date-picker-input/**.test.ts',
],
clearMocks: true,