Skip to content

Commit

Permalink
Test <Form /> component (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
TrySound authored Jun 2, 2018
1 parent d052c7e commit 828adeb
Showing 1 changed file with 64 additions and 2 deletions.
66 changes: 64 additions & 2 deletions tests/components/Form.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,71 @@
import * as React from 'react'
import TestRenderer from 'react-test-renderer'
import { Form } from '../../src'
import { lastCallArg } from './utils'

test('<Form />', () => {
const renderFn = jest.fn().mockReturnValue(null)
TestRenderer.create(<Form render={renderFn} />)
// TODO
TestRenderer.create(
<Form initial={{ prop1: '1', prop2: '2' }} render={renderFn} />
)

expect(renderFn).toBeCalledTimes(1)
expect(renderFn).lastCalledWith(
expect.objectContaining({ values: { prop1: '1', prop2: '2' } })
)

expect(lastCallArg(renderFn).input('prop1')).toEqual(
expect.objectContaining({
value: '1',
bind: expect.objectContaining({ value: '1' }),
})
)
expect(lastCallArg(renderFn).input('prop2')).toEqual(
expect.objectContaining({
value: '2',
bind: expect.objectContaining({ value: '2' }),
})
)

lastCallArg(renderFn)
.input('prop1')
.set('10')
lastCallArg(renderFn)
.input('prop2')
.bind.onChange({ target: { value: '20' } })

expect(lastCallArg(renderFn).input('prop1')).toEqual(
expect.objectContaining({
value: '10',
bind: expect.objectContaining({ value: '10' }),
})
)
expect(lastCallArg(renderFn).input('prop2')).toEqual(
expect.objectContaining({
value: '20',
bind: expect.objectContaining({ value: '20' }),
})
)
})

test('<Form onChange />', () => {
const renderFn = jest.fn().mockReturnValue(null)
const onChangeFn = jest.fn()
TestRenderer.create(
<Form initial={{ prop: '1' }} onChange={onChangeFn} render={renderFn} />
)

expect(onChangeFn).toBeCalledTimes(0)

lastCallArg(renderFn)
.input('prop')
.set('10')
expect(onChangeFn).toBeCalledTimes(1)
expect(onChangeFn).lastCalledWith({ prop: '10' })

lastCallArg(renderFn)
.input('prop')
.bind.onChange({ target: { value: '100' } })
expect(onChangeFn).toBeCalledTimes(2)
expect(onChangeFn).lastCalledWith({ prop: '100' })
})

0 comments on commit 828adeb

Please sign in to comment.