Skip to content

Commit dd4fd4f

Browse files
author
Kent C. Dodds
committed
add tests for 13
1 parent fb8359b commit dd4fd4f

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

src/__tests__/13.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import React from 'react'
2+
import {renderToggle, Simulate} from '../../test/utils'
3+
import Usage from '../exercises-final/13'
4+
// import Usage from '../exercises/13'
5+
6+
const renderRendux = () => {
7+
const utils = renderToggle(<Usage />)
8+
const printedState = utils.getByTestId('printed-state')
9+
const input = utils.getByPlaceholderText('Type')
10+
return {
11+
getPrintedState: () => JSON.parse(printedState.textContent),
12+
input,
13+
changeInputValue: (value, extraEventProps) =>
14+
Simulate.change(input, {target: {value}, ...extraEventProps}),
15+
...utils,
16+
}
17+
}
18+
19+
test('toggle and input state interact', () => {
20+
const {
21+
toggleButton,
22+
getPrintedState,
23+
changeInputValue,
24+
getByText,
25+
input,
26+
} = renderRendux(<Usage />)
27+
expect(toggleButton).toBeOn()
28+
expect(getPrintedState()).toEqual({
29+
on: true,
30+
})
31+
changeInputValue('off')
32+
expect(toggleButton).toBeOff()
33+
expect(getPrintedState()).toEqual({
34+
on: false,
35+
inputValue: 'off',
36+
})
37+
Simulate.click(getByText('reset'))
38+
expect(toggleButton).toBeOn()
39+
expect(getPrintedState()).toEqual({
40+
on: true,
41+
})
42+
expect(input.value).toBe('on')
43+
changeInputValue('something else')
44+
expect(input.value).toBe('something else')
45+
expect(getPrintedState()).toEqual({
46+
on: true,
47+
inputValue: 'something else',
48+
})
49+
})

src/exercises-final/13.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function MyInput() {
6969
<Rendux.Consumer>
7070
{rendux => (
7171
<input
72-
defaultValue={rendux.state.on ? 'on' : 'off'}
72+
value={rendux.state.inputValue || (rendux.state.on ? 'on' : 'off')}
7373
placeholder="Type 'off' or 'on'"
7474
onChange={event => {
7575
if (event.target.value === 'on') {
@@ -122,7 +122,9 @@ function MySwitch() {
122122
const StatePrinter = withRendux(({rendux}) => (
123123
<div style={{textAlign: 'left'}}>
124124
state:
125-
<pre>{JSON.stringify(rendux.state, null, 2)}</pre>
125+
<pre data-testid="printed-state">
126+
{JSON.stringify(rendux.state, null, 2)}
127+
</pre>
126128
</div>
127129
))
128130

src/exercises/13.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ function MySwitch() {
7878
const StatePrinter = withRendux(({rendux}) => (
7979
<div style={{textAlign: 'left'}}>
8080
state:
81-
<pre>{JSON.stringify(rendux.state, null, 2)}</pre>
81+
<pre data-testid="printed-state">
82+
{JSON.stringify(rendux.state, null, 2)}
83+
</pre>
8284
</div>
8385
))
8486

0 commit comments

Comments
 (0)