path-utils.spec.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import * as pathUtils from './path-utils';
  2. describe('page-utils', () => {
  3. describe('.normalizePath', () => {
  4. test.concurrent.each`
  5. path | expected
  6. ${'/'} | ${'/'}
  7. ${''} | ${'/'}
  8. ${'path'} | ${'/path'}
  9. ${'/path'} | ${'/path'}
  10. ${'path/'} | ${'/path'}
  11. ${'/path/'} | ${'/path'}
  12. ${'path1/path2'} | ${'/path1/path2'}
  13. ${'/path1/path2'} | ${'/path1/path2'}
  14. ${'path1/path2/'} | ${'/path1/path2'}
  15. ${'/path1/path2/'} | ${'/path1/path2'}
  16. ${'//path1/path2//'} | ${'/path1/path2'}
  17. ${'https://example.com'} | ${'/https://example.com'}
  18. ${'https://example.com/'} | ${'/https://example.com'}
  19. `('should normalize \'$path\' to \'$expected\'', ({ path, expected }) => {
  20. expect(pathUtils.normalizePath(path)).toBe(expected);
  21. });
  22. });
  23. describe('.hasHeadingSlash', () => {
  24. test.concurrent.each`
  25. path | expected
  26. ${'/'} | ${true}
  27. ${''} | ${false}
  28. ${'path'} | ${false}
  29. ${'/path'} | ${true}
  30. ${'path/'} | ${false}
  31. ${'/path/'} | ${true}
  32. ${'path1/path2'} | ${false}
  33. ${'/path1/path2'} | ${true}
  34. ${'path1/path2/'} | ${false}
  35. ${'/path1/path2/'} | ${true}
  36. ${'//path1/path2//'} | ${true}
  37. ${'https://example.com'} | ${false}
  38. ${'https://example.com/'} | ${false}
  39. `('should return $expected when checking heading slash for \'$path\'', ({ path, expected }) => {
  40. expect(pathUtils.hasHeadingSlash(path)).toBe(expected);
  41. });
  42. });
  43. describe('.hasTrailingSlash', () => {
  44. test.concurrent.each`
  45. path | expected
  46. ${'/'} | ${true}
  47. ${''} | ${false}
  48. ${'path'} | ${false}
  49. ${'/path'} | ${false}
  50. ${'path/'} | ${true}
  51. ${'/path/'} | ${true}
  52. ${'path1/path2'} | ${false}
  53. ${'/path1/path2'} | ${false}
  54. ${'path1/path2/'} | ${true}
  55. ${'/path1/path2/'} | ${true}
  56. ${'//path1/path2//'} | ${true}
  57. ${'https://example.com'} | ${false}
  58. ${'https://example.com/'} | ${true}
  59. `('should return $expected when checking trailing slash for \'$path\'', ({ path, expected }) => {
  60. expect(pathUtils.hasTrailingSlash(path)).toBe(expected);
  61. });
  62. });
  63. describe('.addHeadingSlash', () => {
  64. test.concurrent.each`
  65. path | expected
  66. ${'/'} | ${'/'}
  67. ${''} | ${'/'}
  68. ${'path'} | ${'/path'}
  69. ${'/path'} | ${'/path'}
  70. ${'path/'} | ${'/path/'}
  71. ${'/path/'} | ${'/path/'}
  72. ${'path1/path2'} | ${'/path1/path2'}
  73. ${'/path1/path2'} | ${'/path1/path2'}
  74. ${'path1/path2/'} | ${'/path1/path2/'}
  75. ${'/path1/path2/'} | ${'/path1/path2/'}
  76. ${'//path1/path2//'} | ${'//path1/path2//'}
  77. ${'https://example.com'} | ${'/https://example.com'}
  78. ${'https://example.com/'} | ${'/https://example.com/'}
  79. `('should add heading slash to \'$path\' resulting in \'$expected\'', ({ path, expected }) => {
  80. expect(pathUtils.addHeadingSlash(path)).toBe(expected);
  81. });
  82. });
  83. describe('.addTrailingSlash', () => {
  84. test.concurrent.each`
  85. path | expected
  86. ${'/'} | ${'/'}
  87. ${''} | ${'/'}
  88. ${'path'} | ${'path/'}
  89. ${'/path'} | ${'/path/'}
  90. ${'path/'} | ${'path/'}
  91. ${'/path/'} | ${'/path/'}
  92. ${'path1/path2'} | ${'path1/path2/'}
  93. ${'/path1/path2'} | ${'/path1/path2/'}
  94. ${'path1/path2/'} | ${'path1/path2/'}
  95. ${'/path1/path2/'} | ${'/path1/path2/'}
  96. ${'//path1/path2//'} | ${'//path1/path2//'}
  97. ${'https://example.com'} | ${'https://example.com/'}
  98. ${'https://example.com/'} | ${'https://example.com/'}
  99. `('should add trailing slash to \'$path\' resulting in \'$expected\'', ({ path, expected }) => {
  100. expect(pathUtils.addTrailingSlash(path)).toBe(expected);
  101. });
  102. });
  103. describe('.removeHeadingSlash', () => {
  104. test.concurrent.each`
  105. path | expected
  106. ${'/'} | ${'/'}
  107. ${''} | ${''}
  108. ${'path'} | ${'path'}
  109. ${'/path'} | ${'path'}
  110. ${'path/'} | ${'path/'}
  111. ${'/path/'} | ${'path/'}
  112. ${'path1/path2'} | ${'path1/path2'}
  113. ${'/path1/path2'} | ${'path1/path2'}
  114. ${'path1/path2/'} | ${'path1/path2/'}
  115. ${'/path1/path2/'} | ${'path1/path2/'}
  116. ${'//path1/path2//'} | ${'path1/path2//'}
  117. ${'https://example.com'} | ${'https://example.com'}
  118. ${'https://example.com/'} | ${'https://example.com/'}
  119. ${'//'} | ${'/'} // from former specific test
  120. `('should remove heading slash from \'$path\' resulting in \'$expected\'', ({ path, expected }) => {
  121. expect(pathUtils.removeHeadingSlash(path)).toBe(expected);
  122. });
  123. });
  124. describe('.removeTrailingSlash', () => {
  125. test.concurrent.each`
  126. path | expected
  127. ${'/'} | ${'/'}
  128. ${''} | ${''}
  129. ${'path'} | ${'path'}
  130. ${'/path'} | ${'/path'}
  131. ${'path/'} | ${'path'}
  132. ${'/path/'} | ${'/path'}
  133. ${'path1/path2'} | ${'path1/path2'}
  134. ${'/path1/path2'} | ${'/path1/path2'}
  135. ${'path1/path2/'} | ${'path1/path2'}
  136. ${'/path1/path2/'} | ${'/path1/path2'}
  137. ${'//path1/path2//'} | ${'//path1/path2'}
  138. ${'https://example.com'} | ${'https://example.com'}
  139. ${'https://example.com/'} | ${'https://example.com'}
  140. `('should remove trailing slash from \'$path\' resulting in \'$expected\'', ({ path, expected }) => {
  141. expect(pathUtils.removeTrailingSlash(path)).toBe(expected);
  142. });
  143. });
  144. describe('.getParentPath', () => {
  145. test.concurrent.each`
  146. path | expected
  147. ${'/'} | ${'/'}
  148. ${''} | ${'/'}
  149. ${'path'} | ${'/'}
  150. ${'/path'} | ${'/'}
  151. ${'path/'} | ${'/path'}
  152. ${'/path/'} | ${'/path'}
  153. ${'path1/path2'} | ${'/path1'}
  154. ${'/path1/path2'} | ${'/path1'}
  155. ${'path1/path2/'} | ${'/path1/path2'}
  156. ${'/path1/path2/'} | ${'/path1/path2'}
  157. ${'//path1/path2//'} | ${'/path1/path2'}
  158. ${'https://example.com'} | ${'/https:'}
  159. ${'https://example.com/'} | ${'/https://example.com'}
  160. ${'/page'} | ${'/'} // from former specific test
  161. // Note: getParentPath('page') is covered by 'path' -> '/'
  162. // Note: getParentPath('/path1/path2') is covered by '/path1/path2' -> '/path1'
  163. // Note: getParentPath('/path1/path2/') is covered by '/path1/path2/' -> '/path1/path2'
  164. `('should get parent path of \'$path\' as \'$expected\'', ({ path, expected }) => {
  165. expect(pathUtils.getParentPath(path)).toBe(expected);
  166. });
  167. });
  168. });