-
Notifications
You must be signed in to change notification settings - Fork 81
/
post-editor-03-markup.test.js
54 lines (43 loc) · 1.45 KB
/
post-editor-03-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
import 'jest-dom/extend-expect';
import 'react-testing-library/cleanup-after-each';
import React from 'react';
import {render, fireEvent} from 'react-testing-library';
// import our api call so that we can assert on it
import {savePost as mockSavePost} from '../src/api';
import {Editor} from '../src/post-editor-03-markup';
// mock out our api call
jest.mock('../src/api', () => ({
savePost: jest.fn(subject => Promise.resolve({success: true})),
}));
// clear our mock after each test so that subsequent calls don't interfere in
// other tests
afterEach(() => {
mockSavePost.mockClear();
});
describe('Editor', () => {
test('renders with correct fields and submit button', () => {
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,
});
});
});