index.test.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import {
  2. isRemote, compareVersion, debounce, throttle,
  3. } from '@/common';
  4. import { mocker } from '../mock';
  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. fn(i);
  95. mocker.clock.tick(200);
  96. }
  97. mocker.clock.tick(500);
  98. for (let i = 0; i < 3; i += 1) {
  99. fn(i);
  100. mocker.clock.tick(600);
  101. }
  102. expect(log).toEqual([2, 0, 1, 2]);
  103. });
  104. test('debounce with invalid time', () => {
  105. for (const time of [undefined, -100]) {
  106. const log = [];
  107. const fn = debounce((i) => {
  108. log.push(i);
  109. }, time);
  110. for (let i = 0; i < 3; i += 1) {
  111. fn(i);
  112. }
  113. mocker.clock.tick(500);
  114. expect(log).toEqual([2]);
  115. }
  116. });
  117. test('throttle', () => {
  118. const log = [];
  119. const fn = throttle((i) => {
  120. log.push(i);
  121. }, 500);
  122. for (let i = 0; i < 6; i += 1) {
  123. fn(i);
  124. mocker.clock.tick(200);
  125. }
  126. mocker.clock.tick(500);
  127. for (let i = 0; i < 3; i += 1) {
  128. fn(i);
  129. mocker.clock.tick(600);
  130. }
  131. expect(log).toEqual([0, 3, 0, 1, 2]);
  132. });
  133. test('throttle with invalid time', () => {
  134. for (const time of [undefined, -100]) {
  135. const log = [];
  136. const fn = throttle((i) => {
  137. log.push(i);
  138. }, time);
  139. for (let i = 0; i < 3; i += 1) {
  140. fn(i);
  141. }
  142. mocker.clock.tick(500);
  143. expect(log).toEqual([0]);
  144. }
  145. });