summaryrefslogtreecommitdiff
path: root/tests/mammoth.tests.js
blob: 90b96e53c2901bbe37097e3c80901e2e017651d4 (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
101
102
103
104
105
106
107
108
109
var assert = require("assert");
var path = require("path");
var fs = require("fs");

var promises = require("../lib/promises");

var mammoth = require("../")
var htmlPaths = require("../lib/html-paths");
var documentMatchers = require("../lib/document-matchers");

var testing = require("./testing");
var test = testing.test;
var testData = testing.testData;
var createFakeDocxFile = testing.createFakeDocxFile;


describe('mammoth', function() {
    test('should convert docx containing one paragraph to single p element', function() {
        var docxPath = path.join(__dirname, "test-data/single-paragraph.docx");
        return mammoth.convertToHtml({path: docxPath}).then(function(result) {
            assert.equal(result.value, "<p>Walking on imported air</p>");
            assert.deepEqual(result.messages, []);
        });
    });
    
    test('style map can be expressed as string', function() {
        var docxFile = createFakeDocxFile({
            "word/document.xml": testData("simple/word/document.xml")
        });
        var options = {
            styleMap: "p => h1"
        };
        return mammoth.convertToHtml({file: docxFile}, options).then(function(result) {
            assert.equal("<h1>Hello.</h1>", result.value);
        });
    });
    
    test('style map can be expressed as array of style mappings', function() {
        var docxFile = createFakeDocxFile({
            "word/document.xml": testData("simple/word/document.xml")
        });
        var options = {
            styleMap: ["p => h1"]
        };
        return mammoth.convertToHtml({file: docxFile}, options).then(function(result) {
            assert.equal("<h1>Hello.</h1>", result.value);
        });
    });
    
    test('options are passed to document converter when calling mammoth.convertToHtml', function() {
        var docxFile = createFakeDocxFile({
            "word/document.xml": testData("simple/word/document.xml")
        });
        var options = {
            styleMap: "p => h1"
        };
        return mammoth.convertToHtml({file: docxFile}, options).then(function(result) {
            assert.equal("<h1>Hello.</h1>", result.value);
        });
    });
    
    test('options.transformDocument is used to transform document if set', function() {
        var docxFile = createFakeDocxFile({
            "word/document.xml": testData("simple/word/document.xml")
        });
        var options = {
            transformDocument: function(document) {
                document.children[0].styleId = "Heading1";
                return document;
            }
        };
        return mammoth.convertToHtml({file: docxFile}, options).then(function(result) {
            assert.equal("<h1>Hello.</h1>", result.value);
        });
    });
    
    test('inline images are included in output', function() {
        var docxPath = path.join(__dirname, "test-data/tiny-picture.docx");
        return mammoth.convertToHtml({path: docxPath}).then(function(result) {
            assert.equal(result.value, '<p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAOvgAADr4B6kKxwAAAABNJREFUKFNj/M+ADzDhlWUYqdIAQSwBE8U+X40AAAAASUVORK5CYII=" /></p>');
        });
    });
    
    test('simple list is converted to list elements', function() {
        var docxPath = path.join(__dirname, "test-data/simple-list.docx");
        return mammoth.convertToHtml({path: docxPath}).then(function(result) {
            assert.equal(result.value, '<ul><li>Apple</li><li>Banana</li></ul>');
        });
    });
    
    test('indentation is used if prettyPrint is true', function() {
        var docxPath = path.join(__dirname, "test-data/single-paragraph.docx");
        return mammoth.convertToHtml({path: docxPath}, {prettyPrint: true}).then(function(result) {
            assert.equal(result.value, "<p>\n  Walking on imported air\n</p>");
            assert.deepEqual(result.messages, []);
        });
    });
    
    test('using styleMapping throws error', function() {
        try {
            mammoth.styleMapping()
        } catch (error) {
            assert.equal(
                error.message,
                'Use a raw string instead of mammoth.styleMapping e.g. "p[style-naming=\'Title\'] => h1" instead of mammoth.styleMapping("p[style-naming=\'Title\'] => h1")'
            );
        }
    });
})