|
@@ -0,0 +1,136 @@
|
|
1
|
+import { Linking } from 'react-native';
|
|
2
|
+
|
|
3
|
+import {
|
|
4
|
+ defaultOriginWhitelist,
|
|
5
|
+ createOnShouldStartLoadWithRequest,
|
|
6
|
+} from '../WebViewShared';
|
|
7
|
+
|
|
8
|
+describe('WebViewShared', () => {
|
|
9
|
+ test('exports defaultOriginWhitelist', () => {
|
|
10
|
+ expect(defaultOriginWhitelist).toMatchSnapshot();
|
|
11
|
+ });
|
|
12
|
+
|
|
13
|
+ describe('createOnShouldStartLoadWithRequest', () => {
|
|
14
|
+ const alwaysTrueOnShouldStartLoadWithRequest = (nativeEvent) => {
|
|
15
|
+ return true;
|
|
16
|
+ };
|
|
17
|
+
|
|
18
|
+ const alwaysFalseOnShouldStartLoadWithRequest = (nativeEvent) => {
|
|
19
|
+ return false;
|
|
20
|
+ };
|
|
21
|
+
|
|
22
|
+ const loadRequest = jest.fn();
|
|
23
|
+
|
|
24
|
+ test('loadRequest is called without onShouldStartLoadWithRequest override', () => {
|
|
25
|
+ const onShouldStartLoadWithRequest = createOnShouldStartLoadWithRequest(
|
|
26
|
+ loadRequest,
|
|
27
|
+ defaultOriginWhitelist,
|
|
28
|
+ );
|
|
29
|
+
|
|
30
|
+ onShouldStartLoadWithRequest({ nativeEvent: { url: 'https://www.example.com/', lockIdentifier: 1 } });
|
|
31
|
+ expect(Linking.openURL).toHaveBeenCalledTimes(0);
|
|
32
|
+ expect(loadRequest).toHaveBeenCalledWith(true, 'https://www.example.com/', 1);
|
|
33
|
+ });
|
|
34
|
+
|
|
35
|
+ test('Linking.openURL is called without onShouldStartLoadWithRequest override', () => {
|
|
36
|
+ const onShouldStartLoadWithRequest = createOnShouldStartLoadWithRequest(
|
|
37
|
+ loadRequest,
|
|
38
|
+ defaultOriginWhitelist,
|
|
39
|
+ );
|
|
40
|
+
|
|
41
|
+ onShouldStartLoadWithRequest({ nativeEvent: { url: 'invalid://example.com/', lockIdentifier: 2 } });
|
|
42
|
+ expect(Linking.openURL).toHaveBeenCalledWith('invalid://example.com/');
|
|
43
|
+ expect(loadRequest).toHaveBeenCalledWith(false, 'invalid://example.com/', 2);
|
|
44
|
+ });
|
|
45
|
+
|
|
46
|
+ test('loadRequest with true onShouldStartLoadWithRequest override is called', () => {
|
|
47
|
+ const onShouldStartLoadWithRequest = createOnShouldStartLoadWithRequest(
|
|
48
|
+ loadRequest,
|
|
49
|
+ defaultOriginWhitelist,
|
|
50
|
+ alwaysTrueOnShouldStartLoadWithRequest,
|
|
51
|
+ );
|
|
52
|
+
|
|
53
|
+ onShouldStartLoadWithRequest({ nativeEvent: { url: 'https://www.example.com/', lockIdentifier: 1 } });
|
|
54
|
+ expect(Linking.openURL).toHaveBeenCalledTimes(0);
|
|
55
|
+ expect(loadRequest).toHaveBeenLastCalledWith(true, 'https://www.example.com/', 1);
|
|
56
|
+ });
|
|
57
|
+
|
|
58
|
+ test('Linking.openURL with true onShouldStartLoadWithRequest override is called for links not passing the whitelist', () => {
|
|
59
|
+ const onShouldStartLoadWithRequest = createOnShouldStartLoadWithRequest(
|
|
60
|
+ loadRequest,
|
|
61
|
+ defaultOriginWhitelist,
|
|
62
|
+ alwaysTrueOnShouldStartLoadWithRequest,
|
|
63
|
+ );
|
|
64
|
+
|
|
65
|
+ onShouldStartLoadWithRequest({ nativeEvent: { url: 'invalid://example.com/', lockIdentifier: 1 } });
|
|
66
|
+ expect(Linking.openURL).toHaveBeenLastCalledWith('invalid://example.com/');
|
|
67
|
+ expect(loadRequest).toHaveBeenLastCalledWith(true, 'invalid://example.com/', 1);
|
|
68
|
+ });
|
|
69
|
+
|
|
70
|
+ test('loadRequest with false onShouldStartLoadWithRequest override is called', () => {
|
|
71
|
+ const onShouldStartLoadWithRequest = createOnShouldStartLoadWithRequest(
|
|
72
|
+ loadRequest,
|
|
73
|
+ defaultOriginWhitelist,
|
|
74
|
+ alwaysFalseOnShouldStartLoadWithRequest,
|
|
75
|
+ );
|
|
76
|
+
|
|
77
|
+ onShouldStartLoadWithRequest({ nativeEvent: { url: 'https://www.example.com/', lockIdentifier: 1 } });
|
|
78
|
+ expect(Linking.openURL).toHaveBeenCalledTimes(0);
|
|
79
|
+ expect(loadRequest).toHaveBeenLastCalledWith(false, 'https://www.example.com/', 1);
|
|
80
|
+ });
|
|
81
|
+
|
|
82
|
+ test('loadRequest with limited whitelist', () => {
|
|
83
|
+ const onShouldStartLoadWithRequest = createOnShouldStartLoadWithRequest(
|
|
84
|
+ loadRequest,
|
|
85
|
+ ['https://*'],
|
|
86
|
+ );
|
|
87
|
+
|
|
88
|
+ onShouldStartLoadWithRequest({ nativeEvent: { url: 'https://www.example.com/', lockIdentifier: 1 } });
|
|
89
|
+ expect(Linking.openURL).toHaveBeenCalledTimes(0);
|
|
90
|
+ expect(loadRequest).toHaveBeenLastCalledWith(true, 'https://www.example.com/', 1);
|
|
91
|
+
|
|
92
|
+ onShouldStartLoadWithRequest({ nativeEvent: { url: 'http://insecure.com/', lockIdentifier: 2 } });
|
|
93
|
+ expect(Linking.openURL).toHaveBeenLastCalledWith('http://insecure.com/');
|
|
94
|
+ expect(loadRequest).toHaveBeenLastCalledWith(false, 'http://insecure.com/', 2);
|
|
95
|
+
|
|
96
|
+ onShouldStartLoadWithRequest({ nativeEvent: { url: 'git+https://insecure.com/', lockIdentifier: 3 } });
|
|
97
|
+ expect(Linking.openURL).toHaveBeenLastCalledWith('git+https://insecure.com/');
|
|
98
|
+ expect(loadRequest).toHaveBeenLastCalledWith(false, 'git+https://insecure.com/', 3);
|
|
99
|
+
|
|
100
|
+ onShouldStartLoadWithRequest({ nativeEvent: { url: 'fakehttps://insecure.com/', lockIdentifier: 4 } });
|
|
101
|
+ expect(Linking.openURL).toHaveBeenLastCalledWith('fakehttps://insecure.com/');
|
|
102
|
+ expect(loadRequest).toHaveBeenLastCalledWith(false, 'fakehttps://insecure.com/', 4);
|
|
103
|
+ });
|
|
104
|
+
|
|
105
|
+ test('loadRequest allows for valid URIs', () => {
|
|
106
|
+ const onShouldStartLoadWithRequest = createOnShouldStartLoadWithRequest(
|
|
107
|
+ loadRequest,
|
|
108
|
+ ['plus+https://*', 'DOT.https://*', 'dash-https://*', '0invalid://*', '+invalid://*'],
|
|
109
|
+ );
|
|
110
|
+
|
|
111
|
+ onShouldStartLoadWithRequest({ nativeEvent: { url: 'plus+https://www.example.com/', lockIdentifier: 1 } });
|
|
112
|
+ expect(Linking.openURL).toHaveBeenCalledTimes(0);
|
|
113
|
+ expect(loadRequest).toHaveBeenLastCalledWith(true, 'plus+https://www.example.com/', 1);
|
|
114
|
+
|
|
115
|
+ onShouldStartLoadWithRequest({ nativeEvent: { url: 'DOT.https://www.example.com/', lockIdentifier: 2 } });
|
|
116
|
+ expect(Linking.openURL).toHaveBeenCalledTimes(0);
|
|
117
|
+ expect(loadRequest).toHaveBeenLastCalledWith(true, 'DOT.https://www.example.com/', 2);
|
|
118
|
+
|
|
119
|
+ onShouldStartLoadWithRequest({ nativeEvent: { url: 'dash-https://www.example.com/', lockIdentifier: 3 } });
|
|
120
|
+ expect(Linking.openURL).toHaveBeenCalledTimes(0);
|
|
121
|
+ expect(loadRequest).toHaveBeenLastCalledWith(true, 'dash-https://www.example.com/', 3);
|
|
122
|
+
|
|
123
|
+ onShouldStartLoadWithRequest({ nativeEvent: { url: '0invalid://www.example.com/', lockIdentifier: 4 } });
|
|
124
|
+ expect(Linking.openURL).toHaveBeenLastCalledWith('0invalid://www.example.com/');
|
|
125
|
+ expect(loadRequest).toHaveBeenLastCalledWith(false, '0invalid://www.example.com/', 4);
|
|
126
|
+
|
|
127
|
+ onShouldStartLoadWithRequest({ nativeEvent: { url: '+invalid://www.example.com/', lockIdentifier: 5 } });
|
|
128
|
+ expect(Linking.openURL).toHaveBeenLastCalledWith('+invalid://www.example.com/');
|
|
129
|
+ expect(loadRequest).toHaveBeenLastCalledWith(false, '+invalid://www.example.com/', 5);
|
|
130
|
+
|
|
131
|
+ onShouldStartLoadWithRequest({ nativeEvent: { url: 'FAKE+plus+https://www.example.com/', lockIdentifier: 6 } });
|
|
132
|
+ expect(Linking.openURL).toHaveBeenLastCalledWith('FAKE+plus+https://www.example.com/');
|
|
133
|
+ expect(loadRequest).toHaveBeenLastCalledWith(false, 'FAKE+plus+https://www.example.com/', 6);
|
|
134
|
+ });
|
|
135
|
+ });
|
|
136
|
+});
|