summaryrefslogtreecommitdiff
path: root/tests/docx/style-map.tests.js
blob: f1ac68dc1c9f7e7ad9d97479bfc35611894405b3 (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
var assert = require("assert");

var JSZip = require("jszip");

var zipfile = require("../../lib/zipfile");
var styleMap = require("../../lib/docx/style-map");
var test = require("../testing").test;

describe("style-map", function() {
    test('embedded style map can be read after being written', function() {
        var zip = normalDocx();
        
        return styleMap.writeStyleMap(zip, "p => h1").then(function() {
            return styleMap.readStyleMap(zip).then(function(contents) {
                assert.equal(contents, "p => h1");
            });
        });
    });
    
    test('embedded style map is written to separate file', function() {
        var zip = normalDocx();
        
        return styleMap.writeStyleMap(zip, "p => h1").then(function() {
            return zip.read("mammoth/style-map", "utf8").then(function(contents) {
                assert.equal(contents, "p => h1");
            });
        });
    });
    
    test('embedded style map is referenced in relationships', function() {
        var zip = normalDocx();
        
        var expectedRelationshipsXml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
            '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">' +
            '<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings" Target="settings.xml"/>' +
            '<Relationship Id="rMammothStyleMap" Type="http://schemas.zwobble.org/mammoth/style-map" Target="/mammoth/style-map"/>' +
            '</Relationships>';
        
        return styleMap.writeStyleMap(zip, "p => h1").then(function() {
            return zip.read("word/_rels/document.xml.rels", "utf8").then(function(contents) {
                assert.equal(contents, expectedRelationshipsXml);
            });
        });
    });
});

function normalDocx() {
    var zip = new JSZip();
    var originalRelationshipsXml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
        '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">' +
        '<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings" Target="settings.xml"/>' +
        '</Relationships>';
    zip.file("word/_rels/document.xml.rels", originalRelationshipsXml);
    var buffer = zip.generate({type: "arraybuffer"});
    return zipfile.openArrayBuffer(buffer);
}