refactor(test): add queries and expects

This commit is contained in:
Sergey Krylov 2024-05-23 05:49:58 +03:00
parent af8fe13b98
commit c17cca2b6f
17 changed files with 244 additions and 136 deletions

View File

@ -8,6 +8,11 @@
"import/order": [ "import/order": [
"error", { "error", {
"pathGroups": [ "pathGroups": [
{
"pattern": "__spec__/**",
"group": "builtin",
"position": "before"
},
{ {
"pattern": "apps/**", "pattern": "apps/**",
"group": "internal", "group": "internal",
@ -69,6 +74,8 @@
"spec/**", "spec/**",
"**/__tests__/**", "**/__tests__/**",
"**/__mocks__/**", "**/__mocks__/**",
"**/__spec__/**",
"**/configs/**",
"test.{[j|t]sx,[j|t]sx}", "test.{[j|t]sx,[j|t]sx}",
"test-*.{[j|t]sx,[j|t]sx}", "test-*.{[j|t]sx,[j|t]sx}",
"**/*{.,_}{test,spec}.{[j|t]sx,[j|t]sx}", "**/*{.,_}{test,spec}.{[j|t]sx,[j|t]sx}",

View File

@ -0,0 +1,20 @@
import type { MatcherFunction } from 'expect';
// eslint-disable-next-line func-names
export const toHaveDataDate: MatcherFunction<[dataDate: unknown]> = function (actual, dataDate) {
if (!(actual instanceof HTMLElement)) {
throw new TypeError('These must be HTMLElement!');
}
const actualDataDate = actual.getAttribute('data-date');
const pass = this.equals(actualDataDate, dataDate);
const message = () => `${this.utils.matcherHint('toHaveDataDate', 'element')}\n\n`
+ `Expected element to have data-date attribute:\n ${this.utils.printExpected(dataDate)}\n`
+ `Received:\n ${this.utils.printReceived(actualDataDate)}`;
return {
message,
pass,
};
};

View File

@ -0,0 +1,32 @@
import { queryHelpers, buildQueries } from '@testing-library/react';
import type { DataDateString } from 'shared/types/date';
import type { GetErrorFunction } from '@testing-library/dom/types/query-helpers';
import type { Matcher, MatcherOptions } from '@testing-library/react';
const queryAllByDataDate = (
container: HTMLElement,
id: Matcher,
options?: MatcherOptions | undefined,
) => queryHelpers.queryAllByAttribute('data-date', container, id, options);
const getMultipleError: GetErrorFunction<[id: DataDateString, options?: MatcherOptions | undefined]> = (_, dataDateValue) => `Found multiple elements with the data-date attribute of: ${dataDateValue}`;
const getMissingError: GetErrorFunction<[id: DataDateString, options?: MatcherOptions | undefined]> = (_, dataDateValue: string) => `Unable to find an element with the data-date attribute of: ${dataDateValue}`;
const [
queryByDataDate,
getAllByDataDate,
getByDataDate,
findAllByDataDate,
findByDataDate,
] = buildQueries(queryAllByDataDate, getMultipleError, getMissingError);
export {
queryByDataDate,
queryAllByDataDate,
getByDataDate,
getAllByDataDate,
findAllByDataDate,
findByDataDate,
};

18
__spec__/helpers.ts Normal file
View File

@ -0,0 +1,18 @@
import { fireEvent } from '@testing-library/react';
import type { DataDateString } from 'shared/types/date';
import { screen } from './utils';
export const pickCellByDataDate = (dataDate: DataDateString) => {
const cellElem = screen.getByDataDate(dataDate);
fireEvent.click(cellElem);
};
export const dragAndDropCellByDataDate = (dragDataDate: DataDateString, dropDataDate: DataDateString) => {
const oldLastDateElem = screen.getByDataDate(dragDataDate);
const dropTarget = screen.getByDataDate(dropDataDate);
fireEvent.dragStart(oldLastDateElem);
fireEvent.drop(dropTarget);
};

15
__spec__/utils.js Normal file
View File

@ -0,0 +1,15 @@
import { render, queries, within } from '@testing-library/react';
import * as customQueries from './custom-queries/byDataDate';
const allQueries = {
...queries,
...customQueries,
};
const customScreen = within(document.body, allQueries);
const customWithin = (element) => within(element, allQueries);
const customRender = (ui, options) => render(ui, { queries: allQueries, ...options });
// override render method
export { customScreen as screen, customWithin as within, customRender as render };

10
configs/jest/global.d.ts vendored Normal file
View File

@ -0,0 +1,10 @@
declare global {
namespace jest {
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface Matchers<R> {
toHaveDataDate: (dataDate: string) => R;
}
}
}
export {};

View File

@ -1 +1,8 @@
import '@testing-library/jest-dom'; import '@testing-library/jest-dom';
import { expect } from '@jest/globals';
import { toHaveDataDate } from '../../__spec__/custom-expects/toHaveDataDate';
expect.extend({
toHaveDataDate,
});

View File

@ -7,6 +7,7 @@ const config: Config = {
setupFilesAfterEnv: ['./configs/jest/setupTests.ts'], setupFilesAfterEnv: ['./configs/jest/setupTests.ts'],
modulePaths: [ modulePaths: [
'<rootDir>src', '<rootDir>src',
'<rootDir>',
], ],
verbose: true, verbose: true,
preset: 'ts-jest/presets/js-with-ts', preset: 'ts-jest/presets/js-with-ts',

View File

@ -1,18 +0,0 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import { fireEvent } from '@testing-library/react';
import type { DataDateString } from 'shared/types/date';
// todo fix
// eslint-disable-next-line @stylistic/no-extra-parens
export const getDateCellByDataDate = (container: HTMLElement, dataDate: DataDateString) => (
container.querySelector(`[data-date="${dataDate}"]`)
);
export const pickCellByDataDate = (...params: Parameters<typeof getDateCellByDataDate>) => {
const cellElem = getDateCellByDataDate(...params);
if (!cellElem) {
throw new Error('first cell not found');
}
fireEvent.click(cellElem);
};

View File

@ -32,6 +32,6 @@ describe('Base Render', () => {
setup(new Date(2024, 3, 15)); setup(new Date(2024, 3, 15));
const dateCellElem = screen.getByTestId('date-cell'); const dateCellElem = screen.getByTestId('date-cell');
expect(dateCellElem).toHaveAttribute('data-date', '04/15/2024'); expect(dateCellElem).toHaveDataDate('04/15/2024');
}); });
}); });

View File

@ -9,12 +9,7 @@ import { CalendarWidgetProvider } from '../../../CalendarWidgetContext';
import MonthSelect from './MonthSelect.ui'; import MonthSelect from './MonthSelect.ui';
describe('Test MonthSelect', () => { describe('Test MonthSelect', () => {
let user; const user = userEvent.setup();
beforeEach(() => {
user = userEvent.setup();
});
const setup = (props, providerProps) => { const setup = (props, providerProps) => {
const MonthSelectComponent = () => ( const MonthSelectComponent = () => (
<MonthSelect {...props} /> <MonthSelect {...props} />

View File

@ -9,14 +9,9 @@ import NextMonthButton from './NextMonthButton.ui';
import type { NextMonthButtonProps } from './NextMonthButton.ui'; import type { NextMonthButtonProps } from './NextMonthButton.ui';
import type { CalendarWidgetProviderProps } from '../../../CalendarWidgetContext'; import type { CalendarWidgetProviderProps } from '../../../CalendarWidgetContext';
import type { UserEvent } from '@testing-library/user-event';
describe('Test NextMonthButton', () => { describe('Test NextMonthButton', () => {
let user: UserEvent; const user = userEvent.setup();
beforeEach(() => {
user = userEvent.setup();
});
const setup = (props?: NextMonthButtonProps, providerProps?: CalendarWidgetProviderProps) => { const setup = (props?: NextMonthButtonProps, providerProps?: CalendarWidgetProviderProps) => {
const NextMonthButtonComponent = () => ( const NextMonthButtonComponent = () => (

View File

@ -8,15 +8,9 @@ import { CalendarWidgetProvider, type CalendarWidgetProviderProps } from '../../
import PrevMonthButton from './PrevMonthButton.ui'; import PrevMonthButton from './PrevMonthButton.ui';
import type { PrevMonthButtonProps } from './PrevMonthButton.ui'; import type { PrevMonthButtonProps } from './PrevMonthButton.ui';
import type { UserEvent } from '@testing-library/user-event';
describe('Test NextMonthButton', () => { describe('Test NextMonthButton', () => {
let user: UserEvent; const user = userEvent.setup();
beforeEach(() => {
user = userEvent.setup();
});
const setup = (props?: PrevMonthButtonProps, providerProps?: CalendarWidgetProviderProps) => { const setup = (props?: PrevMonthButtonProps, providerProps?: CalendarWidgetProviderProps) => {
const PrevMonthButtonComponent = () => ( const PrevMonthButtonComponent = () => (
<PrevMonthButton {...props} /> <PrevMonthButton {...props} />

View File

@ -8,15 +8,9 @@ import { CalendarWidgetProvider } from '../../../CalendarWidgetContext';
import TodayButton from './TodayButton.ui'; import TodayButton from './TodayButton.ui';
import type { TodayButtonProps } from './TodayButton.ui'; import type { TodayButtonProps } from './TodayButton.ui';
import type { UserEvent } from '@testing-library/user-event';
describe('Test NextMonthButton', () => { describe('Test NextMonthButton', () => {
let user: UserEvent; const user = userEvent.setup();
beforeEach(() => {
user = userEvent.setup();
});
const setup = (props?: TodayButtonProps) => { const setup = (props?: TodayButtonProps) => {
const TodayButtonComponent = () => ( const TodayButtonComponent = () => (
<TodayButton {...props} /> <TodayButton {...props} />

View File

@ -8,15 +8,9 @@ import { CalendarWidgetProvider } from '../../../CalendarWidgetContext';
import YearSelect from './YearSelect.ui'; import YearSelect from './YearSelect.ui';
import type { YearSelectProps } from './YearSelect.ui'; import type { YearSelectProps } from './YearSelect.ui';
import type { UserEvent } from '@testing-library/user-event';
describe('Test YearSelect', () => { describe('Test YearSelect', () => {
let user: UserEvent; const user = userEvent.setup();
beforeEach(() => {
user = userEvent.setup();
});
const setup = (props?: YearSelectProps) => { const setup = (props?: YearSelectProps) => {
const YearSelectComponent = () => ( const YearSelectComponent = () => (
<YearSelect {...props} /> <YearSelect {...props} />

View File

@ -1,6 +1,5 @@
import { render, screen } from '@testing-library/react'; import { dragAndDropCellByDataDate, pickCellByDataDate } from '__spec__/helpers';
import { render, screen } from '__spec__/utils';
import { getDateCellByDataDate, pickCellByDataDate } from 'shared/utils/spec-helpers/date';
import DatePicker from './DatePicker.ui'; import DatePicker from './DatePicker.ui';
@ -27,7 +26,7 @@ describe('Test DatePicker', () => {
const firstPickedCellElem = screen.queryByTestId('first-selected-cell'); const firstPickedCellElem = screen.queryByTestId('first-selected-cell');
expect(firstPickedCellElem).toBeVisible(); expect(firstPickedCellElem).toBeVisible();
expect(firstPickedCellElem).toHaveAttribute('data-date', '10/11/2024'); expect(firstPickedCellElem).toHaveDataDate('10/11/2024');
const lastPickedCellElem = screen.queryByTestId('last-selected-cell'); const lastPickedCellElem = screen.queryByTestId('last-selected-cell');
expect(lastPickedCellElem).not.toBeInTheDocument(); expect(lastPickedCellElem).not.toBeInTheDocument();
@ -41,7 +40,7 @@ describe('Test DatePicker', () => {
const lastPickedCellElem = screen.queryByTestId('last-selected-cell'); const lastPickedCellElem = screen.queryByTestId('last-selected-cell');
expect(lastPickedCellElem).toBeVisible(); expect(lastPickedCellElem).toBeVisible();
expect(lastPickedCellElem).toHaveAttribute('data-date', '10/18/2024'); expect(lastPickedCellElem).toHaveDataDate('10/18/2024');
const firstPickedCellElem = screen.queryByTestId('first-selected-cell'); const firstPickedCellElem = screen.queryByTestId('first-selected-cell');
expect(firstPickedCellElem).not.toBeInTheDocument(); expect(firstPickedCellElem).not.toBeInTheDocument();
@ -50,13 +49,12 @@ describe('Test DatePicker', () => {
describe('onSelect handler', () => { describe('onSelect handler', () => {
test('No default picked', () => { test('No default picked', () => {
const onSelect = jest.fn(); const onSelect = jest.fn();
setup({
const { container } = setup({
onSelect, onSelect,
value: new Date(2024, 9, 15), value: new Date(2024, 9, 15),
}); });
pickCellByDataDate(container, '10/18/2024'); pickCellByDataDate('10/18/2024');
expect(onSelect).toHaveBeenCalled(); expect(onSelect).toHaveBeenCalled();
expect(onSelect).toHaveBeenCalledWith({ expect(onSelect).toHaveBeenCalledWith({
@ -68,14 +66,13 @@ describe('Test DatePicker', () => {
test('Picked first date', () => { test('Picked first date', () => {
const onSelect = jest.fn(); const onSelect = jest.fn();
setup({
const { container } = setup({
onSelect, onSelect,
value: new Date(2024, 9, 15), value: new Date(2024, 9, 15),
firstDate: new Date(2024, 9, 15), firstDate: new Date(2024, 9, 15),
}); });
pickCellByDataDate(container, '10/18/2024'); pickCellByDataDate('10/18/2024');
expect(onSelect).toHaveBeenCalled(); expect(onSelect).toHaveBeenCalled();
expect(onSelect).toHaveBeenCalledWith({ expect(onSelect).toHaveBeenCalledWith({
@ -87,14 +84,13 @@ describe('Test DatePicker', () => {
test('Picked last date', () => { test('Picked last date', () => {
const onSelect = jest.fn(); const onSelect = jest.fn();
setup({
const { container } = setup({
onSelect, onSelect,
value: new Date(2024, 9, 15), value: new Date(2024, 9, 15),
lastDate: new Date(2024, 9, 15), lastDate: new Date(2024, 9, 15),
}); });
pickCellByDataDate(container, '10/11/2024'); pickCellByDataDate('10/11/2024');
expect(onSelect).toHaveBeenCalled(); expect(onSelect).toHaveBeenCalled();
expect(onSelect).toHaveBeenCalledWith({ expect(onSelect).toHaveBeenCalledWith({
@ -108,15 +104,14 @@ describe('Test DatePicker', () => {
describe('onUnselect handler', () => { describe('onUnselect handler', () => {
test('Unpick first date', () => { test('Unpick first date', () => {
const onUnselect = jest.fn(); const onUnselect = jest.fn();
setup({
const { container } = setup({
onUnselect, onUnselect,
value: new Date(2024, 9, 15), value: new Date(2024, 9, 15),
firstDate: new Date(2024, 9, 15), firstDate: new Date(2024, 9, 15),
lastDate: new Date(2024, 9, 18), lastDate: new Date(2024, 9, 18),
}); });
pickCellByDataDate(container, '10/15/2024'); pickCellByDataDate('10/15/2024');
expect(onUnselect).toHaveBeenCalled(); expect(onUnselect).toHaveBeenCalled();
expect(onUnselect).toHaveBeenCalledWith({ expect(onUnselect).toHaveBeenCalledWith({
@ -128,15 +123,14 @@ describe('Test DatePicker', () => {
test('Unpick last date', () => { test('Unpick last date', () => {
const onUnselect = jest.fn(); const onUnselect = jest.fn();
setup({
const { container } = setup({
onUnselect, onUnselect,
value: new Date(2024, 9, 15), value: new Date(2024, 9, 15),
firstDate: new Date(2024, 9, 15), firstDate: new Date(2024, 9, 15),
lastDate: new Date(2024, 9, 18), lastDate: new Date(2024, 9, 18),
}); });
pickCellByDataDate(container, '10/18/2024'); pickCellByDataDate('10/18/2024');
expect(onUnselect).toHaveBeenCalled(); expect(onUnselect).toHaveBeenCalled();
expect(onUnselect).toHaveBeenCalledWith({ expect(onUnselect).toHaveBeenCalledWith({
@ -150,211 +144,259 @@ describe('Test DatePicker', () => {
describe('Test pick date', () => { describe('Test pick date', () => {
test('No picked date, select first', () => { test('No picked date, select first', () => {
const { container } = setup({ setup({
value: new Date(2024, 9, 15), value: new Date(2024, 9, 15),
}); });
const cellElem = screen.getByDataDate('10/11/2024');
const cellElem = getDateCellByDataDate(container, '10/11/2024'); pickCellByDataDate('10/11/2024');
pickCellByDataDate(container, '10/11/2024');
const firstSelectedCellElem = screen.queryByTestId('first-selected-cell'); const firstSelectedCellElem = screen.queryByTestId('first-selected-cell');
expect(firstSelectedCellElem).toBe(cellElem); expect(firstSelectedCellElem).toBe(cellElem);
}); });
test('Picked first date, pick before first date', () => { test('Picked first date, pick before first date', () => {
const { container } = setup({ setup({
value: new Date(2024, 9, 15), value: new Date(2024, 9, 15),
firstDate: new Date(2024, 9, 15), firstDate: new Date(2024, 9, 15),
}); });
pickCellByDataDate('10/11/2024');
pickCellByDataDate(container, '10/11/2024');
const firstPickedCellElem = screen.queryByTestId('first-selected-cell'); const firstPickedCellElem = screen.queryByTestId('first-selected-cell');
expect(firstPickedCellElem).toHaveAttribute('data-date', '10/11/2024'); expect(firstPickedCellElem).toHaveDataDate('10/11/2024');
const lastPickedCellElem = screen.queryByTestId('last-selected-cell'); const lastPickedCellElem = screen.queryByTestId('last-selected-cell');
expect(lastPickedCellElem).toHaveAttribute('data-date', '10/15/2024'); expect(lastPickedCellElem).toHaveDataDate('10/15/2024');
}); });
test('Picked first date, pick after first date', () => { test('Picked first date, pick after first date', () => {
const { container } = setup({ setup({
value: new Date(2024, 9, 15), value: new Date(2024, 9, 15),
firstDate: new Date(2024, 9, 15), firstDate: new Date(2024, 9, 15),
}); });
pickCellByDataDate('10/18/2024');
pickCellByDataDate(container, '10/18/2024');
const firstPickedCellElem = screen.queryByTestId('first-selected-cell'); const firstPickedCellElem = screen.queryByTestId('first-selected-cell');
expect(firstPickedCellElem).toHaveAttribute('data-date', '10/15/2024'); expect(firstPickedCellElem).toHaveDataDate('10/15/2024');
const lastPickedCellElem = screen.queryByTestId('last-selected-cell'); const lastPickedCellElem = screen.queryByTestId('last-selected-cell');
expect(lastPickedCellElem).toHaveAttribute('data-date', '10/18/2024'); expect(lastPickedCellElem).toHaveDataDate('10/18/2024');
}); });
test('Picked first date, pick this date', () => { test('Picked first date, pick this date', () => {
const { container } = setup({ setup({
value: new Date(2024, 9, 15), value: new Date(2024, 9, 15),
firstDate: new Date(2024, 9, 15), firstDate: new Date(2024, 9, 15),
}); });
pickCellByDataDate('10/15/2024');
pickCellByDataDate(container, '10/15/2024');
const firstPickedCellElem = screen.queryByTestId('first-selected-cell'); const firstPickedCellElem = screen.queryByTestId('first-selected-cell');
expect(firstPickedCellElem).not.toBeInTheDocument(); expect(firstPickedCellElem).not.toBeInTheDocument();
}); });
test('Picked last date, pick before last date', () => { test('Picked last date, pick before last date', () => {
const { container } = setup({ setup({
value: new Date(2024, 9, 15), value: new Date(2024, 9, 15),
lastDate: new Date(2024, 9, 15), lastDate: new Date(2024, 9, 15),
}); });
pickCellByDataDate('10/11/2024');
pickCellByDataDate(container, '10/11/2024');
const firstPickedCellElem = screen.queryByTestId('first-selected-cell'); const firstPickedCellElem = screen.queryByTestId('first-selected-cell');
expect(firstPickedCellElem).toHaveAttribute('data-date', '10/11/2024'); expect(firstPickedCellElem).toHaveDataDate('10/11/2024');
const lastPickedCellElem = screen.queryByTestId('last-selected-cell'); const lastPickedCellElem = screen.queryByTestId('last-selected-cell');
expect(lastPickedCellElem).toHaveAttribute('data-date', '10/15/2024'); expect(lastPickedCellElem).toHaveDataDate('10/15/2024');
}); });
test('Picked last date, pick after last date', () => { test('Picked last date, pick after last date', () => {
const { container } = setup({ setup({
value: new Date(2024, 9, 15), value: new Date(2024, 9, 15),
lastDate: new Date(2024, 9, 15), lastDate: new Date(2024, 9, 15),
}); });
pickCellByDataDate('10/18/2024');
pickCellByDataDate(container, '10/18/2024');
const firstPickedCellElem = screen.queryByTestId('first-selected-cell'); const firstPickedCellElem = screen.queryByTestId('first-selected-cell');
expect(firstPickedCellElem).toHaveAttribute('data-date', '10/15/2024'); expect(firstPickedCellElem).toHaveDataDate('10/15/2024');
const lastPickedCellElem = screen.queryByTestId('last-selected-cell'); const lastPickedCellElem = screen.queryByTestId('last-selected-cell');
expect(lastPickedCellElem).toHaveAttribute('data-date', '10/18/2024'); expect(lastPickedCellElem).toHaveDataDate('10/18/2024');
}); });
test('Picked last date, pick this date', () => { test('Picked last date, pick this date', () => {
const { container } = setup({ setup({
value: new Date(2024, 9, 15), value: new Date(2024, 9, 15),
lastDate: new Date(2024, 9, 15), lastDate: new Date(2024, 9, 15),
}); });
pickCellByDataDate('10/15/2024');
pickCellByDataDate(container, '10/15/2024');
const lastPickedCellElem = screen.queryByTestId('last-selected-cell'); const lastPickedCellElem = screen.queryByTestId('last-selected-cell');
expect(lastPickedCellElem).not.toBeInTheDocument(); expect(lastPickedCellElem).not.toBeInTheDocument();
}); });
test('Picked first & last date, pick date before first', () => { test('Picked first & last date, pick date before first', () => {
const { container } = setup({ setup({
value: new Date(2024, 9, 15), value: new Date(2024, 9, 15),
firstDate: new Date(2024, 9, 15), firstDate: new Date(2024, 9, 15),
lastDate: new Date(2024, 9, 18), lastDate: new Date(2024, 9, 18),
}); });
pickCellByDataDate('10/11/2024');
pickCellByDataDate(container, '10/11/2024');
const firstPickedCellElem = screen.queryByTestId('first-selected-cell'); const firstPickedCellElem = screen.queryByTestId('first-selected-cell');
expect(firstPickedCellElem).toHaveAttribute('data-date', '10/11/2024'); expect(firstPickedCellElem).toHaveDataDate('10/11/2024');
const lastPickedCellElem = screen.queryByTestId('last-selected-cell'); const lastPickedCellElem = screen.queryByTestId('last-selected-cell');
expect(lastPickedCellElem).toHaveAttribute('data-date', '10/18/2024'); expect(lastPickedCellElem).toHaveDataDate('10/18/2024');
}); });
test('Picked first & last date, pick date after last', () => { test('Picked first & last date, pick date after last', () => {
const { container } = setup({ setup({
value: new Date(2024, 9, 15), value: new Date(2024, 9, 15),
firstDate: new Date(2024, 9, 15), firstDate: new Date(2024, 9, 15),
lastDate: new Date(2024, 9, 18), lastDate: new Date(2024, 9, 18),
}); });
pickCellByDataDate('10/21/2024');
pickCellByDataDate(container, '10/21/2024');
const firstPickedCellElem = screen.queryByTestId('first-selected-cell'); const firstPickedCellElem = screen.queryByTestId('first-selected-cell');
expect(firstPickedCellElem).toHaveAttribute('data-date', '10/15/2024'); expect(firstPickedCellElem).toHaveDataDate('10/15/2024');
const lastPickedCellElem = screen.queryByTestId('last-selected-cell'); const lastPickedCellElem = screen.queryByTestId('last-selected-cell');
expect(lastPickedCellElem).toHaveAttribute('data-date', '10/21/2024'); expect(lastPickedCellElem).toHaveDataDate('10/21/2024');
}); });
test('Picked first & last date, pick fist date', () => { test('Picked first & last date, pick fist date', () => {
const { container } = setup({ setup({
value: new Date(2024, 9, 15), value: new Date(2024, 9, 15),
firstDate: new Date(2024, 9, 15), firstDate: new Date(2024, 9, 15),
lastDate: new Date(2024, 9, 18), lastDate: new Date(2024, 9, 18),
}); });
pickCellByDataDate('10/15/2024');
pickCellByDataDate(container, '10/15/2024');
const firstPickedCellElem = screen.queryByTestId('first-selected-cell'); const firstPickedCellElem = screen.queryByTestId('first-selected-cell');
expect(firstPickedCellElem).not.toBeInTheDocument(); expect(firstPickedCellElem).not.toBeInTheDocument();
const lastPickedCellElem = screen.queryByTestId('last-selected-cell'); const lastPickedCellElem = screen.queryByTestId('last-selected-cell');
expect(lastPickedCellElem).toHaveAttribute('data-date', '10/18/2024'); expect(lastPickedCellElem).toHaveDataDate('10/18/2024');
}); });
test('Picked first & last date, pick last date', () => { test('Picked first & last date, pick last date', () => {
const { container } = setup({ setup({
value: new Date(2024, 9, 15), value: new Date(2024, 9, 15),
firstDate: new Date(2024, 9, 15), firstDate: new Date(2024, 9, 15),
lastDate: new Date(2024, 9, 18), lastDate: new Date(2024, 9, 18),
}); });
pickCellByDataDate('10/18/2024');
pickCellByDataDate(container, '10/18/2024');
const firstPickedCellElem = screen.queryByTestId('first-selected-cell'); const firstPickedCellElem = screen.queryByTestId('first-selected-cell');
expect(firstPickedCellElem).toHaveAttribute('data-date', '10/15/2024'); expect(firstPickedCellElem).toHaveDataDate('10/15/2024');
const lastPickedCellElem = screen.queryByTestId('last-selected-cell'); const lastPickedCellElem = screen.queryByTestId('last-selected-cell');
expect(lastPickedCellElem).not.toBeInTheDocument(); expect(lastPickedCellElem).not.toBeInTheDocument();
}); });
test('Picked first & last date, pick the date closer to first date', () => { test('Picked first & last date, pick the date closer to first date', () => {
const { container } = setup({ setup({
value: new Date(2024, 9, 15), value: new Date(2024, 9, 15),
firstDate: new Date(2024, 9, 15), firstDate: new Date(2024, 9, 15),
lastDate: new Date(2024, 9, 18), lastDate: new Date(2024, 9, 18),
}); });
pickCellByDataDate('10/16/2024');
pickCellByDataDate(container, '10/16/2024');
const firstPickedCellElem = screen.queryByTestId('first-selected-cell'); const firstPickedCellElem = screen.queryByTestId('first-selected-cell');
expect(firstPickedCellElem).toHaveAttribute('data-date', '10/16/2024'); expect(firstPickedCellElem).toHaveDataDate('10/16/2024');
const lastPickedCellElem = screen.queryByTestId('last-selected-cell'); const lastPickedCellElem = screen.queryByTestId('last-selected-cell');
expect(lastPickedCellElem).toHaveAttribute('data-date', '10/18/2024'); expect(lastPickedCellElem).toHaveDataDate('10/18/2024');
}); });
test('Picked first & last date, pick the date closer to last date', () => { test('Picked first & last date, pick the date closer to last date', () => {
const { container } = setup({ setup({
value: new Date(2024, 9, 15), value: new Date(2024, 9, 15),
firstDate: new Date(2024, 9, 15), firstDate: new Date(2024, 9, 15),
lastDate: new Date(2024, 9, 18), lastDate: new Date(2024, 9, 18),
}); });
pickCellByDataDate('10/17/2024');
pickCellByDataDate(container, '10/17/2024');
const firstPickedCellElem = screen.queryByTestId('first-selected-cell'); const firstPickedCellElem = screen.queryByTestId('first-selected-cell');
expect(firstPickedCellElem).toHaveAttribute('data-date', '10/15/2024'); expect(firstPickedCellElem).toHaveDataDate('10/15/2024');
const lastPickedCellElem = screen.queryByTestId('last-selected-cell'); const lastPickedCellElem = screen.queryByTestId('last-selected-cell');
expect(lastPickedCellElem).toHaveAttribute('data-date', '10/17/2024'); expect(lastPickedCellElem).toHaveDataDate('10/17/2024');
}); });
test('Picked first & last date, pick the equidistant date', () => { test('Picked first & last date, pick the equidistant date', () => {
const { container } = setup({ setup({
value: new Date(2024, 9, 15), value: new Date(2024, 9, 15),
firstDate: new Date(2024, 9, 15), firstDate: new Date(2024, 9, 15),
lastDate: new Date(2024, 9, 19), lastDate: new Date(2024, 9, 19),
}); });
pickCellByDataDate('10/17/2024');
pickCellByDataDate(container, '10/17/2024');
const firstPickedCellElem = screen.queryByTestId('first-selected-cell'); const firstPickedCellElem = screen.queryByTestId('first-selected-cell');
expect(firstPickedCellElem).toHaveAttribute('data-date', '10/17/2024'); expect(firstPickedCellElem).toHaveDataDate('10/17/2024');
const lastPickedCellElem = screen.queryByTestId('last-selected-cell'); const lastPickedCellElem = screen.queryByTestId('last-selected-cell');
expect(lastPickedCellElem).toHaveAttribute('data-date', '10/19/2024'); expect(lastPickedCellElem).toHaveDataDate('10/19/2024');
});
});
describe('Test dragging', () => {
test('Drag first cell and drop earlier end date', () => {
setup({
firstDate: new Date(2024, 9, 15),
lastDate: new Date(2024, 9, 18),
value: new Date(2024, 9, 15),
});
dragAndDropCellByDataDate('10/15/2024', '10/11/2024');
const newFirstDateElem = screen.getByTestId('first-selected-cell');
expect(newFirstDateElem).toHaveDataDate('10/11/2024');
const lastDateElem = screen.getByTestId('last-selected-cell');
expect(lastDateElem).toHaveDataDate('10/18/2024');
});
test('Drag last cell and drop later start date', () => {
setup({
firstDate: new Date(2024, 9, 15),
lastDate: new Date(2024, 9, 18),
value: new Date(2024, 9, 15),
});
dragAndDropCellByDataDate('10/18/2024', '10/16/2024');
const newFirstDateElem = screen.getByTestId('first-selected-cell');
expect(newFirstDateElem).toHaveDataDate('10/15/2024');
const lastDateElem = screen.getByTestId('last-selected-cell');
expect(lastDateElem).toHaveDataDate('10/16/2024');
});
test('Drag first cell and drop later end date', () => {
setup({
firstDate: new Date(2024, 9, 15),
lastDate: new Date(2024, 9, 18),
value: new Date(2024, 9, 15),
});
dragAndDropCellByDataDate('10/15/2024', '10/20/2024');
const newFirstDateElem = screen.getByTestId('first-selected-cell');
expect(newFirstDateElem).toHaveDataDate('10/18/2024');
const lastDateElem = screen.getByTestId('last-selected-cell');
expect(lastDateElem).toHaveDataDate('10/20/2024');
});
test('Drag last cell and drop earlier start date', () => {
setup({
firstDate: new Date(2024, 9, 15),
lastDate: new Date(2024, 9, 18),
value: new Date(2024, 9, 15),
});
dragAndDropCellByDataDate('10/18/2024', '10/11/2024');
const newFirstDateElem = screen.getByTestId('first-selected-cell');
expect(newFirstDateElem).toHaveDataDate('10/11/2024');
const lastDateElem = screen.getByTestId('last-selected-cell');
expect(lastDateElem).toHaveDataDate('10/15/2024');
}); });
}); });
}); });

View File

@ -2,7 +2,8 @@
"include": [ "include": [
"./src", "./src",
"./configs", "./configs",
"./jest.config.ts" "./jest.config.ts",
"./__spec__",
], ],
"compilerOptions": { "compilerOptions": {
"allowUnreachableCode": false, "allowUnreachableCode": false,
@ -30,6 +31,7 @@
"module": "ESNext", "module": "ESNext",
"moduleResolution": "Node", "moduleResolution": "Node",
"paths": { "paths": {
"__spec__/*": ["./__spec__/*"],
"*": ["./src/*"] "*": ["./src/*"]
}, },