Skip to content

Commit 18205fb

Browse files
author
Kent C. Dodds
committed
slight improvements
1 parent 70bccdf commit 18205fb

File tree

4 files changed

+10
-8
lines changed

4 files changed

+10
-8
lines changed

src/__tests__/tdd-07-error-state.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ test('renders an error message from the server', async () => {
7272
const testError = 'test error'
7373
mockSavePost.mockRejectedValueOnce({data: {error: testError}})
7474
const fakeUser = userBuilder()
75-
const {getByLabelText, getByTestId, getByText} = render(
75+
const {getByLabelText, getByText, getByTestId} = render(
7676
<Editor user={fakeUser} />,
7777
)
7878
const fakePost = postBuilder()
@@ -86,4 +86,5 @@ test('renders an error message from the server', async () => {
8686

8787
const postError = await waitForElement(() => getByTestId('post-error'))
8888
expect(postError).toHaveTextContent(testError)
89+
expect(submitButton).not.toBeDisabled()
8990
})

src/__tests__/tdd-08-custom-render.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,5 @@ test('renders an error message from the server', async () => {
8585

8686
const postError = await waitForElement(() => getByTestId('post-error'))
8787
expect(postError).toHaveTextContent(testError)
88+
expect(submitButton).not.toBeDisabled()
8889
})

src/post-editor-07-error-state.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {Redirect} from 'react-router'
33
import {savePost} from './api'
44

55
class Editor extends React.Component {
6-
state = {isSaving: false, redirect: false}
6+
state = {isSaving: false, redirect: false, error: null}
77
handleSubmit = e => {
88
e.preventDefault()
99
const {title, content, tags} = e.target.elements
@@ -24,9 +24,6 @@ class Editor extends React.Component {
2424
if (this.state.redirect) {
2525
return <Redirect to="/" />
2626
}
27-
if (this.state.error) {
28-
return <div data-testid="post-error">{this.state.error}</div>
29-
}
3027
return (
3128
<form onSubmit={this.handleSubmit}>
3229
<label htmlFor="title-input">Title</label>
@@ -41,6 +38,9 @@ class Editor extends React.Component {
4138
<button type="submit" disabled={this.state.isSaving}>
4239
Submit
4340
</button>
41+
{this.state.error ? (
42+
<div data-testid="post-error">{this.state.error}</div>
43+
) : null}
4444
</form>
4545
)
4646
}

src/post-editor-08-custom-render.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ class Editor extends React.Component {
2424
if (this.state.redirect) {
2525
return <Redirect to="/" />
2626
}
27-
if (this.state.error) {
28-
return <div data-testid="post-error">{this.state.error}</div>
29-
}
3027
return (
3128
<form onSubmit={this.handleSubmit}>
3229
<label htmlFor="title-input">Title</label>
@@ -41,6 +38,9 @@ class Editor extends React.Component {
4138
<button type="submit" disabled={this.state.isSaving}>
4239
Submit
4340
</button>
41+
{this.state.error ? (
42+
<div data-testid="post-error">{this.state.error}</div>
43+
) : null}
4444
</form>
4545
)
4646
}

0 commit comments

Comments
 (0)