index.test.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import test from 'tape';
  2. import {
  3. isRemote, compareVersion, debounce, throttle,
  4. } from '#/common';
  5. import { mocker } from '../mock';
  6. test('isRemote', (t) => {
  7. t.notOk(isRemote());
  8. t.notOk(isRemote('file:///tmp/file'));
  9. t.notOk(isRemote('data:text/plain,hello,world'));
  10. t.ok(isRemote('http://www.google.com'));
  11. t.ok(isRemote('https://www.google.com'));
  12. t.notOk(isRemote('http://localhost/a.user.js'));
  13. t.notOk(isRemote('https://localhost/a.user.js'));
  14. t.notOk(isRemote('http://127.0.0.1/a.user.js'));
  15. t.end();
  16. });
  17. test('compareVersion', (t) => {
  18. t.equal(compareVersion('1.2.3', '1.2.3'), 0);
  19. t.equal(compareVersion('1.2.3', '1.2.0'), 1);
  20. t.equal(compareVersion('1.2.3', '1.2.4'), -1);
  21. t.equal(compareVersion('1.2.0', '1.2'), 0);
  22. t.equal(compareVersion('1.2.1', '1.2'), 1);
  23. t.equal(compareVersion('1.1.9', '1.2'), -1);
  24. t.equal(compareVersion('1.10', '1.9'), 1);
  25. t.end();
  26. });
  27. test('debounce', (t) => {
  28. const log = [];
  29. const fn = debounce((i) => {
  30. log.push(i);
  31. }, 500);
  32. for (let i = 0; i < 3; i += 1) {
  33. fn(i);
  34. mocker.clock.tick(200);
  35. }
  36. mocker.clock.tick(500);
  37. for (let i = 0; i < 3; i += 1) {
  38. fn(i);
  39. mocker.clock.tick(600);
  40. }
  41. t.deepEqual(log, [2, 0, 1, 2]);
  42. t.end();
  43. });
  44. test('throttle', (t) => {
  45. const log = [];
  46. const fn = throttle((i) => {
  47. log.push(i);
  48. }, 500);
  49. for (let i = 0; i < 6; i += 1) {
  50. fn(i);
  51. mocker.clock.tick(200);
  52. }
  53. mocker.clock.tick(500);
  54. for (let i = 0; i < 3; i += 1) {
  55. fn(i);
  56. mocker.clock.tick(600);
  57. }
  58. t.deepEqual(log, [0, 3, 0, 1, 2]);
  59. t.end();
  60. });