Files
docx-js/src/file/core-properties/properties.spec.ts

73 lines
3.2 KiB
TypeScript
Raw Normal View History

import { expect } from "chai";
2016-04-03 20:00:30 +01:00
2018-10-26 01:04:07 +01:00
import { Formatter } from "export/formatter";
2018-02-04 01:43:03 +00:00
import { CoreProperties } from "./properties";
2016-04-03 20:00:30 +01:00
describe("Properties", () => {
describe("#constructor()", () => {
it("sets the appropriate attributes on the top-level", () => {
2018-02-04 01:43:03 +00:00
const properties = new CoreProperties({});
const tree = new Formatter().format(properties);
expect(Object.keys(tree)).to.deep.equal(["cp:coreProperties"]);
expect(tree["cp:coreProperties"]).to.be.an.instanceof(Array);
expect(tree["cp:coreProperties"][0]).to.deep.equal({
_attr: {
"xmlns:cp": "http://schemas.openxmlformats.org/package/2006/metadata/core-properties",
"xmlns:dc": "http://purl.org/dc/elements/1.1/",
"xmlns:dcmitype": "http://purl.org/dc/dcmitype/",
"xmlns:dcterms": "http://purl.org/dc/terms/",
"xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
},
});
});
2016-04-03 20:00:30 +01:00
it("should create properties with a title", () => {
2018-02-04 01:43:03 +00:00
const properties = new CoreProperties({ title: "test document" });
const tree = new Formatter().format(properties);
expect(Object.keys(tree)).to.deep.equal(["cp:coreProperties"]);
expect(tree["cp:coreProperties"]).to.be.an.instanceof(Array);
expect(Object.keys(tree["cp:coreProperties"][0])).to.deep.equal(["_attr"]);
2018-01-23 01:33:12 +00:00
expect(tree["cp:coreProperties"][1]).to.deep.equal({ "dc:title": ["test document"] });
});
it("should create properties with all the attributes given", () => {
2018-02-04 01:43:03 +00:00
const properties = new CoreProperties({
title: "test document",
subject: "test subject",
creator: "me",
keywords: "test docx",
description: "testing document",
lastModifiedBy: "the author",
revision: "123",
2016-04-03 20:00:30 +01:00
});
const tree = new Formatter().format(properties);
expect(Object.keys(tree)).to.deep.equal(["cp:coreProperties"]);
expect(tree["cp:coreProperties"]).to.be.an.instanceof(Array);
const key = (obj) => Object.keys(obj)[0];
const props = tree["cp:coreProperties"].map(key).sort();
expect(props).to.deep.equal([
"_attr",
"cp:keywords",
"cp:lastModifiedBy",
"cp:revision",
"dc:creator",
"dc:description",
"dc:subject",
"dc:title",
"dcterms:created",
"dcterms:modified",
]);
2018-01-23 01:33:12 +00:00
expect(tree["cp:coreProperties"].slice(1, -2).sort((a, b) => (key(a) < key(b) ? -1 : 1))).to.deep.equal([
{ "cp:keywords": ["test docx"] },
{ "cp:lastModifiedBy": ["the author"] },
{ "cp:revision": ["123"] },
{ "dc:creator": ["me"] },
{ "dc:description": ["testing document"] },
{ "dc:subject": ["test subject"] },
{ "dc:title": ["test document"] },
]);
2016-04-03 20:00:30 +01:00
});
2016-05-26 15:08:34 +01:00
});
});