Skip to content

Commit c230a2c

Browse files
committed
feat: add test
1 parent 6d9fdd3 commit c230a2c

File tree

5 files changed

+90
-36
lines changed

5 files changed

+90
-36
lines changed

src/Input.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export class Input extends PureComponent<Props, {}> {
4444
render() {
4545
const {
4646
classname,
47-
placeholder,
47+
placeholder = '',
4848
value,
4949
maxlength,
5050
canClear = true
@@ -66,9 +66,9 @@ export class Input extends PureComponent<Props, {}> {
6666
)
6767
}
6868

69-
handleChange = (e: React.FormEvent<HTMLInputElement>) => {
69+
handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
7070
const { onChange, numericInput = false } = this.props
71-
const { value } = e.currentTarget
71+
const { value } = e.target
7272
if (numericInput && typeof onChange === 'function') {
7373
const reg = /^[0-9]+$/
7474
if (reg.test(value) || value === '') {

tests/Input.test.tsx

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { mount } from 'enzyme'
2+
import React from 'react'
3+
4+
import { sleep } from './util'
5+
6+
import Input from '../src'
7+
8+
describe('src/index', () => {
9+
describe('should render properly', () => {
10+
let wrapper
11+
it('should render basicly', () => {
12+
wrapper = mount(<RenderInput />)
13+
expect(wrapper.find('.or-input').length).toBe(1)
14+
expect(wrapper.find('.or-input input').prop('placeholder')).toBe('')
15+
expect(wrapper.find('.or-input input').prop('value')).toBe('')
16+
expect(wrapper.find('.or-input .or-clear-icon').length).toBe(1)
17+
})
18+
19+
it('should render basicly #placeholder', () => {
20+
wrapper = mount(<RenderInput placeholder="name" />)
21+
expect(wrapper.find('.or-input').length).toBe(1)
22+
expect(wrapper.find('.or-input input[placeholder="name"]').length).toBe(1)
23+
expect(wrapper.find('.or-input input').prop('value')).toBe('')
24+
})
25+
})
26+
27+
describe('simulate events', async () => {
28+
let wrapper
29+
it('onchange event', async () => {
30+
wrapper = mount(<RenderInput />)
31+
const input = wrapper.find('.or-input input')
32+
input.instance().value = 'yuki'
33+
input.simulate('change')
34+
// input.simulate('focus');
35+
// input.simulate('change', { target: { value: 'Changed' } });
36+
// input.simulate('keyDown', {
37+
// which: 72,
38+
// target: {
39+
// blur() {
40+
// // Needed since <EditableText /> calls target.blur()
41+
// input.simulate('blur');
42+
// },
43+
// },
44+
// })
45+
// await sleep(500)
46+
// expect(input.props().value).toBe('Hello');
47+
// input.simulate('change', { target: { value: 'yuki' } })
48+
// expect(wrapper.find('input').prop('value')).toBe('yuki')
49+
50+
expect(input.value).toBe('yuki')
51+
})
52+
})
53+
})
54+
55+
interface Props {
56+
numericInput?: boolean
57+
maxlength?: number
58+
placeholder?: string
59+
}
60+
61+
class RenderInput extends React.Component<Props, {}> {
62+
state = {
63+
name: ''
64+
}
65+
66+
render() {
67+
return (
68+
<div>
69+
<Input
70+
value={this.state.name}
71+
onChange={this.handleChange}
72+
{...this.props}
73+
/>
74+
</div>
75+
)
76+
}
77+
78+
handleChange = value => {
79+
this.setState({
80+
name: value
81+
})
82+
}
83+
}

tests/Sample.test.tsx

Lines changed: 0 additions & 33 deletions
This file was deleted.

tests/setup.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
import { configure } from 'enzyme'
33
import Adapter from 'enzyme-adapter-react-16'
44
import 'raf/polyfill'
5+
import 'regenerator-runtime/runtime'
56

67
configure({ adapter: new Adapter() })

tests/util.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function sleep(ms) {
2+
return new Promise(resolve => setTimeout(resolve, ms))
3+
}

0 commit comments

Comments
 (0)