index.test.js 4.3 KB

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