Skip to content

Commit

Permalink
Implement test for No. 4
Browse files Browse the repository at this point in the history
  • Loading branch information
3c1u committed Apr 28, 2021
1 parent 9329f2f commit ee26480
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions tests/station04.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import React from 'react'
import renderer, { act } from 'react-test-renderer'
import { App } from '../src/App'


describe('<App />', () => {
let setState: React.Dispatch<unknown> | undefined = undefined
const useState = React.useState
const useStateSpy = jest.spyOn(React, 'useState')
useStateSpy.mockImplementation((v?: unknown) => {
const [value, dispatcher] = useState(v)
setState = dispatcher
return [value, dispatcher]
})

afterEach(() => {
jest.clearAllMocks()
})

it('<App /> calls useState', () => {
renderer.create(<App />)

expect(useStateSpy).toBeCalled()
})

it('<img> uses a state value', () => {
const res = renderer.create(<App />)
const img = res.root.findByType('img')
const injectValue = '🐕'

expect(img.props.src).toBeTruthy()
expect(useStateSpy).toBeCalledWith(img.props.src)

useStateSpy.mockClear()

expect(setState).not.toBeUndefined()

act(() => {
if (setState) {
setState(injectValue)
}
})

expect(img.props.src).toBe(injectValue)
})
})

0 comments on commit ee26480

Please sign in to comment.