import React from 'react' import { render, screen, within } from '@testing-library/react' import { format } from 'date-fns' import { enUS } from 'date-fns/locale' import { vi } from 'vitest' import { CalendarSingle, type CalendarSingleProps } from '../CalendarSingle' import { setFocusInCalendar } from './setFocusInCalendar' const CalendarWrapper = (props: Partial): JSX.Element => ( setFocusInCalendar(calendarElement, props.selected)} {...props} /> ) const today = new Date() const todayMonth = format(today, 'MMMM yyyy') // e.g "June 2023" const todayDay = today.getDate().toString() // e.g "6" describe('setFocusInCalendar', () => { it('should focus on today when no date is selected', () => { render() const targetMonth = screen.getByRole('grid', { name: todayMonth }) const targetDay = within(targetMonth).getByRole('gridcell', { name: todayDay, }) const targetBtn = within(targetDay).getByRole('button') expect(targetBtn).toHaveFocus() }) it('should focus on the selected day', () => { render() const targetMonth = screen.getByRole('grid', { name: 'August 2022' }) const targetDay = within(targetMonth).getByRole('gridcell', { name: '15' }) const targetBtn = within(targetDay).getByRole('button') expect(targetBtn).toHaveFocus() }) it('should focus on today when selected date is invalid', () => { render() const targetMonth = screen.getByRole('grid', { name: todayMonth }) const targetDay = within(targetMonth).getByRole('gridcell', { name: todayDay, }) const targetBtn = within(targetDay).getByRole('button') expect(targetBtn).toHaveFocus() }) it('should focus on the first of the month when there is no selected day nor in the current month', () => { render() const targetMonth = screen.getByRole('grid', { name: 'May 2022' }) const targetDay = within(targetMonth).getByRole('gridcell', { name: '1' }) const targetBtn = within(targetDay).getByRole('button') expect(targetBtn).toHaveFocus() }) })