-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Expand file tree
/
Copy pathindex.native.js
More file actions
105 lines (88 loc) · 2.41 KB
/
index.native.js
File metadata and controls
105 lines (88 loc) · 2.41 KB
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
/**
* External dependencies
*/
import { act } from 'react-test-renderer';
import { fireEvent, render } from 'test/helpers';
import { Keyboard, Text } from 'react-native';
/**
* WordPress dependencies
*/
import { SlotFillProvider } from '@wordpress/components';
/**
* Internal dependencies
*/
import Tooltip from '../index';
// Minimal tree to render tooltip.
const TooltipSlot = ( { children } ) => (
<SlotFillProvider>
<Tooltip.Slot>{ children }</Tooltip.Slot>
</SlotFillProvider>
);
let keyboardAddListenerSpy;
const keyboardHandlers = [];
beforeAll( () => {
keyboardAddListenerSpy = jest
.spyOn( Keyboard, 'addListener' )
.mockImplementation( ( event, handler ) => {
const length = keyboardHandlers.push( [ event, handler ] );
return {
remove: () => {
keyboardHandlers.splice( length - 1 );
},
};
} );
} );
afterAll( () => {
keyboardAddListenerSpy.mockRestore();
} );
it( 'displays the message', () => {
const screen = render(
<TooltipSlot>
<Tooltip visible={ true } text="A helpful message">
<Text>I need help</Text>
</Tooltip>
</TooltipSlot>
);
expect( screen.getByText( 'A helpful message' ) ).toBeTruthy();
} );
// Skipped until `pointerEvents: 'box-none'` no longer erroneously prevents
// triggering `onTouch*` on the element: https://github.com/callstack/react-native-testing-library/issues/897
it.skip( 'dismisses when the screen is tapped', () => {
const screen = render(
<TooltipSlot>
<Tooltip visible={ true } text="A helpful message">
<Text>I need help</Text>
</Tooltip>
</TooltipSlot>
);
expect( screen.getByText( 'A helpful message' ) ).toBeTruthy();
fireEvent( screen.getByTestId( 'tooltip-overlay' ), 'touchStart' );
expect( screen.queryByText( 'A helpful message' ) ).toBeNull();
} );
it( 'dismisses when the keyboard closes', () => {
const screen = render(
<TooltipSlot>
<Tooltip visible={ true } text="A helpful message">
<Text>I need help</Text>
</Tooltip>
</TooltipSlot>
);
// Show keyboard.
act( () => {
keyboardHandlers.forEach( ( [ event, handler ] ) => {
if ( event === 'keyboardDidShow' ) {
handler();
}
} );
} );
expect( screen.getByText( 'A helpful message' ) ).toBeTruthy();
// Hide keyboard
act( () => {
keyboardHandlers.forEach( ( [ event, handler ] ) => {
if ( event === 'keyboardDidHide' ) {
handler();
}
} );
} );
expect( screen.queryByText( 'A helpful message' ) ).toBeNull();
} );