diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-04-25 14:15:28 +0200 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-04-25 14:15:28 +0200 |
commit | 14ce495e9e6f162a74ef1e202fef62855da2a5fc (patch) | |
tree | dcb963e282a3de773059fde982485b2bd93fc32a /lib/utils | |
parent | 4aed2cf65240abb26384d7d86ccb27b171e6bb9a (diff) | |
download | gitbook-14ce495e9e6f162a74ef1e202fef62855da2a5fc.zip gitbook-14ce495e9e6f162a74ef1e202fef62855da2a5fc.tar.gz gitbook-14ce495e9e6f162a74ef1e202fef62855da2a5fc.tar.bz2 |
Add more tests for LocationUtils
Diffstat (limited to 'lib/utils')
-rw-r--r-- | lib/utils/__tests__/location.js | 51 |
1 files changed, 50 insertions, 1 deletions
diff --git a/lib/utils/__tests__/location.js b/lib/utils/__tests__/location.js index 183086d..f2037ff 100644 --- a/lib/utils/__tests__/location.js +++ b/lib/utils/__tests__/location.js @@ -3,7 +3,56 @@ jest.autoMockOff(); describe('LocationUtils', function() { var LocationUtils = require('../location'); - describe('toAbsolute', function() { + it('should correctly test external location', function() { + expect(LocationUtils.isExternal('http://google.fr')).toBe(true); + expect(LocationUtils.isExternal('https://google.fr')).toBe(true); + expect(LocationUtils.isExternal('test.md')).toBe(false); + expect(LocationUtils.isExternal('folder/test.md')).toBe(false); + expect(LocationUtils.isExternal('/folder/test.md')).toBe(false); + }); + + it('should correctly detect anchor location', function() { + expect(LocationUtils.isAnchor('#test')).toBe(true); + expect(LocationUtils.isAnchor(' #test')).toBe(true); + expect(LocationUtils.isAnchor('https://google.fr#test')).toBe(false); + expect(LocationUtils.isAnchor('test.md#test')).toBe(false); + }); + + describe('.relative', function() { + it('should resolve to a relative path (same folder)', function() { + expect(LocationUtils.relative('links/', 'links/test.md')).toBe('test.md'); + }); + + it('should resolve to a relative path (parent folder)', function() { + expect(LocationUtils.relative('links/', 'test.md')).toBe('../test.md'); + }); + + it('should resolve to a relative path (child folder)', function() { + expect(LocationUtils.relative('links/', 'links/hello/test.md')).toBe('hello/test.md'); + }); + }); + + describe('.toAbsolute', function() { + it('should correctly transform as absolute', function() { + expect(LocationUtils.toAbsolute('http://google.fr')).toBe('http://google.fr'); + expect(LocationUtils.toAbsolute('test.md', './', './')).toBe('test.md'); + expect(LocationUtils.toAbsolute('folder/test.md', './', './')).toBe('folder/test.md'); + }); + + it('should correctly handle windows path', function() { + expect(LocationUtils.toAbsolute('folder\\test.md', './', './')).toBe('folder/test.md'); + }); + + it('should correctly handle absolute path', function() { + expect(LocationUtils.toAbsolute('/test.md', './', './')).toBe('test.md'); + expect(LocationUtils.toAbsolute('/test.md', 'test', 'test')).toBe('../test.md'); + expect(LocationUtils.toAbsolute('/sub/test.md', 'test', 'test')).toBe('../sub/test.md'); + expect(LocationUtils.toAbsolute('/test.png', 'folder', '')).toBe('test.png'); + }); + + it('should correctly handle absolute path (windows)', function() { + expect(LocationUtils.toAbsolute('\\test.png', 'folder', '')).toBe('test.png'); + }); it('should resolve path starting by "/" in root directory', function() { expect( |