Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue8: Create test cases for current components #14

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"test": "react-scripts test --silent --transformIgnorePatterns 'node_modules/(?!@uiw/react-textarea-code-editor)/'",
"eject": "react-scripts eject"
},
"eslintConfig": {
Expand Down
1 change: 1 addition & 0 deletions src/Components/CodeDisplayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const CodeDisplayer = ({ code }) => {
<div class="output-container">
<CodeEditor
data-color-mode="dark"
data-testid="code-output"
id="code-output"
value={code}
language="jsx"
Expand Down
13 changes: 13 additions & 0 deletions src/Components/CodeDisplayer.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from 'react';
import { render } from '@testing-library/react';
import CodeDisplayer from './CodeDisplayer';

describe('CodeDisplayer', () => {
it('should render code editor with given code', () => {
const code = '<div>Hello World</div>';
const { getByTestId } = render(<CodeDisplayer code={code} />);
const codeEditor = getByTestId('code-output');
expect(codeEditor).toBeInTheDocument();
expect(codeEditor.value).toBe(code);
});
});
60 changes: 60 additions & 0 deletions src/Components/CodeInputer.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import React from 'react';
import { render, fireEvent, waitFor } from '@testing-library/react';
import CodeInputer from './CodeInputer';

describe('CodeInputer', () => {
const onSubmitMock = jest.fn();

it('should render CodeEditor and button', () => {
const { getByPlaceholderText, getByRole } = render(
<CodeInputer onSubmit={onSubmitMock} />
);
expect(getByPlaceholderText('Enter your code here')).toBeInTheDocument();
expect(getByRole('button', { name: /Submit/i })).toBeInTheDocument();
});

it('should call onSubmit with response message on form submit', async () => {
global.fetch = jest.fn(() =>
Promise.resolve({
json: () =>
Promise.resolve({
choices: [
{
message: {
content: 'Generated Jest test case using react-testing-library!'
}
}
]
})
})
);

const { getByPlaceholderText, getByRole } = render(
<CodeInputer onSubmit={onSubmitMock} />
);

fireEvent.change(getByPlaceholderText('Enter your code here'), {
target: { value: 'console.log("Hello World!");' }
});
fireEvent.click(getByRole('button', { name: /Submit/i }));

expect(getByRole('button', { name: /Loading.../i })).toBeInTheDocument();

await waitFor(() => {
expect(global.fetch).toHaveBeenCalledWith(
'https://api.openai.com/v1/chat/completions',
expect.objectContaining({
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: expect.any(String)
}
})
);
});

expect(onSubmitMock).toHaveBeenCalledWith(
'Generated Jest test case using react-testing-library!'
);
});
});