Skip to content
This repository has been archived by the owner on Dec 26, 2024. It is now read-only.

Commit

Permalink
feat: add CalendarCarousel test code
Browse files Browse the repository at this point in the history
  • Loading branch information
sunjungAn committed Oct 22, 2023
1 parent 2e7da04 commit 4e1c593
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 32 deletions.
29 changes: 17 additions & 12 deletions packages/CalendarCarousel/CalendarItem/DateItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,18 @@ export type DateItemProp = {
styles?: DateItemStyles;
};

export default function DateItem(props: DateItemProp): JSX.Element {
export default function DateItem({
isEvent,
dateObject,
calendarWidth,
onPress,
selected = false,
styles,
showNextDates = true,
showPrevDates = true,
}: DateItemProp): JSX.Element {
const {theme} = useTheme();

const {
isEvent,
dateObject,
calendarWidth,
onPress,
selected = false,
styles,
showNextDates = true,
showPrevDates = true,
} = props;

const {isNextMonth, isPrevMonth} = dateObject;
const dateWidth = calendarWidth - 24;
const dayHeight = dateWidth / 7;
Expand Down Expand Up @@ -90,6 +88,13 @@ export default function DateItem(props: DateItemProp): JSX.Element {
},
isEvent && styles?.event,
]}
testID={
isPrevMonth
? 'prev-dateItem'
: isNextMonth
? 'next-dateItem'
: 'dateItem'
}
>
<Text
numberOfLines={1}
Expand Down
34 changes: 16 additions & 18 deletions packages/CalendarCarousel/CalendarItem/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,17 @@ interface CalendarItemProp {
}) => JSX.Element;
}

export default function CalendarItem(props: CalendarItemProp): JSX.Element {
const {
dateObjects,
selectedDateObject,
width,
events,
showPrevDates = false,
showNextDates = false,
styles,
onDatePress,
renderDate,
} = props;

export default function CalendarItem({
dateObjects,
selectedDateObject,
width,
events,
showPrevDates = false,
showNextDates = false,
styles,
onDatePress,
renderDate,
}: CalendarItemProp): JSX.Element {
const onPressDateItem = (date: DateObject): void => {
onDatePress?.(date);
};
Expand Down Expand Up @@ -77,14 +75,14 @@ export default function CalendarItem(props: CalendarItemProp): JSX.Element {

return (
<DateItem
calendarWidth={width}
dateObject={dateObj}
isEvent={events?.includes(dateObj.date)}
showPrevDates={showPrevDates}
showNextDates={showNextDates}
selected={selected}
key={`${dateObj}-${i}`}
dateObject={dateObj}
calendarWidth={width}
onPress={onPressDateItem}
selected={selected}
showNextDates={showNextDates}
showPrevDates={showPrevDates}
styles={styles}
/>
);
Expand Down
64 changes: 62 additions & 2 deletions packages/__tests__/CalendarCarousel.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type {ComponentProps} from 'react';
import React from 'react';
import React, {Component} from 'react';

Check warning on line 2 in packages/__tests__/CalendarCarousel.test.tsx

View workflow job for this annotation

GitHub Actions / build

'Component' is defined but never used. Allowed unused vars must match /^_/u
import {Text} from 'react-native';
import type {RenderAPI} from '@testing-library/react-native';
import {fireEvent, render} from '@testing-library/react-native';
import {fireEvent, render, screen} from '@testing-library/react-native';

Check warning on line 5 in packages/__tests__/CalendarCarousel.test.tsx

View workflow job for this annotation

GitHub Actions / build

'screen' is defined but never used. Allowed unused vars must match /^_/u
import {ko} from 'date-fns/locale';

import {createComponent, createTestProps} from '../../test/testUtils';
import CalendarCarousel from '../CalendarCarousel';
Expand Down Expand Up @@ -41,3 +42,62 @@ describe('[Calendar] render test', () => {
expect(clickMock).toBeCalledTimes(1);
});
});

describe('[Calendar-WeekdayItem] render', () => {
it('change with ko locale', () => {
props = createTestProps({
locale: ko,
});
component = createComponent(<CalendarCarousel {...props} />);

testingLib = render(component);

const json = testingLib.toJSON();
expect(json).toBeTruthy();
});
});

describe('[Calendar-MonthHeader] render', () => {
it('render MonthHeader with another component', () => {
props = createTestProps({
renderHeader: (value: Date) => (
<Text testID="test-render-header">{value.toString()}</Text>
),
});

component = createComponent(<CalendarCarousel {...props} />);

testingLib = render(component);

const changeHeader = testingLib.getByTestId('test-render-header');
expect(changeHeader).toBeTruthy();
});
});

describe('[Calender-CalendarItem] render', () => {
it('should hide prev date item', () => {
props = createTestProps({
showPrevDates: false,
});

component = createComponent(<CalendarCarousel {...props} />);
testingLib = render(component);

const prevDates = testingLib.getAllByTestId('prev-dateItem');

expect(prevDates.length).toBe(0);
});

it('should hide next date item', () => {
props = createTestProps({
showNextPrevDates: false,
});

component = createComponent(<CalendarCarousel {...props} />);
testingLib = render(component);

const nextDates = testingLib.getAllByTestId('next-dateItem');

expect(nextDates.length).toBe(0);
});
});

0 comments on commit 4e1c593

Please sign in to comment.