Browse Source

Add js tests

Janic Duplessis 5 years ago
parent
commit
71763ac4ed
4 changed files with 1800 additions and 19 deletions
  1. 9
    0
      package.json
  2. 140
    0
      src/__tests__/__snapshots__/index-test.tsx.snap
  3. 110
    0
      src/__tests__/index-test.tsx
  4. 1541
    19
      yarn.lock

+ 9
- 0
package.json View File

@@ -27,6 +27,7 @@
27 27
     "validate:eslint": "eslint \"src/**/*.{js,ts,tsx}\" \"example/**/*.{js,ts,tsx}\"",
28 28
     "validate:typescript": "tsc --project ./ --noEmit",
29 29
     "validate:prettier": "prettier \"src/**/*.{js,ts,tsx}\" \"example/**/*.{js,ts,tsx}\" --check",
30
+    "validate:jest": "jest",
30 31
     "prepare": "bob build"
31 32
   },
32 33
   "keywords": [
@@ -44,19 +45,24 @@
44 45
   "devDependencies": {
45 46
     "@react-native-community/bob": "^0.7.0",
46 47
     "@react-native-community/eslint-config": "^0.0.5",
48
+    "@types/jest": "^24.0.21",
49
+    "@types/react": "^16.9.11",
47 50
     "@types/react-native": "^0.60.22",
51
+    "@types/react-test-renderer": "^16.9.1",
48 52
     "@typescript-eslint/eslint-plugin": "^2.6.0",
49 53
     "@typescript-eslint/parser": "^2.6.0",
50 54
     "eslint": "6.6.0",
51 55
     "eslint-config-prettier": "^6.5.0",
52 56
     "eslint-plugin-prettier": "3.1.1",
53 57
     "expo": "^35.0.0",
58
+    "jest": "^24.9.0",
54 59
     "metro-react-native-babel-preset": "^0.57.0",
55 60
     "prettier": "^1.18.2",
56 61
     "react": "^16.11.0",
57 62
     "react-dom": "^16.11.0",
58 63
     "react-native": "^0.61.0",
59 64
     "react-native-web": "^0.11.7",
65
+    "react-test-renderer": "^16.11.0",
60 66
     "typescript": "^3.6.4"
61 67
   },
62 68
   "repository": {
@@ -64,6 +70,9 @@
64 70
     "url": "https://github.com/th3rdwave/react-native-safe-area-context.git"
65 71
   },
66 72
   "jest": {
73
+    "preset": "react-native",
74
+    "testEnvironment": "node",
75
+    "clearMocks": true,
67 76
     "modulePathIgnorePatterns": [
68 77
       "<rootDir>/lib/"
69 78
     ]

+ 140
- 0
src/__tests__/__snapshots__/index-test.tsx.snap View File

@@ -0,0 +1,140 @@
1
+// Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+exports[`SafeAreaProvider does not render child until inset values are received 1`] = `
4
+<RNCSafeAreaView
5
+  onInsetsChange={[Function]}
6
+  style={
7
+    Object {
8
+      "flex": 1,
9
+    }
10
+  }
11
+/>
12
+`;
13
+
14
+exports[`SafeAreaProvider renders 1`] = `
15
+<RNCSafeAreaView
16
+  onInsetsChange={[Function]}
17
+  style={
18
+    Object {
19
+      "flex": 1,
20
+    }
21
+  }
22
+/>
23
+`;
24
+
25
+exports[`SafeAreaProvider renders child when inset values are received 1`] = `
26
+<RNCSafeAreaView
27
+  onInsetsChange={[Function]}
28
+  style={
29
+    Object {
30
+      "flex": 1,
31
+    }
32
+  }
33
+/>
34
+`;
35
+
36
+exports[`SafeAreaProvider renders child when inset values are received 2`] = `
37
+<RNCSafeAreaView
38
+  onInsetsChange={[Function]}
39
+  style={
40
+    Object {
41
+      "flex": 1,
42
+    }
43
+  }
44
+>
45
+  <View
46
+    style={
47
+      Object {
48
+        "bottom": 4,
49
+        "left": 2,
50
+        "right": 3,
51
+        "top": 1,
52
+      }
53
+    }
54
+  />
55
+</RNCSafeAreaView>
56
+`;
57
+
58
+exports[`SafeAreaProvider supports setting initial insets 1`] = `
59
+<RNCSafeAreaView
60
+  onInsetsChange={[Function]}
61
+  style={
62
+    Object {
63
+      "flex": 1,
64
+    }
65
+  }
66
+>
67
+  <View
68
+    style={
69
+      Object {
70
+        "bottom": 4,
71
+        "left": 2,
72
+        "right": 3,
73
+        "top": 1,
74
+      }
75
+    }
76
+  />
77
+</RNCSafeAreaView>
78
+`;
79
+
80
+exports[`SafeAreaProvider uses inner insets 1`] = `
81
+<RNCSafeAreaView
82
+  onInsetsChange={[Function]}
83
+  style={
84
+    Object {
85
+      "flex": 1,
86
+    }
87
+  }
88
+>
89
+  <RNCSafeAreaView
90
+    onInsetsChange={[Function]}
91
+    style={
92
+      Object {
93
+        "flex": 1,
94
+      }
95
+    }
96
+  >
97
+    <View
98
+      style={
99
+        Object {
100
+          "bottom": 5,
101
+          "left": 3,
102
+          "right": 4,
103
+          "top": 2,
104
+        }
105
+      }
106
+    />
107
+  </RNCSafeAreaView>
108
+</RNCSafeAreaView>
109
+`;
110
+
111
+exports[`SafeAreaProvider uses parent insets when available 1`] = `
112
+<RNCSafeAreaView
113
+  onInsetsChange={[Function]}
114
+  style={
115
+    Object {
116
+      "flex": 1,
117
+    }
118
+  }
119
+>
120
+  <RNCSafeAreaView
121
+    onInsetsChange={[Function]}
122
+    style={
123
+      Object {
124
+        "flex": 1,
125
+      }
126
+    }
127
+  >
128
+    <View
129
+      style={
130
+        Object {
131
+          "bottom": 4,
132
+          "left": 2,
133
+          "right": 3,
134
+          "top": 1,
135
+        }
136
+      }
137
+    />
138
+  </RNCSafeAreaView>
139
+</RNCSafeAreaView>
140
+`;

+ 110
- 0
src/__tests__/index-test.tsx View File

@@ -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
+});

+ 1541
- 19
yarn.lock
File diff suppressed because it is too large
View File