Ei kuvausta

WebViewShared-test.js 6.4KB

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