You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.test.js 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import {expect, test} from 'vitest';
  2. import {
  3. basename, extname, isObject, uniq, stripTags, joinPaths, parseIssueHref,
  4. prettyNumber, parseUrl,
  5. } from './utils.js';
  6. test('basename', () => {
  7. expect(basename('/path/to/file.js')).toEqual('file.js');
  8. expect(basename('/path/to/file')).toEqual('file');
  9. expect(basename('file.js')).toEqual('file.js');
  10. });
  11. test('extname', () => {
  12. expect(extname('/path/to/file.js')).toEqual('.js');
  13. expect(extname('/path/')).toEqual('');
  14. expect(extname('/path')).toEqual('');
  15. expect(extname('file.js')).toEqual('.js');
  16. });
  17. test('joinPaths', () => {
  18. expect(joinPaths('', '')).toEqual('');
  19. expect(joinPaths('', 'b')).toEqual('b');
  20. expect(joinPaths('', '/b')).toEqual('/b');
  21. expect(joinPaths('', '/b/')).toEqual('/b/');
  22. expect(joinPaths('a', '')).toEqual('a');
  23. expect(joinPaths('/a', '')).toEqual('/a');
  24. expect(joinPaths('/a/', '')).toEqual('/a/');
  25. expect(joinPaths('a', 'b')).toEqual('a/b');
  26. expect(joinPaths('a', '/b')).toEqual('a/b');
  27. expect(joinPaths('/a', '/b')).toEqual('/a/b');
  28. expect(joinPaths('/a', '/b')).toEqual('/a/b');
  29. expect(joinPaths('/a/', '/b')).toEqual('/a/b');
  30. expect(joinPaths('/a', '/b/')).toEqual('/a/b/');
  31. expect(joinPaths('/a/', '/b/')).toEqual('/a/b/');
  32. expect(joinPaths('', '', '')).toEqual('');
  33. expect(joinPaths('', 'b', '')).toEqual('b');
  34. expect(joinPaths('', 'b', 'c')).toEqual('b/c');
  35. expect(joinPaths('', '', 'c')).toEqual('c');
  36. expect(joinPaths('', '/b', '/c')).toEqual('/b/c');
  37. expect(joinPaths('/a', '', '/c')).toEqual('/a/c');
  38. expect(joinPaths('/a', '/b', '')).toEqual('/a/b');
  39. expect(joinPaths('', '/')).toEqual('/');
  40. expect(joinPaths('a', '/')).toEqual('a/');
  41. expect(joinPaths('', '/', '/')).toEqual('/');
  42. expect(joinPaths('/', '/')).toEqual('/');
  43. expect(joinPaths('/', '')).toEqual('/');
  44. expect(joinPaths('/', 'b')).toEqual('/b');
  45. expect(joinPaths('/', 'b/')).toEqual('/b/');
  46. expect(joinPaths('/', '', '/')).toEqual('/');
  47. expect(joinPaths('/', 'b', '/')).toEqual('/b/');
  48. expect(joinPaths('/', 'b/', '/')).toEqual('/b/');
  49. expect(joinPaths('a', '/', '/')).toEqual('a/');
  50. expect(joinPaths('/', '/', 'c')).toEqual('/c');
  51. expect(joinPaths('/', '/', 'c/')).toEqual('/c/');
  52. });
  53. test('isObject', () => {
  54. expect(isObject({})).toBeTruthy();
  55. expect(isObject([])).toBeFalsy();
  56. });
  57. test('uniq', () => {
  58. expect(uniq([1, 1, 1, 2])).toEqual([1, 2]);
  59. });
  60. test('stripTags', () => {
  61. expect(stripTags('<a>test</a>')).toEqual('test');
  62. });
  63. test('parseIssueHref', () => {
  64. expect(parseIssueHref('/owner/repo/issues/1')).toEqual({owner: 'owner', repo: 'repo', type: 'issues', index: '1'});
  65. expect(parseIssueHref('/owner/repo/pulls/1?query')).toEqual({owner: 'owner', repo: 'repo', type: 'pulls', index: '1'});
  66. expect(parseIssueHref('/owner/repo/issues/1#hash')).toEqual({owner: 'owner', repo: 'repo', type: 'issues', index: '1'});
  67. expect(parseIssueHref('/sub/owner/repo/issues/1')).toEqual({owner: 'owner', repo: 'repo', type: 'issues', index: '1'});
  68. expect(parseIssueHref('/sub/sub2/owner/repo/pulls/1')).toEqual({owner: 'owner', repo: 'repo', type: 'pulls', index: '1'});
  69. expect(parseIssueHref('/sub/sub2/owner/repo/issues/1?query')).toEqual({owner: 'owner', repo: 'repo', type: 'issues', index: '1'});
  70. expect(parseIssueHref('/sub/sub2/owner/repo/issues/1#hash')).toEqual({owner: 'owner', repo: 'repo', type: 'issues', index: '1'});
  71. expect(parseIssueHref('https://example.com/owner/repo/issues/1')).toEqual({owner: 'owner', repo: 'repo', type: 'issues', index: '1'});
  72. expect(parseIssueHref('https://example.com/owner/repo/pulls/1?query')).toEqual({owner: 'owner', repo: 'repo', type: 'pulls', index: '1'});
  73. expect(parseIssueHref('https://example.com/owner/repo/issues/1#hash')).toEqual({owner: 'owner', repo: 'repo', type: 'issues', index: '1'});
  74. expect(parseIssueHref('https://example.com/sub/owner/repo/issues/1')).toEqual({owner: 'owner', repo: 'repo', type: 'issues', index: '1'});
  75. expect(parseIssueHref('https://example.com/sub/sub2/owner/repo/pulls/1')).toEqual({owner: 'owner', repo: 'repo', type: 'pulls', index: '1'});
  76. expect(parseIssueHref('https://example.com/sub/sub2/owner/repo/issues/1?query')).toEqual({owner: 'owner', repo: 'repo', type: 'issues', index: '1'});
  77. expect(parseIssueHref('https://example.com/sub/sub2/owner/repo/issues/1#hash')).toEqual({owner: 'owner', repo: 'repo', type: 'issues', index: '1'});
  78. expect(parseIssueHref('')).toEqual({owner: undefined, repo: undefined, type: undefined, index: undefined});
  79. });
  80. test('prettyNumber', () => {
  81. expect(prettyNumber()).toEqual('');
  82. expect(prettyNumber(null)).toEqual('');
  83. expect(prettyNumber(undefined)).toEqual('');
  84. expect(prettyNumber('1200')).toEqual('');
  85. expect(prettyNumber(12345678, 'en-US')).toEqual('12,345,678');
  86. expect(prettyNumber(12345678, 'de-DE')).toEqual('12.345.678');
  87. expect(prettyNumber(12345678, 'be-BE')).toEqual('12 345 678');
  88. expect(prettyNumber(12345678, 'hi-IN')).toEqual('1,23,45,678');
  89. });
  90. test('parseUrl', () => {
  91. expect(parseUrl('').pathname).toEqual('/');
  92. expect(parseUrl('/path').pathname).toEqual('/path');
  93. expect(parseUrl('/path?search').pathname).toEqual('/path');
  94. expect(parseUrl('/path?search').search).toEqual('?search');
  95. expect(parseUrl('/path?search#hash').hash).toEqual('#hash');
  96. expect(parseUrl('https://localhost/path').pathname).toEqual('/path');
  97. expect(parseUrl('https://localhost/path?search').pathname).toEqual('/path');
  98. expect(parseUrl('https://localhost/path?search').search).toEqual('?search');
  99. expect(parseUrl('https://localhost/path?search#hash').hash).toEqual('#hash');
  100. });