diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-07-03 19:19:21 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-07-03 19:20:00 +0100 |
| commit | 0b56dfce42c3944cce47e284608e6806f4c839c3 (patch) | |
| tree | 5f96f9c2908f1c5b00a7192e71d4c192abd7cf47 /test/docx/style-map.tests.js | |
| parent | 627c4a755395e313234bdb1914d168392006c483 (diff) | |
Move tests to test for easier Mocha usage
Diffstat (limited to 'test/docx/style-map.tests.js')
| -rw-r--r-- | test/docx/style-map.tests.js | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/test/docx/style-map.tests.js b/test/docx/style-map.tests.js new file mode 100644 index 0000000..55171f8 --- /dev/null +++ b/test/docx/style-map.tests.js @@ -0,0 +1,115 @@ +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('reading embedded style map on document without embedded style map returns null', function() { + var zip = normalDocx(); + + return styleMap.readStyleMap(zip).then(function(contents) { + assert.equal(contents, null); + }); + }); + + 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(); + + return styleMap.writeStyleMap(zip, "p => h1").then(function() { + return zip.read("word/_rels/document.xml.rels", "utf8").then(function(contents) { + assert.equal(contents, expectedRelationshipsXml); + }); + }); + }); + + test('re-embedding style map replaces original', function() { + var zip = normalDocx(); + + return styleMap.writeStyleMap(zip, "p => h1").then(function() { + return styleMap.writeStyleMap(zip, "p => h2"); + }).then(function() { + return zip.read("word/_rels/document.xml.rels", "utf8").then(function(contents) { + assert.equal(contents, expectedRelationshipsXml); + }); + }).then(function() { + return styleMap.readStyleMap(zip).then(function(contents) { + assert.equal(contents, "p => h2"); + }); + }); + }); + + test('embedded style map has override content type in [Content_Types].xml', function() { + var zip = normalDocx(); + + return styleMap.writeStyleMap(zip, "p => h1").then(function() { + return zip.read("[Content_Types].xml", "utf8").then(function(contents) { + assert.equal(contents, expectedContentTypesXml); + }); + }); + }); + + test('replacing style map keeps content type', function() { + var zip = normalDocx(); + + return styleMap.writeStyleMap(zip, "p => h1").then(function() { + return styleMap.writeStyleMap(zip, "p => h2"); + }).then(function() { + return zip.read("[Content_Types].xml", "utf8").then(function(contents) { + assert.equal(contents, expectedContentTypesXml); + }); + }); + }); + +}); + +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>'; + +var expectedContentTypesXml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' + + '<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">' + + '<Default Extension="png" ContentType="image/png"/>' + + '<Override PartName="/mammoth/style-map" ContentType="text/prs.mammoth.style-map"/>' + + '</Types>'; + +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>'; + var originalContentTypesXml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' + + '<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">' + + '<Default Extension="png" ContentType="image/png"/>' + + '</Types>'; + zip.file("word/_rels/document.xml.rels", originalRelationshipsXml); + zip.file("[Content_Types].xml", originalContentTypesXml); + var buffer = zip.generate({type: "arraybuffer"}); + return zipfile.openArrayBuffer(buffer); +} + |
