-
Notifications
You must be signed in to change notification settings - Fork 333
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(use-input): add hooks-bsaed testcase
- Loading branch information
Showing
1 changed file
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import React, { useEffect } from 'react' | ||
import { mount } from 'enzyme' | ||
import { AutoComplete, useInput } from 'components' | ||
|
||
describe('UseInput', () => { | ||
it('should follow change with use-input', () => { | ||
let log = '' | ||
const logSpy = jest.spyOn(console, 'log').mockImplementation(msg => (log = msg)) | ||
const MockInput: React.FC<{ value?: string }> = ({ value }) => { | ||
const { state, setState, bindings } = useInput('') | ||
useEffect(() => { | ||
if (value) setState(value) | ||
}, [value]) | ||
useEffect(() => { | ||
if (state) console.log(state) | ||
}, [state]) | ||
return <AutoComplete {...bindings} /> | ||
} | ||
|
||
const wrapper = mount(<MockInput />) | ||
wrapper.setProps({ value: 'test' }) | ||
const input = wrapper.find('input').at(0).getDOMNode() as HTMLInputElement | ||
|
||
expect(input.value).toEqual('test') | ||
expect(log).toContain('test') | ||
|
||
log = '' | ||
wrapper | ||
.find('input') | ||
.at(0) | ||
.simulate('change', { target: { value: 'test-change' } }) | ||
expect(log).toContain('test-change') | ||
logSpy.mockRestore() | ||
}) | ||
|
||
it('should follow change with use-input', () => { | ||
const MockInput: React.FC<{ value?: string; resetValue?: boolean }> = ({ | ||
value, | ||
resetValue, | ||
}) => { | ||
const { reset, setState, bindings } = useInput('') | ||
useEffect(() => { | ||
if (value) setState(value) | ||
}, [value]) | ||
useEffect(() => { | ||
if (resetValue) reset() | ||
}, [resetValue]) | ||
return <AutoComplete {...bindings} /> | ||
} | ||
|
||
const wrapper = mount(<MockInput />) | ||
wrapper.setProps({ value: 'test' }) | ||
let input = wrapper.find('input').at(0).getDOMNode() as HTMLInputElement | ||
expect(input.value).toEqual('test') | ||
|
||
wrapper.setProps({ resetValue: true }) | ||
input = wrapper.find('input').at(0).getDOMNode() as HTMLInputElement | ||
expect(input.value).toEqual('') | ||
}) | ||
}) |