Merge pull request #1589 from dolanmiu/feat/1583-fix-comments-missing

#1583 Comments to always write default data
This commit is contained in:
Dolan
2022-07-12 18:33:13 +01:00
committed by GitHub
4 changed files with 51 additions and 32 deletions

8
.nycrc
View File

@ -1,9 +1,9 @@
{ {
"check-coverage": true, "check-coverage": true,
"statements": 99.43, "statements": 99.62,
"branches": 96.6, "branches": 96.81,
"functions": 99.47, "functions": 99.82,
"lines": 99.43, "lines": 99.62,
"include": [ "include": [
"src/**/*.ts" "src/**/*.ts"
], ],

View File

@ -4,7 +4,8 @@ import * as sinon from "sinon";
import { File } from "@file/file"; import { File } from "@file/file";
import { Footer, Header } from "@file/header"; import { Footer, Header } from "@file/header";
import { Paragraph } from "@file/paragraph"; import { ImageRun, Paragraph } from "@file/paragraph";
import * as convenienceFunctions from "@util/convenience-functions";
import { Compiler } from "./next-compiler"; import { Compiler } from "./next-compiler";
@ -15,6 +16,14 @@ describe("Compiler", () => {
compiler = new Compiler(); compiler = new Compiler();
}); });
before(() => {
sinon.stub(convenienceFunctions, "uniqueId").callsFake(() => "test");
});
after(() => {
(convenienceFunctions.uniqueId as sinon.SinonStub).restore();
});
describe("#compile()", () => { describe("#compile()", () => {
it("should pack all the content", async function () { it("should pack all the content", async function () {
this.timeout(99999999); this.timeout(99999999);
@ -111,18 +120,38 @@ describe("Compiler", () => {
const spy = sinon.spy(compiler["formatter"], "format"); const spy = sinon.spy(compiler["formatter"], "format");
compiler.compile(file); compiler.compile(file);
expect(spy.callCount).to.equal(12); expect(spy.callCount).to.equal(13);
}); });
it("should work with media datas", () => { it("should work with media datas", () => {
// This test is required because before, there was a case where Document was formatted twice, which was inefficient // This test is required because before, there was a case where Document was formatted twice, which was inefficient
// This also caused issues such as running prepForXml multiple times as format() was ran multiple times. // This also caused issues such as running prepForXml multiple times as format() was ran multiple times.
const paragraph = new Paragraph("");
const file = new File({ const file = new File({
sections: [ sections: [
{ {
properties: {}, headers: {
children: [paragraph], default: new Header({
children: [new Paragraph("test")],
}),
},
footers: {
default: new Footer({
children: [new Paragraph("test")],
}),
},
children: [
new Paragraph({
children: [
new ImageRun({
data: Buffer.from("", "base64"),
transformation: {
width: 100,
height: 100,
},
}),
],
}),
],
}, },
], ],
}); });

View File

@ -406,26 +406,19 @@ export class Compiler {
path: "word/settings.xml", path: "word/settings.xml",
}, },
Comments: { Comments: {
data: (() => { data: xml(
if (!file.Comments) { this.formatter.format(file.Comments, {
return; viewWrapper: file.Document,
} file,
}),
const data = xml( {
this.formatter.format(file.Comments, { indent: prettify,
viewWrapper: file.Document, declaration: {
file, standalone: "yes",
}), encoding: "UTF-8",
{
indent: prettify,
declaration: {
standalone: "yes",
encoding: "UTF-8",
},
}, },
); },
return data; ),
})(),
path: "word/comments.xml", path: "word/comments.xml",
}, },
}; };

View File

@ -71,10 +71,7 @@ export class File {
}, },
); );
if (options.comments) { this.comments = new Comments(options.comments ?? { children: [] });
this.comments = new Comments(options.comments);
}
this.fileRelationships = new Relationships(); this.fileRelationships = new Relationships();
this.customProperties = new CustomProperties(options.customProperties ?? []); this.customProperties = new CustomProperties(options.customProperties ?? []);
this.appProperties = new AppProperties(); this.appProperties = new AppProperties();