-
Notifications
You must be signed in to change notification settings - Fork 81
/
post-editor-04-markup.test.js
78 lines (63 loc) · 2.48 KB
/
post-editor-04-markup.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import 'jest-dom/extend-expect';
import 'react-testing-library/cleanup-after-each';
import React from 'react';
import {render, fireEvent, wait} from 'react-testing-library';
import {savePost as mockSavePost} from '../src/api';
// import Redirect from react-router-dom so we can assert on it
import {Redirect as MockRedirect} from 'react-router-dom';
import {Editor} from '../src/post-editor-04-markup';
jest.mock('../src/api', () => ({
savePost: jest.fn(subject => Promise.resolve({success: true})),
}));
// mock out Redirect so that we can assert on it
jest.mock('react-router-dom', () => ({
Redirect: jest.fn(() => null),
}));
afterEach(() => {
// clear the redirect after each test
MockRedirect.mockClear();
mockSavePost.mockClear();
});
describe('Editor', () => {
// make our test async because we need to wait for a promise to resolve inside
// the submit handler
test('renders with correct fields and submit button', async () => {
const fakeUser = {id: 'user-1'};
const payload = {
content: 'foobar',
tags: ['tag1', 'tag2'],
title: 'foo',
};
const {container, getByLabelText, getByText} = render(
<Editor user={fakeUser} />
);
const titleInput = getByLabelText(/title/i);
const contentInput = getByLabelText(/content/i);
const tagsInput = getByLabelText(/tags/i);
titleInput.value = payload.title;
contentInput.value = payload.content;
tagsInput.value = payload.tags.join(', ');
const button = getByText(/submit/i);
fireEvent.click(button);
expect(button).toBeDisabled();
expect(mockSavePost).toHaveBeenCalledTimes(1);
expect(mockSavePost).toHaveBeenCalledWith({
...payload,
authorId: fakeUser.id,
});
await wait(() => {
// Assert that our mocked redirect call actually happened
// Each call inside wait increases the time tests take to run, because
// wait will keep executing every 15ms for 4s until either the test passes
// or fails for each assertion inside the callback
// The more tests you have inside `wait` the longer you will have to wait
// for tests to fail if any of them fail
expect(MockRedirect).toHaveBeenCalledTimes(1);
// This failing test will result in this callback holding up feedback in
// Jest for 8s
// expect(MockRedirect).toHaveBeenCalledTimes(2);
});
// assert that our redirect was called with the correct path for redirection
expect(MockRedirect).toHaveBeenCalledWith({to: '/'}, {});
});
});