-
Notifications
You must be signed in to change notification settings - Fork 933
/
preact.js
105 lines (98 loc) · 2.89 KB
/
preact.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Tell Babel to transform JSX into preact.h() calls:
/** @jsx preact.h */
/*
eslint-disable
react/prop-types,
no-console,
react/display-name,
import/extensions,
import/no-unresolved
*/
/*
Testing the preact version is a tiny bit complicated because
we need the preact build (the one that imports 'preact' rather
than 'react') otherwise things don't work very well.
So there's a script `test.build` which will run the cjs build
for preact before running this test.
*/
import preact from 'preact'
import render from 'preact-render-to-string'
import Downshift from '../../../preact'
test('works with preact', () => {
const renderSpy = jest.fn(({getInputProps, getItemProps}) => (
<div>
<input {...getInputProps()} />
<div>
<div {...getItemProps({item: 'foo', index: 0})}>foo</div>
<div {...getItemProps({item: 'bar', index: 1})}>bar</div>
</div>
</div>
))
const ui = <Downshift render={renderSpy} />
render(ui)
expect(renderSpy).toHaveBeenCalledWith(
expect.objectContaining({
isOpen: false,
highlightedIndex: null,
selectedItem: null,
inputValue: '',
}),
)
})
test('can render a composite component', () => {
const Div = ({innerRef, ...props}) => <div {...props} ref={innerRef} />
const renderSpy = jest.fn(({getRootProps}) => (
<Div {...getRootProps({refKey: 'innerRef'})} />
))
const ui = <Downshift render={renderSpy} />
render(ui)
expect(renderSpy).toHaveBeenCalledWith(
expect.objectContaining({
isOpen: false,
highlightedIndex: null,
selectedItem: null,
inputValue: '',
}),
)
})
test('getInputProps composes onChange with onInput', () => {
const onChange = jest.fn()
const onInput = jest.fn()
const Input = jest.fn(props => <input {...props} />)
const {ui} = setup({
children({getInputProps}) {
return (
<div>
<Input {...getInputProps({onChange, onInput})} />
</div>
)
},
})
render(ui)
expect(Input).toHaveBeenCalledTimes(1)
const [[firstArg]] = Input.mock.calls
expect(firstArg).toMatchObject({
onInput: expect.any(Function),
})
expect(firstArg.onChange).toBeUndefined()
const fakeEvent = {defaultPrevented: false, target: {value: ''}}
firstArg.onInput(fakeEvent)
expect(onChange).toHaveBeenCalledTimes(1)
expect(onChange).toHaveBeenCalledWith(fakeEvent)
expect(onInput).toHaveBeenCalledTimes(1)
expect(onInput).toHaveBeenCalledWith(fakeEvent)
})
test('can use children instead of render prop', () => {
const childrenSpy = jest.fn()
render(<Downshift>{childrenSpy}</Downshift>)
expect(childrenSpy).toHaveBeenCalledTimes(1)
})
function setup({children = () => <div />, ...props} = {}) {
let renderArg
const renderSpy = jest.fn(controllerArg => {
renderArg = controllerArg
return children(controllerArg)
})
const ui = <Downshift {...props}>{renderSpy}</Downshift>
return {renderSpy, ui, ...renderArg}
}