Skip to content

Commit c5c3c06

Browse files
committed
test: complete testing for single mode
1 parent 73571fb commit c5c3c06

2 files changed

Lines changed: 33 additions & 5 deletions

File tree

packages/react-use-calendar-component/src/app/components/StateInfo.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ interface Props {
66
export default function StateInfo({ value, selectedDates }: Props) {
77
return (
88
<div className='text-white flex justify-between mt-5'>
9-
<div className='flex flex-col'>
9+
<div className='flex flex-col' data-testid='value'>
1010
<div className='text-lg text-rose-300'>value:</div>
1111
{value.map((v, index) => (
12-
<span key={index}>{v.toLocaleDateString()}</span>
12+
<span key={index}>{v.toDateString()}</span>
1313
))}
1414
</div>
15-
<div className='flex flex-col'>
15+
<div className='flex flex-col' data-testid='selected-dates'>
1616
<div className='text-lg text-rose-300'>selected dates:</div>
1717
{selectedDates.map((date, index) => {
18-
return <span key={index}>{date.toLocaleDateString()}</span>;
18+
return <span key={index}>{date.toDateString()}</span>;
1919
})}
2020
</div>
2121
</div>

packages/react-use-calendar-component/src/test/single.test.tsx

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ import userEvent from '@testing-library/user-event';
44
import { Single } from '../app/components/Examples';
55
import { getLocaleMonth } from './utils';
66

7+
const dateInstance = new Date(2022, 9, 1);
8+
const idValue = 'value';
9+
const idSelectedDates = 'selected-dates';
10+
711
describe('common functionality', () => {
812
it('should display correct year/month', async () => {
9-
const dateInstance = new Date(2022, 9, 1);
1013
jest.useFakeTimers({ now: dateInstance });
1114
const user = userEvent.setup({ delay: null });
1215
render(<Single />);
@@ -49,4 +52,29 @@ describe('common functionality', () => {
4952
expect(screen.getByText(currentMonth)).toBeInTheDocument();
5053
expect(screen.getByTitle('today')).toHaveTextContent('1');
5154
});
55+
56+
it('should change value and selected dates correctly', async () => {
57+
jest.useFakeTimers({ now: dateInstance });
58+
const user = userEvent.setup({ delay: null });
59+
render(<Single />);
60+
const valueBlock = screen.getByTestId(idValue);
61+
const datesBlock = screen.getByTestId(idSelectedDates);
62+
let testString = dateInstance.toDateString();
63+
expect(valueBlock).toHaveTextContent(testString);
64+
expect(datesBlock).toHaveTextContent(testString);
65+
66+
const date10 = screen.getByRole('button', { name: '10' });
67+
await user.click(date10);
68+
testString = new Date(2022, 9, 10).toDateString();
69+
expect(valueBlock).toHaveTextContent(testString);
70+
expect(datesBlock).toHaveTextContent(testString);
71+
expect(datesBlock.childElementCount).toBe(2);
72+
73+
const date20 = screen.getByRole('button', { name: '20' });
74+
await user.click(date20);
75+
testString = new Date(2022, 9, 20).toDateString();
76+
expect(valueBlock).toHaveTextContent(testString);
77+
expect(datesBlock).toHaveTextContent(testString);
78+
expect(datesBlock.childElementCount).toBe(2);
79+
});
5280
});

0 commit comments

Comments
 (0)