55 lines
1.8 KiB
TypeScript

import { render, screen } from '@testing-library/react';
import { userEvent } from '@testing-library/user-event';
import { withProvider } from 'shared/hoc/withProvider';
import { CalendarWidgetProvider } from '../../../CalendarWidgetContext';
import TodayButton from './TodayButton.ui';
import type { TodayButtonProps } from './TodayButton.ui';
describe('Test NextMonthButton', () => {
const user = userEvent.setup();
const setup = (props?: TodayButtonProps) => {
const TodayButtonComponent = () => (
<TodayButton {...props} />
);
const TodayButtonWithProvider = withProvider(CalendarWidgetProvider)(TodayButtonComponent);
render(<TodayButtonWithProvider />);
};
test('Basic render', () => {
setup();
const todayButtonElem = screen.getByTestId('today-button');
expect(todayButtonElem).toBeInTheDocument();
expect(todayButtonElem).toBeVisible();
});
test('onClick handler', async () => {
const onClick = jest.fn();
setup({ onClick });
const todayButtonElem = screen.getByTestId('today-button');
await user.click(todayButtonElem);
expect(onClick).toHaveBeenCalled();
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const onClickArgument = onClick.mock.calls[0][0] as number;
const calledDate = new Date(onClickArgument);
const calledYear = calledDate.getFullYear();
const calledMonthIndex = calledDate.getMonth();
const calledDateNumber = calledDate.getDate();
const calledString = `${calledYear}-${calledMonthIndex}-${calledDateNumber}`;
const currentYear = new Date().getFullYear();
const currentMonthIndex = new Date().getMonth();
const currentDateNumber = new Date().getDate();
const expectedString = `${currentYear}-${currentMonthIndex}-${currentDateNumber}`;
expect(calledString).toBe(expectedString);
});
});