summaryrefslogtreecommitdiffstats
path: root/test/links.js
blob: 1d835111a3f30475d54cb991aa9e0bff6e7f1f1f (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
var should = require('should');
var fs = require('fs');
var _ = require('lodash');
var path = require('path');
var cheerio = require('cheerio');

var links = require("../lib/utils/links");

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

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

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

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

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

    describe('page', function() {
        var book;

        before(function() {
            return books.generate("links", "website")
                .then(function(_book) {
                    book = _book;
                });
        });

        it('should correctly replace relative links', function() {
            var readme = fs.readFileSync(
                path.join(book.options.output, "folder1/index.html"),
                { encoding: "utf-8" }
            );
            var $ = cheerio.load(readme);
            var $a = $(".page-inner a");

            $a.attr('href').should.be.exactly("../folder2/index.html");
        })
    });

});