summaryrefslogtreecommitdiffstats
path: root/test/location.js
blob: 3e2294ea6acd5b58f6170e61c3c7f39bd5038e60 (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
var location = require('../lib/utils/location');

describe('Location', function() {
    it('should correctly test external location', function() {
        location.isExternal('http://google.fr').should.be.exactly(true);
        location.isExternal('https://google.fr').should.be.exactly(true);
        location.isExternal('test.md').should.be.exactly(false);
        location.isExternal('folder/test.md').should.be.exactly(false);
        location.isExternal('/folder/test.md').should.be.exactly(false);
    });

    it('should correctly detect anchor location', function() {
        location.isAnchor('#test').should.be.exactly(true);
        location.isAnchor(' #test').should.be.exactly(true);
        location.isAnchor('https://google.fr#test').should.be.exactly(false);
        location.isAnchor('test.md#test').should.be.exactly(false);
    });

    describe('.relative', function() {
        it('should resolve to a relative path (same folder)', function() {
            location.relative('links/', 'links/test.md').should.equal('test.md');
        });

        it('should resolve to a relative path (parent folder)', function() {
            location.relative('links/', 'test.md').should.equal('../test.md');
        });

        it('should resolve to a relative path (child folder)', function() {
            location.relative('links/', 'links/hello/test.md').should.equal('hello/test.md');
        });
    });

    describe('.toAbsolute', function() {
        it('should correctly transform as absolute', function() {
            location.toAbsolute('http://google.fr').should.be.equal('http://google.fr');
            location.toAbsolute('test.md', './', './').should.be.equal('test.md');
            location.toAbsolute('folder/test.md', './', './').should.be.equal('folder/test.md');
        });

        it('should correctly handle windows path', function() {
            location.toAbsolute('folder\\test.md', './', './').should.be.equal('folder/test.md');
        });

        it('should correctly handle absolute path', function() {
            location.toAbsolute('/test.md', './', './').should.be.equal('test.md');
            location.toAbsolute('/test.md', 'test', 'test').should.be.equal('../test.md');
            location.toAbsolute('/sub/test.md', 'test', 'test').should.be.equal('../sub/test.md');
            location.toAbsolute('/test.png', 'folder', '').should.be.equal('test.png');
        });

        it('should correctly handle absolute path (windows)', function() {
            location.toAbsolute('\\test.png', 'folder', '').should.be.equal('test.png');
        });
    });
});