Skip to content

Commit 9cb6747

Browse files
committed
test: add tests for AddCommentFormView
Part of #1489
1 parent dc5ed88 commit 9cb6747

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

Diff for: src/main/frontend/src/components/AddCommentForm.js

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// IMPORTANT:
33
// You must update ResourceUrl.RESOURCES_VERSION each time whenever you're modified this file!
44
//
5+
import React from 'react';
56

67
// @todo #1338 AddCommentForm: add tests
78
class AddCommentForm extends React.PureComponent {
@@ -138,3 +139,4 @@ class AddCommentFormView extends React.PureComponent {
138139
}
139140

140141
window.AddCommentForm = AddCommentForm;
142+
window.AddCommentFormView = AddCommentFormView;
+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import React from 'react';
2+
import { render, screen } from '@testing-library/react'
3+
import '@testing-library/jest-dom/extend-expect'
4+
import './AddCommentForm.js'
5+
6+
describe('AddCommentFormView', () => {
7+
8+
it('renders the form', () => {
9+
// given
10+
// when
11+
render(
12+
<AddCommentFormView />
13+
);
14+
// then
15+
const commentField = screen.getByLabelText('Comment');
16+
expect(commentField).toBeRequired();
17+
18+
const submitButton = screen.getByRole('button', {name: 'Add'});
19+
expect(submitButton).toBeEnabled();
20+
});
21+
22+
it('disables the submit button', () => {
23+
// given
24+
// when
25+
render(
26+
<AddCommentFormView isDisabled={ true } />
27+
);
28+
// then
29+
const submitButton = screen.getByRole('button', {name: 'Add'});
30+
expect(submitButton).toBeDisabled();
31+
});
32+
33+
describe('shows an error for', () => {
34+
35+
it('a whole form', () => {
36+
// given
37+
// when
38+
render(
39+
<AddCommentFormView hasServerError={ true } />
40+
);
41+
// then
42+
const alert = screen.getByRole('alert');
43+
expect(alert).toHaveTextContent('Server error');
44+
expect(alert).not.toHaveClass('hidden');
45+
});
46+
47+
it('the comment field', () => {
48+
// given
49+
// when
50+
const { container } = render(
51+
<AddCommentFormView validationErrors={ [ 'err1', 'err2' ] } />
52+
);
53+
// then
54+
const form = screen.getByRole('form', {name: 'Add a comment'});
55+
expect(form).not.toBeNull();
56+
expect(form).toHaveClass('has-error');
57+
58+
// @todo #1489 Use toHaveErrorMessage() or toHaveDescription() for checking error messages
59+
const fieldErrors = container.querySelector('#new-comment\\.errors');
60+
expect(fieldErrors).not.toBeNull();
61+
expect(fieldErrors).not.toHaveClass('hidden');
62+
expect(fieldErrors).toHaveTextContent('err1, err2');
63+
});
64+
});
65+
66+
});

0 commit comments

Comments
 (0)