path-utils.spec.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. `(
  40. "should return $expected when checking heading slash for '$path'",
  41. ({ path, expected }) => {
  42. expect(pathUtils.hasHeadingSlash(path)).toBe(expected);
  43. },
  44. );
  45. });
  46. describe('.hasTrailingSlash', () => {
  47. test.concurrent.each`
  48. path | expected
  49. ${'/'} | ${true}
  50. ${''} | ${false}
  51. ${'path'} | ${false}
  52. ${'/path'} | ${false}
  53. ${'path/'} | ${true}
  54. ${'/path/'} | ${true}
  55. ${'path1/path2'} | ${false}
  56. ${'/path1/path2'} | ${false}
  57. ${'path1/path2/'} | ${true}
  58. ${'/path1/path2/'} | ${true}
  59. ${'//path1/path2//'} | ${true}
  60. ${'https://example.com'} | ${false}
  61. ${'https://example.com/'} | ${true}
  62. `(
  63. "should return $expected when checking trailing slash for '$path'",
  64. ({ path, expected }) => {
  65. expect(pathUtils.hasTrailingSlash(path)).toBe(expected);
  66. },
  67. );
  68. });
  69. describe('.addHeadingSlash', () => {
  70. test.concurrent.each`
  71. path | expected
  72. ${'/'} | ${'/'}
  73. ${''} | ${'/'}
  74. ${'path'} | ${'/path'}
  75. ${'/path'} | ${'/path'}
  76. ${'path/'} | ${'/path/'}
  77. ${'/path/'} | ${'/path/'}
  78. ${'path1/path2'} | ${'/path1/path2'}
  79. ${'/path1/path2'} | ${'/path1/path2'}
  80. ${'path1/path2/'} | ${'/path1/path2/'}
  81. ${'/path1/path2/'} | ${'/path1/path2/'}
  82. ${'//path1/path2//'} | ${'//path1/path2//'}
  83. ${'https://example.com'} | ${'/https://example.com'}
  84. ${'https://example.com/'} | ${'/https://example.com/'}
  85. `(
  86. "should add heading slash to '$path' resulting in '$expected'",
  87. ({ path, expected }) => {
  88. expect(pathUtils.addHeadingSlash(path)).toBe(expected);
  89. },
  90. );
  91. });
  92. describe('.addTrailingSlash', () => {
  93. test.concurrent.each`
  94. path | expected
  95. ${'/'} | ${'/'}
  96. ${''} | ${'/'}
  97. ${'path'} | ${'path/'}
  98. ${'/path'} | ${'/path/'}
  99. ${'path/'} | ${'path/'}
  100. ${'/path/'} | ${'/path/'}
  101. ${'path1/path2'} | ${'path1/path2/'}
  102. ${'/path1/path2'} | ${'/path1/path2/'}
  103. ${'path1/path2/'} | ${'path1/path2/'}
  104. ${'/path1/path2/'} | ${'/path1/path2/'}
  105. ${'//path1/path2//'} | ${'//path1/path2//'}
  106. ${'https://example.com'} | ${'https://example.com/'}
  107. ${'https://example.com/'} | ${'https://example.com/'}
  108. `(
  109. "should add trailing slash to '$path' resulting in '$expected'",
  110. ({ path, expected }) => {
  111. expect(pathUtils.addTrailingSlash(path)).toBe(expected);
  112. },
  113. );
  114. });
  115. describe('.removeHeadingSlash', () => {
  116. test.concurrent.each`
  117. path | expected
  118. ${'/'} | ${'/'}
  119. ${''} | ${''}
  120. ${'path'} | ${'path'}
  121. ${'/path'} | ${'path'}
  122. ${'path/'} | ${'path/'}
  123. ${'/path/'} | ${'path/'}
  124. ${'path1/path2'} | ${'path1/path2'}
  125. ${'/path1/path2'} | ${'path1/path2'}
  126. ${'path1/path2/'} | ${'path1/path2/'}
  127. ${'/path1/path2/'} | ${'path1/path2/'}
  128. ${'//path1/path2//'} | ${'path1/path2//'}
  129. ${'https://example.com'} | ${'https://example.com'}
  130. ${'https://example.com/'} | ${'https://example.com/'}
  131. ${'//'} | ${'/'}
  132. `(
  133. "should remove heading slash from '$path' resulting in '$expected'",
  134. ({ path, expected }) => {
  135. expect(pathUtils.removeHeadingSlash(path)).toBe(expected);
  136. },
  137. );
  138. });
  139. describe('.removeTrailingSlash', () => {
  140. test.concurrent.each`
  141. path | expected
  142. ${'/'} | ${'/'}
  143. ${''} | ${''}
  144. ${'path'} | ${'path'}
  145. ${'/path'} | ${'/path'}
  146. ${'path/'} | ${'path'}
  147. ${'/path/'} | ${'/path'}
  148. ${'path1/path2'} | ${'path1/path2'}
  149. ${'/path1/path2'} | ${'/path1/path2'}
  150. ${'path1/path2/'} | ${'path1/path2'}
  151. ${'/path1/path2/'} | ${'/path1/path2'}
  152. ${'//path1/path2//'} | ${'//path1/path2'}
  153. ${'https://example.com'} | ${'https://example.com'}
  154. ${'https://example.com/'} | ${'https://example.com'}
  155. `(
  156. "should remove trailing slash from '$path' resulting in '$expected'",
  157. ({ path, expected }) => {
  158. expect(pathUtils.removeTrailingSlash(path)).toBe(expected);
  159. },
  160. );
  161. });
  162. describe('.getParentPath', () => {
  163. test.concurrent.each`
  164. path | expected
  165. ${'/'} | ${'/'}
  166. ${''} | ${'/'}
  167. ${'path'} | ${'/'}
  168. ${'/path'} | ${'/'}
  169. ${'path/'} | ${'/path'}
  170. ${'/path/'} | ${'/path'}
  171. ${'path1/path2'} | ${'/path1'}
  172. ${'/path1/path2'} | ${'/path1'}
  173. ${'path1/path2/'} | ${'/path1/path2'}
  174. ${'/path1/path2/'} | ${'/path1/path2'}
  175. ${'//path1/path2//'} | ${'/path1/path2'}
  176. ${'https://example.com'} | ${'/https:'}
  177. ${'https://example.com/'} | ${'/https://example.com'}
  178. ${'/page'} | ${'/'}
  179. `(
  180. "should get parent path of '$path' as '$expected'",
  181. ({ path, expected }) => {
  182. expect(pathUtils.getParentPath(path)).toBe(expected);
  183. },
  184. );
  185. });
  186. });