-
Notifications
You must be signed in to change notification settings - Fork 10
/
forwardRefFc.test.tsx
52 lines (42 loc) · 1.32 KB
/
forwardRefFc.test.tsx
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
import React, { Component } from 'react';
import { collect, WithStoreProp } from '../..';
import * as testUtils from '../testUtils';
type Props = WithStoreProp & {
defaultValue: string;
inputRef: React.Ref<HTMLInputElement>;
};
const CollectedWithRef = collect((props: Props) => (
<label>
The input
<input ref={props.inputRef} defaultValue={props.defaultValue} />
</label>
));
class ComponentWithRef extends Component {
inputRef = React.createRef<HTMLInputElement>();
render() {
return (
<div>
<button
onClick={() => {
this.inputRef.current!.value = 'X';
}}
>
Empty the input
</button>
<CollectedWithRef defaultValue="some text" inputRef={this.inputRef} />
</div>
);
}
}
const { getByText, getByLabelText } = testUtils.renderStrict(
<ComponentWithRef />
);
const getInputByLabelText = (text: string) =>
getByLabelText(text) as HTMLInputElement;
it('should empty the input when the button is clicked', () => {
expect(getInputByLabelText('The input').value).toBe('some text');
getInputByLabelText('The input').value = 'some different text';
expect(getInputByLabelText('The input').value).toBe('some different text');
getByText('Empty the input').click();
expect(getInputByLabelText('The input').value).toBe('X');
});