|
| 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