Skip to content

Commit a015d58

Browse files
committed
test: add basic testing
1 parent d2a285c commit a015d58

21 files changed

Lines changed: 267 additions & 112 deletions

packages/react-use-calendar-component/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ This project is aimed to provide an easy-to-use API interface for those people w
66

77
- [x] Generate 42 date cell information
88
- [x] Change displayed year/month when selected date is change
9-
- [ ] Support selecting with range
10-
- [ ] Select multiple dates
9+
- [x] Select multiple dates
10+
- [ ] Select with range
1111
- [ ] Support disabled date
1212
- [ ] Support displaying two calendar
1313
- [ ] Support week, month, year selection

packages/react-use-calendar-component/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"devDependencies": {
2525
"@testing-library/jest-dom": "^5.16.5",
2626
"@testing-library/react": "^13.4.0",
27+
"@testing-library/user-event": "14.4.3",
2728
"@types/jest": "^29.0.3",
2829
"@types/react": "^18.0.20",
2930
"@types/react-dom": "^18.0.6",

packages/react-use-calendar-component/src/app/App.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { useState } from 'react';
22

3-
import Multiple from './components/Examples/Multiple';
4-
import Single from './components/Examples/Single';
3+
import { Single, Multiple } from './components/Examples';
54

65
type ExampleType = 'single' | 'multiple';
76

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

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,9 @@ export default function CalendarBody({ dateCellInfos }: Props) {
1919
))}
2020
</div>
2121
<div className='grid grid-cols-7 gap-y-4'>
22-
{dateCellInfos.map(
23-
({
24-
key,
25-
monthStatus,
26-
isSelected,
27-
isToday,
28-
dayOfMonth,
29-
selectThisDate,
30-
}) => {
31-
let className = 'rounded-full';
32-
if (monthStatus !== 'current')
33-
className = `${className} text-slate-300`;
34-
if (isSelected) className = `${className} bg-green-200`;
35-
if (isToday && !isSelected) className = `${className} bg-amber-200`;
36-
if (!isToday && !isSelected)
37-
className = `${className} hover:bg-stone-100`;
38-
39-
return (
40-
<CalendarCell
41-
key={key}
42-
onClick={selectThisDate}
43-
className={className}
44-
dayOfMonth={dayOfMonth}
45-
/>
46-
);
47-
}
48-
)}
22+
{dateCellInfos.map(({ key, ...info }) => (
23+
<CalendarCell key={key} {...info} />
24+
))}
4925
</div>
5026
</div>
5127
);

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

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
1-
interface Props {
2-
className: string;
3-
onClick: () => void;
4-
dayOfMonth: number;
5-
}
1+
import type { DateCellInfo } from '../../lib/types';
62

73
export default function CalendarCell({
8-
className,
9-
onClick,
4+
monthStatus,
5+
isSelected,
6+
isToday,
107
dayOfMonth,
11-
}: Props) {
8+
selectThisDate,
9+
}: DateCellInfo) {
10+
const title = isToday ? 'today' : undefined;
11+
let className = 'rounded-full';
12+
if (monthStatus !== 'current') className = `${className} text-slate-300`;
13+
if (isSelected) className = `${className} bg-green-200 selected`;
14+
if (isToday && !isSelected) className = `${className} bg-amber-200`;
15+
if (!isToday && !isSelected) className = `${className} hover:bg-stone-100`;
16+
1217
return (
13-
<button className={className} onClick={onClick}>
18+
<button
19+
className={`${className} ${monthStatus}`}
20+
onClick={() => selectThisDate()}
21+
type='button'
22+
title={title}>
1423
<div className='flex items-center justify-center'>
1524
<span>{dayOfMonth}</span>
1625
</div>

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,22 @@ export default function CalendarHeader({
1919
}: Props) {
2020
return (
2121
<div className='flex justify-between items-center'>
22-
<LeftArrow arrowAmount={2} onClick={() => onYearClick(-1)} />
23-
<LeftArrow onClick={() => onMonthClick(-1)} />
22+
<LeftArrow
23+
arrowAmount={2}
24+
onClick={() => onYearClick(-1)}
25+
ariaLabel='sub-year'
26+
/>
27+
<LeftArrow onClick={() => onMonthClick(-1)} ariaLabel='sub-month' />
2428
<div className='flex flex-col justify-center items-center'>
2529
<span className='text-lg'>{year}</span>
2630
<span className='text-sm'>{monthList[month]}</span>
2731
</div>
28-
<RightArrow onClick={() => onMonthClick(1)} />
29-
<RightArrow arrowAmount={2} onClick={() => onYearClick(1)} />
32+
<RightArrow onClick={() => onMonthClick(1)} ariaLabel='add-month' />
33+
<RightArrow
34+
arrowAmount={2}
35+
onClick={() => onYearClick(1)}
36+
ariaLabel='add-year'
37+
/>
3038
</div>
3139
);
3240
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { useEffect } from 'react';
33
import useCalendarComponent from '../../../lib';
44
import Calendar from '../Calendar';
55

6-
export default function Multiple() {
6+
export function Multiple() {
77
const calendarControl = useCalendarComponent({ selectType: 'multiple' });
88
const { selectedDates, value } = calendarControl;
99
// const { year, month, dayOfMonth } = selectedDate;

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
1-
import { useEffect } from 'react';
2-
31
import useCalendarComponent from '../../../lib';
42
import Calendar from '../Calendar';
53

6-
export default function Single() {
4+
export function Single() {
75
const calendarControl = useCalendarComponent();
8-
const { selectedDates, value } = calendarControl;
9-
// const { year, month, dayOfMonth } = selectedDate;
10-
11-
useEffect(() => {
12-
console.log('value:', value);
13-
}, [value]);
6+
const { selectedDates } = calendarControl;
147

158
return (
169
<div>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './Single';
2+
export * from './Multiple';

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
export default function LeftArrow({
22
arrowAmount = 1,
33
onClick,
4+
ariaLabel,
45
}: {
56
arrowAmount?: number;
67
onClick: () => void;
8+
ariaLabel: string;
79
}) {
810
return (
911
<button
12+
aria-label={ariaLabel}
1013
onClick={onClick}
1114
className='w-10 h-10 flex justify-center items-center'>
1215
{[...Array(arrowAmount)].map((_, index) => (

0 commit comments

Comments
 (0)