|
|
@@ -1,26 +1,177 @@
|
|
|
import * as pathUtils from './path-utils';
|
|
|
|
|
|
-
|
|
|
describe('page-utils', () => {
|
|
|
+
|
|
|
describe('.normalizePath', () => {
|
|
|
- test.concurrent('should return the root path with empty string', () => {
|
|
|
- expect(pathUtils.normalizePath('')).toBe('/');
|
|
|
+ test.concurrent.each`
|
|
|
+ path | expected
|
|
|
+ ${'/'} | ${'/'}
|
|
|
+ ${''} | ${'/'}
|
|
|
+ ${'path'} | ${'/path'}
|
|
|
+ ${'/path'} | ${'/path'}
|
|
|
+ ${'path/'} | ${'/path'}
|
|
|
+ ${'/path/'} | ${'/path'}
|
|
|
+ ${'path1/path2'} | ${'/path1/path2'}
|
|
|
+ ${'/path1/path2'} | ${'/path1/path2'}
|
|
|
+ ${'path1/path2/'} | ${'/path1/path2'}
|
|
|
+ ${'/path1/path2/'} | ${'/path1/path2'}
|
|
|
+ ${'//path1/path2//'} | ${'/path1/path2'}
|
|
|
+ ${'https://example.com'} | ${'/https://example.com'}
|
|
|
+ ${'https://example.com/'} | ${'/https://example.com'}
|
|
|
+ `('should normalize \'$path\' to \'$expected\'', ({ path, expected }) => {
|
|
|
+ expect(pathUtils.normalizePath(path)).toBe(expected);
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ describe('.hasHeadingSlash', () => {
|
|
|
+ test.concurrent.each`
|
|
|
+ path | expected
|
|
|
+ ${'/'} | ${true}
|
|
|
+ ${''} | ${false}
|
|
|
+ ${'path'} | ${false}
|
|
|
+ ${'/path'} | ${true}
|
|
|
+ ${'path/'} | ${false}
|
|
|
+ ${'/path/'} | ${true}
|
|
|
+ ${'path1/path2'} | ${false}
|
|
|
+ ${'/path1/path2'} | ${true}
|
|
|
+ ${'path1/path2/'} | ${false}
|
|
|
+ ${'/path1/path2/'} | ${true}
|
|
|
+ ${'//path1/path2//'} | ${true}
|
|
|
+ ${'https://example.com'} | ${false}
|
|
|
+ ${'https://example.com/'} | ${false}
|
|
|
+ `('should return $expected when checking heading slash for \'$path\'', ({ path, expected }) => {
|
|
|
+ expect(pathUtils.hasHeadingSlash(path)).toBe(expected);
|
|
|
});
|
|
|
+ });
|
|
|
|
|
|
- test.concurrent('should return the root path as is', () => {
|
|
|
- expect(pathUtils.normalizePath('/')).toBe('/');
|
|
|
+ describe('.hasTrailingSlash', () => {
|
|
|
+ test.concurrent.each`
|
|
|
+ path | expected
|
|
|
+ ${'/'} | ${true}
|
|
|
+ ${''} | ${false}
|
|
|
+ ${'path'} | ${false}
|
|
|
+ ${'/path'} | ${false}
|
|
|
+ ${'path/'} | ${true}
|
|
|
+ ${'/path/'} | ${true}
|
|
|
+ ${'path1/path2'} | ${false}
|
|
|
+ ${'/path1/path2'} | ${false}
|
|
|
+ ${'path1/path2/'} | ${true}
|
|
|
+ ${'/path1/path2/'} | ${true}
|
|
|
+ ${'//path1/path2//'} | ${true}
|
|
|
+ ${'https://example.com'} | ${false}
|
|
|
+ ${'https://example.com/'} | ${true}
|
|
|
+ `('should return $expected when checking trailing slash for \'$path\'', ({ path, expected }) => {
|
|
|
+ expect(pathUtils.hasTrailingSlash(path)).toBe(expected);
|
|
|
});
|
|
|
+ });
|
|
|
|
|
|
- test.concurrent('should add heading slash', () => {
|
|
|
- expect(pathUtils.normalizePath('hoge/fuga')).toBe('/hoge/fuga');
|
|
|
+ describe('.addHeadingSlash', () => {
|
|
|
+ test.concurrent.each`
|
|
|
+ path | expected
|
|
|
+ ${'/'} | ${'/'}
|
|
|
+ ${''} | ${'/'}
|
|
|
+ ${'path'} | ${'/path'}
|
|
|
+ ${'/path'} | ${'/path'}
|
|
|
+ ${'path/'} | ${'/path/'}
|
|
|
+ ${'/path/'} | ${'/path/'}
|
|
|
+ ${'path1/path2'} | ${'/path1/path2'}
|
|
|
+ ${'/path1/path2'} | ${'/path1/path2'}
|
|
|
+ ${'path1/path2/'} | ${'/path1/path2/'}
|
|
|
+ ${'/path1/path2/'} | ${'/path1/path2/'}
|
|
|
+ ${'//path1/path2//'} | ${'//path1/path2//'}
|
|
|
+ ${'https://example.com'} | ${'/https://example.com'}
|
|
|
+ ${'https://example.com/'} | ${'/https://example.com/'}
|
|
|
+ `('should add heading slash to \'$path\' resulting in \'$expected\'', ({ path, expected }) => {
|
|
|
+ expect(pathUtils.addHeadingSlash(path)).toBe(expected);
|
|
|
});
|
|
|
+ });
|
|
|
|
|
|
- test.concurrent('should remove trailing slash', () => {
|
|
|
- expect(pathUtils.normalizePath('/hoge/fuga/')).toBe('/hoge/fuga');
|
|
|
+ describe('.addTrailingSlash', () => {
|
|
|
+ test.concurrent.each`
|
|
|
+ path | expected
|
|
|
+ ${'/'} | ${'/'}
|
|
|
+ ${''} | ${'/'}
|
|
|
+ ${'path'} | ${'path/'}
|
|
|
+ ${'/path'} | ${'/path/'}
|
|
|
+ ${'path/'} | ${'path/'}
|
|
|
+ ${'/path/'} | ${'/path/'}
|
|
|
+ ${'path1/path2'} | ${'path1/path2/'}
|
|
|
+ ${'/path1/path2'} | ${'/path1/path2/'}
|
|
|
+ ${'path1/path2/'} | ${'path1/path2/'}
|
|
|
+ ${'/path1/path2/'} | ${'/path1/path2/'}
|
|
|
+ ${'//path1/path2//'} | ${'//path1/path2//'}
|
|
|
+ ${'https://example.com'} | ${'https://example.com/'}
|
|
|
+ ${'https://example.com/'} | ${'https://example.com/'}
|
|
|
+ `('should add trailing slash to \'$path\' resulting in \'$expected\'', ({ path, expected }) => {
|
|
|
+ expect(pathUtils.addTrailingSlash(path)).toBe(expected);
|
|
|
});
|
|
|
+ });
|
|
|
+
|
|
|
+ describe('.removeHeadingSlash', () => {
|
|
|
+ test.concurrent.each`
|
|
|
+ path | expected
|
|
|
+ ${'/'} | ${'/'}
|
|
|
+ ${''} | ${''}
|
|
|
+ ${'path'} | ${'path'}
|
|
|
+ ${'/path'} | ${'path'}
|
|
|
+ ${'path/'} | ${'path/'}
|
|
|
+ ${'/path/'} | ${'path/'}
|
|
|
+ ${'path1/path2'} | ${'path1/path2'}
|
|
|
+ ${'/path1/path2'} | ${'path1/path2'}
|
|
|
+ ${'path1/path2/'} | ${'path1/path2/'}
|
|
|
+ ${'/path1/path2/'} | ${'path1/path2/'}
|
|
|
+ ${'//path1/path2//'} | ${'path1/path2//'}
|
|
|
+ ${'https://example.com'} | ${'https://example.com'}
|
|
|
+ ${'https://example.com/'} | ${'https://example.com/'}
|
|
|
+ ${'//'} | ${'/'} // from former specific test
|
|
|
+ `('should remove heading slash from \'$path\' resulting in \'$expected\'', ({ path, expected }) => {
|
|
|
+ expect(pathUtils.removeHeadingSlash(path)).toBe(expected);
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ describe('.removeTrailingSlash', () => {
|
|
|
+ test.concurrent.each`
|
|
|
+ path | expected
|
|
|
+ ${'/'} | ${'/'}
|
|
|
+ ${''} | ${''}
|
|
|
+ ${'path'} | ${'path'}
|
|
|
+ ${'/path'} | ${'/path'}
|
|
|
+ ${'path/'} | ${'path'}
|
|
|
+ ${'/path/'} | ${'/path'}
|
|
|
+ ${'path1/path2'} | ${'path1/path2'}
|
|
|
+ ${'/path1/path2'} | ${'/path1/path2'}
|
|
|
+ ${'path1/path2/'} | ${'path1/path2'}
|
|
|
+ ${'/path1/path2/'} | ${'/path1/path2'}
|
|
|
+ ${'//path1/path2//'} | ${'//path1/path2'}
|
|
|
+ ${'https://example.com'} | ${'https://example.com'}
|
|
|
+ ${'https://example.com/'} | ${'https://example.com'}
|
|
|
+ `('should remove trailing slash from \'$path\' resulting in \'$expected\'', ({ path, expected }) => {
|
|
|
+ expect(pathUtils.removeTrailingSlash(path)).toBe(expected);
|
|
|
+ });
|
|
|
+ });
|
|
|
|
|
|
- test.concurrent('should remove unnecessary slashes', () => {
|
|
|
- expect(pathUtils.normalizePath('//hoge/fuga//')).toBe('/hoge/fuga');
|
|
|
+ describe('.getParentPath', () => {
|
|
|
+ test.concurrent.each`
|
|
|
+ path | expected
|
|
|
+ ${'/'} | ${'/'}
|
|
|
+ ${''} | ${'/'}
|
|
|
+ ${'path'} | ${'/'}
|
|
|
+ ${'/path'} | ${'/'}
|
|
|
+ ${'path/'} | ${'/path'}
|
|
|
+ ${'/path/'} | ${'/path'}
|
|
|
+ ${'path1/path2'} | ${'/path1'}
|
|
|
+ ${'/path1/path2'} | ${'/path1'}
|
|
|
+ ${'path1/path2/'} | ${'/path1/path2'}
|
|
|
+ ${'/path1/path2/'} | ${'/path1/path2'}
|
|
|
+ ${'//path1/path2//'} | ${'/path1/path2'}
|
|
|
+ ${'https://example.com'} | ${'/https:'}
|
|
|
+ ${'https://example.com/'} | ${'/https://example.com'}
|
|
|
+ ${'/page'} | ${'/'} // from former specific test
|
|
|
+ // Note: getParentPath('page') is covered by 'path' -> '/'
|
|
|
+ // Note: getParentPath('/path1/path2') is covered by '/path1/path2' -> '/path1'
|
|
|
+ // Note: getParentPath('/path1/path2/') is covered by '/path1/path2/' -> '/path1/path2'
|
|
|
+ `('should get parent path of \'$path\' as \'$expected\'', ({ path, expected }) => {
|
|
|
+ expect(pathUtils.getParentPath(path)).toBe(expected);
|
|
|
});
|
|
|
});
|
|
|
});
|