summaryrefslogtreecommitdiffstats
path: root/lib/utils/__tests__/location.js
blob: 822338eb3b97761a748065189eb6a2b2dc1f6ba1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
var LocationUtils = require('../location');

describe('LocationUtils', 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);
        expect(LocationUtils.isExternal('data:image/png')).toBe(false);
    });

    it('should correctly test data:uri location', function() {
        expect(LocationUtils.isDataURI('data:image/png')).toBe(true);
        expect(LocationUtils.isDataURI('http://google.fr')).toBe(false);
        expect(LocationUtils.isDataURI('https://google.fr')).toBe(false);
        expect(LocationUtils.isDataURI('test.md')).toBe(false);
        expect(LocationUtils.isDataURI('data.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('.flatten', function() {
        it('should remove leading slash', function() {
            expect(LocationUtils.flatten('/test.md')).toBe('test.md');
            expect(LocationUtils.flatten('/hello/cool.md')).toBe('hello/cool.md');
        });

        it('should remove leading slashes', function() {
            expect(LocationUtils.flatten('///test.md')).toBe('test.md');
        });

        it('should not break paths', function() {
            expect(LocationUtils.flatten('hello/cool.md')).toBe('hello/cool.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(
                LocationUtils.toAbsolute('/test/hello.md', './', './')
            ).toBe('test/hello.md');
        });

        it('should resolve path starting by "/" in child directory', function() {
            expect(
                LocationUtils.toAbsolute('/test/hello.md', './hello', './')
            ).toBe('test/hello.md');
        });

        it('should resolve path starting by "/" in child directory, with same output directory', function() {
            expect(
                LocationUtils.toAbsolute('/test/hello.md', './hello', './hello')
            ).toBe('../test/hello.md');
        });
    });

});