Skip to content

Commit bc615d9

Browse files
committed
feat: suggestion webview tests
1 parent 639d979 commit bc615d9

File tree

5 files changed

+110
-20
lines changed

5 files changed

+110
-20
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { render, fireEvent } from '@testing-library/react';
2+
import { Feedback } from './index';
3+
4+
describe('Feedback', () => {
5+
it('renders correctly', () => {
6+
const handleDiscardClick = jest.fn();
7+
const handleEndorseClick = jest.fn();
8+
const { getByTestId } = render(
9+
<Feedback handleDiscardClick={handleDiscardClick} handleEndorseClick={handleEndorseClick} />,
10+
);
11+
const discardButton = getByTestId('discard-button');
12+
const endorseButton = getByTestId('endorse-button');
13+
expect(discardButton).toBeInTheDocument();
14+
expect(endorseButton).toBeInTheDocument();
15+
});
16+
17+
it('calls handleDiscardClick on Discard button click', () => {
18+
const handleDiscardClick = jest.fn();
19+
const handleEndorseClick = jest.fn();
20+
const { getByTestId } = render(
21+
<Feedback handleDiscardClick={handleDiscardClick} handleEndorseClick={handleEndorseClick} />,
22+
);
23+
const discardButton = getByTestId('discard-button');
24+
fireEvent.click(discardButton);
25+
expect(handleDiscardClick).toHaveBeenCalledTimes(1);
26+
expect(handleEndorseClick).not.toHaveBeenCalled();
27+
});
28+
29+
it('calls handleEndorseClick on Endorse button click', () => {
30+
const handleDiscardClick = jest.fn();
31+
const handleEndorseClick = jest.fn();
32+
const { getByTestId } = render(
33+
<Feedback handleDiscardClick={handleDiscardClick} handleEndorseClick={handleEndorseClick} />,
34+
);
35+
const endorseButton = getByTestId('endorse-button');
36+
fireEvent.click(endorseButton);
37+
expect(handleEndorseClick).toHaveBeenCalledTimes(1);
38+
expect(handleDiscardClick).not.toHaveBeenCalled();
39+
});
40+
});

src/components/Sugggestion/Feedback/index.tsx

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

4-
interface FeedbackProps {
4+
export interface FeedbackProps {
55
handleDiscardClick: React.MouseEventHandler<HTMLButtonElement>;
66
handleEndorseClick: React.MouseEventHandler<HTMLButtonElement>;
77
}
@@ -13,6 +13,7 @@ export const Feedback = ({
1313
<>
1414
<ButtonGroup variant='outlined' size='large' aria-label='large button group'>
1515
<Button
16+
data-testid='discard-button'
1617
onClick={handleDiscardClick}
1718
variant='contained'
1819
color='primary'
@@ -21,6 +22,7 @@ export const Feedback = ({
2122
Discard
2223
</Button>
2324
<Button
25+
data-testid='endorse-button'
2426
onClick={handleEndorseClick}
2527
variant='contained'
2628
color='primary'

src/components/Sugggestion/Pagination/index.spec.tsx

+36
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,40 @@ describe('RecommendationPagination', () => {
8989
fireEvent.click(applyButton);
9090
expect(handleApplyRecommendationMock).toHaveBeenCalled();
9191
});
92+
93+
it('gotoNextPage Button is Disabled', () => {
94+
const handleApplyRecommendationMock = jest.fn();
95+
const props: RecommendationPaginationProps = {
96+
gotoNextPage: jest.fn(),
97+
gotoPreviousPage: jest.fn(),
98+
handleApplyRecommendation: handleApplyRecommendationMock,
99+
shouldRenderPagination: true,
100+
currentPage: 5,
101+
totalPages: 5,
102+
};
103+
104+
const { getByTestId } = render(<RecommendationPagination {...props} />);
105+
106+
const gotoNextButton = getByTestId('goto-next-button');
107+
expect(gotoNextButton).toBeInTheDocument();
108+
expect(gotoNextButton).toHaveAttribute('disabled');
109+
});
110+
111+
it('gotoPrevPage Button is Disabled', () => {
112+
const handleApplyRecommendationMock = jest.fn();
113+
const props: RecommendationPaginationProps = {
114+
gotoNextPage: jest.fn(),
115+
gotoPreviousPage: jest.fn(),
116+
handleApplyRecommendation: handleApplyRecommendationMock,
117+
shouldRenderPagination: true,
118+
currentPage: 0,
119+
totalPages: 5,
120+
};
121+
122+
const { getByTestId } = render(<RecommendationPagination {...props} />);
123+
124+
const gotoPreviousButton = getByTestId('goto-previous-button');
125+
expect(gotoPreviousButton).toBeInTheDocument();
126+
expect(gotoPreviousButton).toHaveAttribute('disabled');
127+
});
92128
});

src/components/Sugggestion/Recommendation/index.spec.tsx

+2-18
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,9 @@ import { Recommendation } from './index';
33

44
describe('Recommendation', () => {
55
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', () => {
136
const text = 'This is a sample text';
147
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);
8+
const textElement = getByTestId('code-block');
9+
expect(textElement).toBeInTheDocument();
2610
});
2711
});

src/layout/Layout.spec.tsx

+29-1
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
import { render, fireEvent } from '@testing-library/react';
2-
import { RecoilRoot } from 'recoil';
2+
import { RecoilRoot, useRecoilValue } from 'recoil';
33

44
// Manually mock vscode
55
jest.mock('vscode');
66
import vscode from '../__mocks__/vscode';
77
import { Layout } from './Layout';
88
import * as State from '../state';
99
import { ApplicationWebviewState } from '../types';
10+
import { useEffect } from 'react';
1011

1112
(global as any).vscode = vscode;
1213

14+
export const RecoilObserver = ({ node, onChange }: any) => {
15+
const value = useRecoilValue(node);
16+
useEffect(() => onChange(value), [onChange, value]);
17+
return null;
18+
};
19+
1320
describe('Layout component', () => {
1421
test('renders correctly', () => {
1522
const { getByText, getByTestId } = render(
@@ -50,4 +57,25 @@ describe('Layout component', () => {
5057
},
5158
});
5259
});
60+
61+
test('back button click switches to analyze mode', () => {
62+
const onChange = jest.fn();
63+
64+
const { getByTestId, queryByTestId } = render(
65+
<RecoilRoot
66+
initializeState={({ set }) =>
67+
set(State.applicationState, ApplicationWebviewState.SUGGESTION_MODE)
68+
}
69+
>
70+
<RecoilObserver node={State.applicationState} onChange={onChange} />
71+
<Layout>
72+
<div>Child component</div>
73+
</Layout>
74+
</RecoilRoot>,
75+
);
76+
77+
expect(queryByTestId('analyze-mode-back-button')).toBeDefined();
78+
fireEvent.click(getByTestId('analyze-mode-back-button'));
79+
expect(onChange).toHaveBeenCalledWith(ApplicationWebviewState.ANALYZE_MODE);
80+
});
5381
});

0 commit comments

Comments
 (0)