Skip to content

Commit faafa79

Browse files
committed
Add test to demonstrate testing React components
1 parent 5a464a6 commit faafa79

File tree

6 files changed

+162
-1
lines changed

6 files changed

+162
-1
lines changed

jest.config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ module.exports = {
22
preset: 'ts-jest/presets/js-with-babel',
33
testEnvironment: 'jsdom',
44
collectCoverage: true,
5+
setupFilesAfterEnv: [
6+
'@testing-library/react/cleanup-after-each'
7+
],
58
// For some reason Jest is not measuring coverage without the below option.
69
// Unfortunately, despite `!(.test)`, it still measures coverage of test files as well:
7-
forceCoverageMatch: ['**/*!(.test).ts'],
10+
forceCoverageMatch: ['**/*!(.test).tsx?'],
811
// Since we're only measuring coverage for TypeScript (i.e. added with test infrastructure in place),
912
// we can be fairly strict. However, if you feel that something is not fit for coverage,
1013
// mention why in a comment and mark it as ignored:
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Edit mode should properly render the edit form 1`] = `
4+
<div>
5+
<form>
6+
<textarea>
7+
Arbitrary markdown
8+
</textarea>
9+
<button
10+
type="submit"
11+
>
12+
RENDER
13+
</button>
14+
,
15+
</form>
16+
</div>
17+
`;
18+
19+
exports[`should properly render markdown 1`] = `
20+
<div>
21+
<form>
22+
<p>
23+
Some
24+
<strong>
25+
awesome
26+
</strong>
27+
markdown
28+
</p>
29+
<button
30+
type="submit"
31+
>
32+
EDIT
33+
</button>
34+
</form>
35+
</div>
36+
`;

markdown/actErrorWorkaround.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* eslint-env jest */
2+
3+
/* istanbul ignore next [This is a test helper, so it doesn't need to be tested itself] */
4+
/**
5+
* This is a workaround for a bug that will be fixed in [email protected]
6+
*
7+
* The bug results in a warning being thrown about calls not being wrapped in `act()`
8+
* when a component calls `setState` twice.
9+
* More info about the issue: https://github.com/testing-library/react-testing-library/issues/281#issuecomment-480349256
10+
* The PR that will fix it: https://github.com/facebook/react/pull/14853
11+
*/
12+
export function workaroundActError () {
13+
const originalError = console.error
14+
beforeAll(() => {
15+
console.error = (...args) => {
16+
if (/Warning.*not wrapped in act/.test(args[0])) {
17+
return
18+
}
19+
originalError.call(console, ...args)
20+
}
21+
})
22+
23+
afterAll(() => {
24+
console.error = originalError
25+
})
26+
}

markdown/view.test.tsx

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/* eslint-env jest */
2+
import * as React from 'react'
3+
import {
4+
render,
5+
fireEvent
6+
} from '@testing-library/react'
7+
import { View } from './view'
8+
import { workaroundActError } from './actErrorWorkaround'
9+
10+
workaroundActError()
11+
12+
it('should properly render markdown', () => {
13+
const { container } = render(<View markdown='Some **awesome** markdown' onSave={jest.fn()}/>)
14+
15+
expect(container).toMatchSnapshot()
16+
})
17+
18+
describe('Edit mode', () => {
19+
it('should properly render the edit form', () => {
20+
const { container, getByRole } = render(<View markdown='Arbitrary markdown' onSave={jest.fn()}/>)
21+
22+
const editButton = getByRole('button')
23+
editButton.click()
24+
25+
expect(container).toMatchSnapshot()
26+
})
27+
28+
it('should call the onSave handler after saving the new content', () => {
29+
const mockHandler = jest.fn().mockReturnValue(Promise.resolve())
30+
const { getByRole, getByDisplayValue } = render(<View markdown='Arbitrary markdown' onSave={mockHandler}/>)
31+
32+
const editButton = getByRole('button')
33+
editButton.click()
34+
35+
const textarea = getByDisplayValue('Arbitrary markdown')
36+
fireEvent.change(textarea, { target: { value: 'Some _other_ markdown' } })
37+
38+
const renderButton = getByRole('button')
39+
renderButton.click()
40+
41+
expect(mockHandler.mock.calls.length).toBe(1)
42+
expect(mockHandler.mock.calls[0][0]).toBe('Some _other_ markdown')
43+
})
44+
})

package-lock.json

Lines changed: 51 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
"@babel/preset-env": "^7.4.4",
7878
"@babel/preset-react": "^7.0.0",
7979
"@babel/preset-typescript": "^7.3.3",
80+
"@testing-library/react": "^8.0.1",
8081
"@types/jest": "^24.0.12",
8182
"@types/rdflib": "^0.20.0",
8283
"@types/react": "^16.8.19",

0 commit comments

Comments
 (0)