index.test.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import {
  2. isRemote, compareVersion, debounce, throttle,
  3. } from '@/common';
  4. jest.useFakeTimers();
  5. test('isRemote', () => {
  6. [
  7. isRemote(),
  8. isRemote('file:///tmp/file'),
  9. isRemote('data:text/plain,hello,world'),
  10. isRemote('http://localhost/a.user.js'),
  11. isRemote('https://localhost/a.user.js'),
  12. isRemote('http://127.0.0.1/a.user.js'),
  13. isRemote('http://127.0.0.1:5555/a.user.js'),
  14. isRemote('http://192.168.1.32/a.user.js'),
  15. isRemote('http://172.16.0.1/a.user.js'),
  16. isRemote('http://10.0.0.1/a.user.js'),
  17. isRemote('http://[::1]/a.user.js'),
  18. isRemote('http://[fe80::6996:2ba9:37e6:8762]/a.user.js'),
  19. isRemote('http://[fc00::90:90]/a.user.js'),
  20. isRemote('http://example.test/a.user.js'),
  21. isRemote('https://example.example/a.user.js'),
  22. isRemote('http://example.invalid/a.user.js'),
  23. isRemote('https://example.localhost/a.user.js'),
  24. ].forEach(f => {
  25. expect(f).toBeFalsy();
  26. });
  27. [
  28. isRemote('http://www.google.com'),
  29. isRemote('https://www.google.com'),
  30. ].forEach(t => {
  31. expect(t).toBeTruthy();
  32. });
  33. });
  34. test('compareVersion', () => {
  35. expect(compareVersion('1.2.3', '1.2.3')).toEqual(0);
  36. expect(compareVersion('1.2.3', '1.2.0')).toEqual(1);
  37. expect(compareVersion('1.2.3', '1.2.4')).toEqual(-1);
  38. expect(compareVersion('1.2.0', '1.2')).toEqual(0);
  39. expect(compareVersion('1.2.1', '1.2')).toEqual(1);
  40. expect(compareVersion('1.1.9', '1.2')).toEqual(-1);
  41. expect(compareVersion('1.10', '1.9')).toEqual(1);
  42. expect([
  43. '1.2.3',
  44. '1.2.3-alpha',
  45. '1.0.0-x.7.z.92',
  46. '1.0.0-alpha.1',
  47. '1.0.0-alpha',
  48. '4.11.6',
  49. '4.2.0',
  50. '1.5.19',
  51. '1.5.5',
  52. '4.1.3',
  53. '2.3.1',
  54. '10.5.5',
  55. '11.3.0',
  56. '1.0.0',
  57. '1.0.0-rc.1',
  58. '1.0.0-beta.11',
  59. '1.0.0-beta',
  60. '1.0.0-beta.2',
  61. '1.0.0-alpha.beta+build',
  62. '1.0.0-alpha.1',
  63. '1.0.0-alpha',
  64. ].sort(compareVersion)).toEqual([
  65. '1.0.0-alpha',
  66. '1.0.0-alpha',
  67. '1.0.0-alpha.1',
  68. '1.0.0-alpha.1',
  69. '1.0.0-alpha.beta+build',
  70. '1.0.0-beta',
  71. '1.0.0-beta.2',
  72. '1.0.0-beta.11',
  73. '1.0.0-rc.1',
  74. '1.0.0-x.7.z.92',
  75. '1.0.0',
  76. '1.2.3-alpha',
  77. '1.2.3',
  78. '1.5.5',
  79. '1.5.19',
  80. '2.3.1',
  81. '4.1.3',
  82. '4.2.0',
  83. '4.11.6',
  84. '10.5.5',
  85. '11.3.0',
  86. ]);
  87. });
  88. test('debounce', () => {
  89. const log = [];
  90. const fn = debounce((i) => {
  91. log.push(i);
  92. }, 500);
  93. for (let i = 0; i < 3; i += 1) {
  94. setTimeout(fn, 200 * i, i);
  95. }
  96. for (let i = 0; i < 3; i += 1) {
  97. setTimeout(fn, 1200 + 600 * i, i);
  98. }
  99. jest.runAllTimers();
  100. expect(log).toEqual([2, 0, 1, 2]);
  101. });
  102. test('debounce with invalid time', () => {
  103. for (const time of [undefined, -100]) {
  104. const log = [];
  105. const fn = debounce((i) => {
  106. log.push(i);
  107. }, time);
  108. for (let i = 0; i < 3; i += 1) {
  109. fn(i);
  110. }
  111. jest.runAllTimers();
  112. expect(log).toEqual([2]);
  113. }
  114. });
  115. test('throttle', () => {
  116. const log = [];
  117. const fn = throttle((i) => {
  118. log.push(i);
  119. }, 500);
  120. for (let i = 0; i < 6; i += 1) {
  121. setTimeout(fn, 200 * i, i);
  122. }
  123. for (let i = 0; i < 3; i += 1) {
  124. setTimeout(fn, 1200 + 600 * i, i);
  125. }
  126. jest.runAllTimers();
  127. expect(log).toEqual([0, 3, 0, 1, 2]);
  128. });
  129. test('throttle with invalid time', () => {
  130. for (const time of [undefined, -100]) {
  131. const log = [];
  132. const fn = throttle((i) => {
  133. log.push(i);
  134. }, time);
  135. for (let i = 0; i < 3; i += 1) {
  136. fn(i);
  137. }
  138. jest.runAllTimers();
  139. expect(log).toEqual([0]);
  140. }
  141. });