Skip to content

Commit 639d979

Browse files
committed
feat: suggestion test cases
1 parent c50dad4 commit 639d979

File tree

8 files changed

+199
-7
lines changed

8 files changed

+199
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { render } from '@testing-library/react';
2+
import { Header } from './index';
3+
4+
describe('Header', () => {
5+
it('renders category and description', () => {
6+
const category = 'Category';
7+
const description = 'Description';
8+
const { getByTestId } = render(<Header category={category} description={description} />);
9+
10+
expect(getByTestId('category')).toBeInTheDocument();
11+
expect(getByTestId('category')).toHaveTextContent('Problem Category:');
12+
expect(getByTestId('category')).toHaveTextContent('Category');
13+
14+
expect(getByTestId('description')).toBeInTheDocument();
15+
expect(getByTestId('description')).toHaveTextContent('Problem Description:');
16+
expect(getByTestId('description')).toHaveTextContent('Description');
17+
});
18+
19+
it('renders only category', () => {
20+
const category = 'Category';
21+
const { getByTestId, queryByTestId } = render(<Header category={category} />);
22+
23+
expect(getByTestId('category')).toBeInTheDocument();
24+
expect(getByTestId('category')).toHaveTextContent('Problem Category:');
25+
expect(getByTestId('category')).toHaveTextContent('Category');
26+
27+
expect(queryByTestId('description')).toBeNull();
28+
});
29+
30+
it('renders only description', () => {
31+
const description = 'Description';
32+
const { getByTestId, queryByTestId } = render(<Header description={description} />);
33+
34+
expect(queryByTestId('category')).toBeNull();
35+
36+
expect(getByTestId('description')).toBeInTheDocument();
37+
expect(getByTestId('description')).toHaveTextContent('Problem Description:');
38+
expect(getByTestId('description')).toHaveTextContent('Description');
39+
});
40+
41+
it('does not render anything when neither category nor description is provided', () => {
42+
const { queryByTestId } = render(<Header />);
43+
44+
expect(queryByTestId('category')).toBeNull();
45+
expect(queryByTestId('description')).toBeNull();
46+
});
47+
});

src/components/Sugggestion/Header/index.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export const Header = ({ category, description }: HeaderProps): JSX.Element => {
99
return (
1010
<>
1111
{category && (
12-
<Box width='100%'>
12+
<Box width='100%' data-testid='category'>
1313
<Typography component='span' variant='subtitle1' color='whitesmoke' fontWeight={600}>
1414
Problem Category:
1515
</Typography>{' '}
@@ -20,7 +20,7 @@ export const Header = ({ category, description }: HeaderProps): JSX.Element => {
2020
)}
2121

2222
{description && (
23-
<Box width='100%'>
23+
<Box width='100%' data-testid='description'>
2424
<Typography variant='subtitle1' component='span' fontWeight={600} color='whitesmoke'>
2525
Problem Description:
2626
</Typography>{' '}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import { fireEvent, render } from '@testing-library/react';
2+
import { RecommendationPagination, RecommendationPaginationProps } from './index';
3+
4+
describe('RecommendationPagination', () => {
5+
it('renders pagination buttons when shouldRenderPagination is true', () => {
6+
const props: RecommendationPaginationProps = {
7+
gotoNextPage: jest.fn(),
8+
gotoPreviousPage: jest.fn(),
9+
handleApplyRecommendation: jest.fn(),
10+
shouldRenderPagination: true,
11+
currentPage: 1,
12+
totalPages: 5,
13+
};
14+
const { getByTestId } = render(<RecommendationPagination {...props} />);
15+
const applyButton = getByTestId('apply-button');
16+
const gotoPreviousButton = getByTestId('goto-previous-button');
17+
const gotoNextButton = getByTestId('goto-next-button');
18+
19+
expect(applyButton).toBeInTheDocument();
20+
expect(gotoPreviousButton).toBeInTheDocument();
21+
expect(gotoNextButton).toBeInTheDocument();
22+
});
23+
24+
it('does not render pagination buttons when shouldRenderPagination is false', () => {
25+
const props: RecommendationPaginationProps = {
26+
gotoNextPage: jest.fn(),
27+
gotoPreviousPage: jest.fn(),
28+
handleApplyRecommendation: jest.fn(),
29+
shouldRenderPagination: false,
30+
currentPage: 1,
31+
totalPages: 5,
32+
};
33+
34+
const { getByTestId, queryByTestId } = render(<RecommendationPagination {...props} />);
35+
36+
expect(getByTestId('apply-button')).toBeInTheDocument();
37+
expect(queryByTestId('goto-previous-button')).toBeNull();
38+
expect(queryByTestId('goto-next-button')).toBeNull();
39+
});
40+
41+
it('calls gotoNextPage when Next Page button is clicked', () => {
42+
const gotoNextPageMock = jest.fn();
43+
const props: RecommendationPaginationProps = {
44+
gotoNextPage: gotoNextPageMock,
45+
gotoPreviousPage: jest.fn(),
46+
handleApplyRecommendation: jest.fn(),
47+
shouldRenderPagination: true,
48+
currentPage: 1,
49+
totalPages: 5,
50+
};
51+
const { getByTestId } = render(<RecommendationPagination {...props} />);
52+
53+
const gotoNextButton = getByTestId('goto-next-button');
54+
fireEvent.click(gotoNextButton);
55+
expect(gotoNextPageMock).toHaveBeenCalled();
56+
});
57+
58+
it('calls gotoPreviousPage when Previous Page button is clicked', () => {
59+
const gotoPreviousPageMock = jest.fn();
60+
const props: RecommendationPaginationProps = {
61+
gotoNextPage: jest.fn(),
62+
gotoPreviousPage: gotoPreviousPageMock,
63+
handleApplyRecommendation: jest.fn(),
64+
shouldRenderPagination: true,
65+
currentPage: 1,
66+
totalPages: 5,
67+
};
68+
const { getByTestId } = render(<RecommendationPagination {...props} />);
69+
70+
const gotoPreviousButton = getByTestId('goto-previous-button');
71+
fireEvent.click(gotoPreviousButton);
72+
expect(gotoPreviousPageMock).toHaveBeenCalled();
73+
});
74+
75+
it('calls handleApplyRecommendation when Apply button is clicked', () => {
76+
const handleApplyRecommendationMock = jest.fn();
77+
const props: RecommendationPaginationProps = {
78+
gotoNextPage: jest.fn(),
79+
gotoPreviousPage: jest.fn(),
80+
handleApplyRecommendation: handleApplyRecommendationMock,
81+
shouldRenderPagination: true,
82+
currentPage: 1,
83+
totalPages: 5,
84+
};
85+
86+
const { getByTestId } = render(<RecommendationPagination {...props} />);
87+
88+
const applyButton = getByTestId('apply-button');
89+
fireEvent.click(applyButton);
90+
expect(handleApplyRecommendationMock).toHaveBeenCalled();
91+
});
92+
});

src/components/Sugggestion/Pagination/index.tsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Box, Button, ButtonGroup, SxProps } from '@mui/material';
22
import { useMemo } from 'react';
33

4-
interface RecommendationPaginationProps {
4+
export interface RecommendationPaginationProps {
55
gotoNextPage: () => void;
66
gotoPreviousPage: () => void;
77
handleApplyRecommendation: () => void;
@@ -65,6 +65,7 @@ export const RecommendationPagination = ({
6565
}}
6666
>
6767
<Button
68+
data-testid='goto-previous-button'
6869
color='primary'
6970
onClick={gotoPreviousPage}
7071
disabled={isLeftArrowDisabled}
@@ -84,6 +85,7 @@ export const RecommendationPagination = ({
8485
</svg>
8586
</Button>
8687
<Button
88+
data-testid='goto-next-button'
8789
color='primary'
8890
onClick={gotoNextPage}
8991
disabled={isRightArrowDisabled}
@@ -106,6 +108,7 @@ export const RecommendationPagination = ({
106108
)}
107109

108110
<Button
111+
data-testid='apply-button'
109112
variant='contained'
110113
sx={{
111114
justifyContent: 'flex-end',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { RenderResult, render } from '@testing-library/react';
2+
import { RecommendationSkeletonLoader } from './Skeleton';
3+
4+
describe('RecommendationSkeletonLoader', () => {
5+
it('renders without crashing', () => {
6+
const { getByTestId }: RenderResult = render(<RecommendationSkeletonLoader />);
7+
const skeletonElement = getByTestId('recommendation-skeleton');
8+
expect(skeletonElement).toBeInTheDocument();
9+
});
10+
11+
it('renders a Skeleton component with 100% width', () => {
12+
const { getByTestId }: RenderResult = render(<RecommendationSkeletonLoader />);
13+
const skeletonElement = getByTestId('recommendation-skeleton');
14+
expect(skeletonElement).toBeInTheDocument();
15+
expect(skeletonElement).toHaveStyle('width: 100%');
16+
});
17+
18+
it('renders a Skeleton component with 100px height', () => {
19+
const { getByTestId }: RenderResult = render(<RecommendationSkeletonLoader />);
20+
const skeletonElement = getByTestId('recommendation-skeleton');
21+
expect(skeletonElement).toBeInTheDocument();
22+
expect(skeletonElement).toHaveStyle('height: 100px');
23+
});
24+
});

src/components/Sugggestion/Recommendation/Skeleton.tsx

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import { Skeleton } from '@mui/material';
22

33
export const RecommendationSkeletonLoader = (): JSX.Element => {
44
return (
5-
<>
6-
<Skeleton variant='rounded' width='100%' height='100px' />
7-
</>
5+
<Skeleton data-testid='recommendation-skeleton' variant='rounded' width='100%' height='100px' />
86
);
97
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { render } from '@testing-library/react';
2+
import { Recommendation } from './index';
3+
4+
describe('Recommendation', () => {
5+
it('renders without crashing', () => {
6+
const text = 'This is a sample text';
7+
const { getByText } = render(<Recommendation text={text} />);
8+
const textElement = getByText(text);
9+
expect(textElement).toBeInTheDocument();
10+
});
11+
12+
it('renders Markdown text correctly', () => {
13+
const text = 'This is a sample text';
14+
const { getByTestId } = render(<Recommendation text={text} />);
15+
const markdownTextElement = getByTestId('markdown-text');
16+
expect(markdownTextElement).toBeInTheDocument();
17+
expect(markdownTextElement).toHaveTextContent(`~~~${text}~~~`);
18+
});
19+
20+
it('renders SyntaxHighlighter for code blocks', () => {
21+
const codeText = 'console.log("Hello, world!")';
22+
const { getByTestId } = render(<Recommendation text={codeText} />);
23+
const codeBlockElement = getByTestId('code-block');
24+
expect(codeBlockElement).toBeInTheDocument();
25+
expect(codeBlockElement).toHaveTextContent(codeText);
26+
});
27+
});

src/components/Sugggestion/Recommendation/index.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@ export const Recommendation = ({ text }: RecommendationProps): JSX.Element => {
2121

2222
return !inline && match ? (
2323
<SyntaxHighlighter
24+
data-testid='code-block'
2425
{...props}
2526
children={String(children).replace(/\n$/, '')}
2627
style={dark}
2728
language={match[1]}
2829
PreTag='div'
2930
/>
3031
) : (
31-
<code {...props} className={className}>
32+
<code data-testid='markdown-text' {...props} className={className}>
3233
{children}
3334
</code>
3435
);

0 commit comments

Comments
 (0)