Nessuna descrizione

WebViewShared-test.js 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import { Linking } from 'react-native';
  2. import {
  3. defaultOriginWhitelist,
  4. createOnShouldStartLoadWithRequest,
  5. } from '../WebViewShared';
  6. Linking.openURL.mockResolvedValue(undefined);
  7. Linking.canOpenURL.mockResolvedValue(true);
  8. // The tests that call createOnShouldStartLoadWithRequest will cause a promise
  9. // to get kicked off (by calling the mocked `Linking.canOpenURL`) that the tests
  10. // _need_ to get run to completion _before_ doing any `expect`ing. The reason
  11. // is: once that promise is resolved another function should get run which will
  12. // call `Linking.openURL`, and we want to test that.
  13. //
  14. // Normally we would probably do something like `await
  15. // createShouldStartLoadWithRequest(...)` in the tests, but that doesn't work
  16. // here because the promise that gets kicked off is not returned (because
  17. // non-test code doesn't need to know about it).
  18. //
  19. // The tests thus need a way to "flush any pending promises" (to make sure
  20. // pending promises run to completion) before doing any `expect`ing. `jest`
  21. // doesn't provide a way to do this out of the box, but we can use this function
  22. // to do it.
  23. //
  24. // See this issue for more discussion: https://github.com/facebook/jest/issues/2157
  25. function flushPromises() {
  26. return new Promise(resolve => setImmediate(resolve));
  27. }
  28. describe('WebViewShared', () => {
  29. test('exports defaultOriginWhitelist', () => {
  30. expect(defaultOriginWhitelist).toMatchSnapshot();
  31. });
  32. describe('createOnShouldStartLoadWithRequest', () => {
  33. const alwaysTrueOnShouldStartLoadWithRequest = (nativeEvent) => {
  34. return true;
  35. };
  36. const alwaysFalseOnShouldStartLoadWithRequest = (nativeEvent) => {
  37. return false;
  38. };
  39. const loadRequest = jest.fn();
  40. test('loadRequest is called without onShouldStartLoadWithRequest override', async () => {
  41. const onShouldStartLoadWithRequest = createOnShouldStartLoadWithRequest(
  42. loadRequest,
  43. defaultOriginWhitelist,
  44. );
  45. onShouldStartLoadWithRequest({ nativeEvent: { url: 'https://www.example.com/', lockIdentifier: 1 } });
  46. await flushPromises();
  47. expect(Linking.openURL).toHaveBeenCalledTimes(0);
  48. expect(loadRequest).toHaveBeenCalledWith(true, 'https://www.example.com/', 1);
  49. });
  50. test('Linking.openURL is called without onShouldStartLoadWithRequest override', async () => {
  51. const onShouldStartLoadWithRequest = createOnShouldStartLoadWithRequest(
  52. loadRequest,
  53. defaultOriginWhitelist,
  54. );
  55. onShouldStartLoadWithRequest({ nativeEvent: { url: 'invalid://example.com/', lockIdentifier: 2 } });
  56. await flushPromises();
  57. expect(Linking.openURL).toHaveBeenCalledWith('invalid://example.com/');
  58. expect(loadRequest).toHaveBeenCalledWith(false, 'invalid://example.com/', 2);
  59. });
  60. test('loadRequest with true onShouldStartLoadWithRequest override is called', async () => {
  61. const onShouldStartLoadWithRequest = createOnShouldStartLoadWithRequest(
  62. loadRequest,
  63. defaultOriginWhitelist,
  64. alwaysTrueOnShouldStartLoadWithRequest,
  65. );
  66. onShouldStartLoadWithRequest({ nativeEvent: { url: 'https://www.example.com/', lockIdentifier: 1 } });
  67. await flushPromises();
  68. expect(Linking.openURL).toHaveBeenCalledTimes(0);
  69. expect(loadRequest).toHaveBeenLastCalledWith(true, 'https://www.example.com/', 1);
  70. });
  71. test('Linking.openURL with true onShouldStartLoadWithRequest override is called for links not passing the whitelist', async () => {
  72. const onShouldStartLoadWithRequest = createOnShouldStartLoadWithRequest(
  73. loadRequest,
  74. defaultOriginWhitelist,
  75. alwaysTrueOnShouldStartLoadWithRequest,
  76. );
  77. var a = 10;
  78. onShouldStartLoadWithRequest({ nativeEvent: { url: 'invalid://example.com/', lockIdentifier: 1 } });
  79. await flushPromises();
  80. expect(Linking.openURL).toHaveBeenLastCalledWith('invalid://example.com/');
  81. // We don't expect the URL to have been loaded in the WebView because it
  82. // is not in the origin whitelist
  83. expect(loadRequest).toHaveBeenLastCalledWith(false, 'invalid://example.com/', 1);
  84. });
  85. test('loadRequest with false onShouldStartLoadWithRequest override is called', async () => {
  86. const onShouldStartLoadWithRequest = createOnShouldStartLoadWithRequest(
  87. loadRequest,
  88. defaultOriginWhitelist,
  89. alwaysFalseOnShouldStartLoadWithRequest,
  90. );
  91. onShouldStartLoadWithRequest({ nativeEvent: { url: 'https://www.example.com/', lockIdentifier: 1 } });
  92. await flushPromises();
  93. expect(Linking.openURL).toHaveBeenCalledTimes(0);
  94. expect(loadRequest).toHaveBeenLastCalledWith(false, 'https://www.example.com/', 1);
  95. });
  96. test('loadRequest with limited whitelist', async () => {
  97. const onShouldStartLoadWithRequest = createOnShouldStartLoadWithRequest(
  98. loadRequest,
  99. ['https://*'],
  100. );
  101. onShouldStartLoadWithRequest({ nativeEvent: { url: 'https://www.example.com/', lockIdentifier: 1 } });
  102. await flushPromises();
  103. expect(Linking.openURL).toHaveBeenCalledTimes(0);
  104. expect(loadRequest).toHaveBeenLastCalledWith(true, 'https://www.example.com/', 1);
  105. onShouldStartLoadWithRequest({ nativeEvent: { url: 'http://insecure.com/', lockIdentifier: 2 } });
  106. await flushPromises();
  107. expect(Linking.openURL).toHaveBeenLastCalledWith('http://insecure.com/');
  108. expect(loadRequest).toHaveBeenLastCalledWith(false, 'http://insecure.com/', 2);
  109. onShouldStartLoadWithRequest({ nativeEvent: { url: 'git+https://insecure.com/', lockIdentifier: 3 } });
  110. await flushPromises();
  111. expect(Linking.openURL).toHaveBeenLastCalledWith('git+https://insecure.com/');
  112. expect(loadRequest).toHaveBeenLastCalledWith(false, 'git+https://insecure.com/', 3);
  113. onShouldStartLoadWithRequest({ nativeEvent: { url: 'fakehttps://insecure.com/', lockIdentifier: 4 } });
  114. await flushPromises();
  115. expect(Linking.openURL).toHaveBeenLastCalledWith('fakehttps://insecure.com/');
  116. expect(loadRequest).toHaveBeenLastCalledWith(false, 'fakehttps://insecure.com/', 4);
  117. });
  118. test('loadRequest allows for valid URIs', async () => {
  119. const onShouldStartLoadWithRequest = createOnShouldStartLoadWithRequest(
  120. loadRequest,
  121. ['plus+https://*', 'DOT.https://*', 'dash-https://*', '0invalid://*', '+invalid://*'],
  122. );
  123. onShouldStartLoadWithRequest({ nativeEvent: { url: 'plus+https://www.example.com/', lockIdentifier: 1 } });
  124. await flushPromises();
  125. expect(Linking.openURL).toHaveBeenCalledTimes(0);
  126. expect(loadRequest).toHaveBeenLastCalledWith(true, 'plus+https://www.example.com/', 1);
  127. onShouldStartLoadWithRequest({ nativeEvent: { url: 'DOT.https://www.example.com/', lockIdentifier: 2 } });
  128. await flushPromises();
  129. expect(Linking.openURL).toHaveBeenCalledTimes(0);
  130. expect(loadRequest).toHaveBeenLastCalledWith(true, 'DOT.https://www.example.com/', 2);
  131. onShouldStartLoadWithRequest({ nativeEvent: { url: 'dash-https://www.example.com/', lockIdentifier: 3 } });
  132. await flushPromises();
  133. expect(Linking.openURL).toHaveBeenCalledTimes(0);
  134. expect(loadRequest).toHaveBeenLastCalledWith(true, 'dash-https://www.example.com/', 3);
  135. onShouldStartLoadWithRequest({ nativeEvent: { url: '0invalid://www.example.com/', lockIdentifier: 4 } });
  136. await flushPromises();
  137. expect(Linking.openURL).toHaveBeenLastCalledWith('0invalid://www.example.com/');
  138. expect(loadRequest).toHaveBeenLastCalledWith(false, '0invalid://www.example.com/', 4);
  139. onShouldStartLoadWithRequest({ nativeEvent: { url: '+invalid://www.example.com/', lockIdentifier: 5 } });
  140. await flushPromises();
  141. expect(Linking.openURL).toHaveBeenLastCalledWith('+invalid://www.example.com/');
  142. expect(loadRequest).toHaveBeenLastCalledWith(false, '+invalid://www.example.com/', 5);
  143. onShouldStartLoadWithRequest({ nativeEvent: { url: 'FAKE+plus+https://www.example.com/', lockIdentifier: 6 } });
  144. await flushPromises();
  145. expect(Linking.openURL).toHaveBeenLastCalledWith('FAKE+plus+https://www.example.com/');
  146. expect(loadRequest).toHaveBeenLastCalledWith(false, 'FAKE+plus+https://www.example.com/', 6);
  147. });
  148. });
  149. });