|
@@ -0,0 +1,110 @@
|
|
1
|
+import * as React from 'react';
|
|
2
|
+import * as ReactTestRenderer from 'react-test-renderer';
|
|
3
|
+import { View } from 'react-native';
|
|
4
|
+import { SafeAreaProvider, useSafeArea } from '../index';
|
|
5
|
+import NativeSafeAreaView from '../NativeSafeAreaView';
|
|
6
|
+
|
|
7
|
+const PrintInsetsTestView = () => {
|
|
8
|
+ const insets = useSafeArea();
|
|
9
|
+ return (
|
|
10
|
+ <View
|
|
11
|
+ style={{
|
|
12
|
+ top: insets.top,
|
|
13
|
+ left: insets.left,
|
|
14
|
+ bottom: insets.bottom,
|
|
15
|
+ right: insets.right,
|
|
16
|
+ }}
|
|
17
|
+ />
|
|
18
|
+ );
|
|
19
|
+};
|
|
20
|
+
|
|
21
|
+describe('SafeAreaProvider', () => {
|
|
22
|
+ it('renders', () => {
|
|
23
|
+ const component = ReactTestRenderer.create(<SafeAreaProvider />);
|
|
24
|
+ expect(component).toMatchSnapshot();
|
|
25
|
+ });
|
|
26
|
+
|
|
27
|
+ it('does not render child until inset values are received', () => {
|
|
28
|
+ const component = ReactTestRenderer.create(
|
|
29
|
+ <SafeAreaProvider>
|
|
30
|
+ <PrintInsetsTestView />
|
|
31
|
+ </SafeAreaProvider>,
|
|
32
|
+ );
|
|
33
|
+ expect(component).toMatchSnapshot();
|
|
34
|
+ });
|
|
35
|
+
|
|
36
|
+ it('renders child when inset values are received', () => {
|
|
37
|
+ const component = ReactTestRenderer.create(
|
|
38
|
+ <SafeAreaProvider>
|
|
39
|
+ <PrintInsetsTestView />
|
|
40
|
+ </SafeAreaProvider>,
|
|
41
|
+ );
|
|
42
|
+ expect(component).toMatchSnapshot();
|
|
43
|
+ const { onInsetsChange } = component.root.findByType(
|
|
44
|
+ NativeSafeAreaView,
|
|
45
|
+ ).props;
|
|
46
|
+ ReactTestRenderer.act(() => {
|
|
47
|
+ onInsetsChange({
|
|
48
|
+ nativeEvent: { insets: { top: 1, left: 2, right: 3, bottom: 4 } },
|
|
49
|
+ });
|
|
50
|
+ });
|
|
51
|
+ expect(component).toMatchSnapshot();
|
|
52
|
+ });
|
|
53
|
+
|
|
54
|
+ it('uses parent insets when available', () => {
|
|
55
|
+ const component = ReactTestRenderer.create(
|
|
56
|
+ <SafeAreaProvider
|
|
57
|
+ initialSafeAreaInsets={{ top: 1, left: 2, right: 3, bottom: 4 }}
|
|
58
|
+ >
|
|
59
|
+ <SafeAreaProvider>
|
|
60
|
+ <PrintInsetsTestView />
|
|
61
|
+ </SafeAreaProvider>
|
|
62
|
+ </SafeAreaProvider>,
|
|
63
|
+ );
|
|
64
|
+ expect(component).toMatchSnapshot();
|
|
65
|
+ });
|
|
66
|
+
|
|
67
|
+ it('uses inner insets', () => {
|
|
68
|
+ const component = ReactTestRenderer.create(
|
|
69
|
+ <SafeAreaProvider
|
|
70
|
+ initialSafeAreaInsets={{ top: 1, left: 2, right: 3, bottom: 4 }}
|
|
71
|
+ >
|
|
72
|
+ <SafeAreaProvider
|
|
73
|
+ initialSafeAreaInsets={{ top: 2, left: 3, right: 4, bottom: 5 }}
|
|
74
|
+ >
|
|
75
|
+ <PrintInsetsTestView />
|
|
76
|
+ </SafeAreaProvider>
|
|
77
|
+ </SafeAreaProvider>,
|
|
78
|
+ );
|
|
79
|
+ expect(component).toMatchSnapshot();
|
|
80
|
+ });
|
|
81
|
+
|
|
82
|
+ it('supports setting initial insets', () => {
|
|
83
|
+ const component = ReactTestRenderer.create(
|
|
84
|
+ <SafeAreaProvider
|
|
85
|
+ initialSafeAreaInsets={{ top: 1, left: 2, right: 3, bottom: 4 }}
|
|
86
|
+ >
|
|
87
|
+ <PrintInsetsTestView />
|
|
88
|
+ </SafeAreaProvider>,
|
|
89
|
+ );
|
|
90
|
+ expect(component).toMatchSnapshot();
|
|
91
|
+ });
|
|
92
|
+
|
|
93
|
+ it('throws when no provider is rendered', () => {
|
|
94
|
+ // Silence the React error boundary warning; we expect an uncaught error.
|
|
95
|
+ const consoleErrorMock = jest
|
|
96
|
+ .spyOn(console, 'error')
|
|
97
|
+ .mockImplementation(message => {
|
|
98
|
+ if (message.startsWith('The above error occured in the ')) {
|
|
99
|
+ return;
|
|
100
|
+ }
|
|
101
|
+ });
|
|
102
|
+ expect(() => {
|
|
103
|
+ ReactTestRenderer.create(<PrintInsetsTestView />);
|
|
104
|
+ }).toThrow(
|
|
105
|
+ 'No safe area value available. Make sure you are rendering `<SafeAreaProvider>` at the top of your app.',
|
|
106
|
+ );
|
|
107
|
+
|
|
108
|
+ consoleErrorMock.mockRestore();
|
|
109
|
+ });
|
|
110
|
+});
|