2020-10-05 16:46:18 +03:00
|
|
|
const w = require("../dist/node");
|
|
|
|
const { readFileSync, writeFileSync } = require("fs");
|
|
|
|
const Zip = require("adm-zip");
|
|
|
|
|
|
|
|
describe("reader", () => {
|
|
|
|
test("should read lvlOverride docx", () => {
|
|
|
|
const buf = readFileSync("../fixtures/lvl_override/override.docx");
|
|
|
|
const json = w.readDocx(buf);
|
2020-10-09 08:11:46 +03:00
|
|
|
expect(json).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
test("should read gridAfter docx", () => {
|
|
|
|
const buf = readFileSync("../fixtures/grid_after/grid_after.docx");
|
|
|
|
const json = w.readDocx(buf);
|
2020-10-30 13:29:06 +02:00
|
|
|
expect(json).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
test("should read table style docx", () => {
|
|
|
|
const buf = readFileSync("../fixtures/table_style/table_style.docx");
|
|
|
|
const json = w.readDocx(buf);
|
2020-10-05 16:46:18 +03:00
|
|
|
expect(json).toMatchSnapshot();
|
2020-12-15 15:33:01 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
test("should read extended comments docx", () => {
|
2020-12-18 09:09:48 +02:00
|
|
|
const buf = readFileSync(
|
|
|
|
"../fixtures/extended_comments/extended_comments.docx"
|
|
|
|
);
|
2020-12-15 15:33:01 +02:00
|
|
|
const json = w.readDocx(buf);
|
|
|
|
expect(json).toMatchSnapshot();
|
2020-10-05 16:46:18 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("writer", () => {
|
|
|
|
test("should write hello", () => {
|
|
|
|
const p = new w.Paragraph().addRun(new w.Run().addText("Hello world!!"));
|
|
|
|
const buf = new w.Docx().addParagraph(p).build();
|
|
|
|
const z = new Zip(Buffer.from(buf));
|
|
|
|
for (const e of z.getEntries()) {
|
|
|
|
if (e.entryName.match(/document.xml|numbering.xml/)) {
|
|
|
|
expect(z.readAsText(e)).toMatchSnapshot();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
test("should write lvlOverride with level", () => {
|
|
|
|
const p = new w.Paragraph()
|
|
|
|
.addRun(new w.Run().addText("Hello world!!"))
|
|
|
|
.numbering(0, 0);
|
|
|
|
const num = new w.Numbering(0, 0);
|
|
|
|
num.addOverride(
|
|
|
|
new w.LevelOverride(0).overrideLevel(
|
|
|
|
new w.Level(0, 3, "decimal", "%1", "left")
|
|
|
|
)
|
|
|
|
);
|
|
|
|
const buf = new w.Docx()
|
|
|
|
.addParagraph(p)
|
|
|
|
.addAbstractNumbering(new w.AbstractNumbering(0))
|
|
|
|
.addNumbering(num)
|
|
|
|
.build();
|
|
|
|
|
|
|
|
const z = new Zip(Buffer.from(buf));
|
|
|
|
for (const e of z.getEntries()) {
|
|
|
|
if (e.entryName.match(/document.xml|numbering.xml/)) {
|
|
|
|
expect(z.readAsText(e)).toMatchSnapshot();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2020-10-09 14:30:55 +03:00
|
|
|
|
|
|
|
test("should write page size", () => {
|
|
|
|
const p = new w.Paragraph().addRun(new w.Run().addText("Hello world!!"));
|
|
|
|
const buf = new w.Docx().addParagraph(p).pageSize(400, 800).build();
|
|
|
|
const z = new Zip(Buffer.from(buf));
|
|
|
|
for (const e of z.getEntries()) {
|
|
|
|
if (e.entryName.match(/document.xml|numbering.xml/)) {
|
|
|
|
expect(z.readAsText(e)).toMatchSnapshot();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2020-10-12 18:04:09 +03:00
|
|
|
|
|
|
|
test("should write page margin", () => {
|
|
|
|
const p = new w.Paragraph().addRun(new w.Run().addText("Hello world!!"));
|
|
|
|
const buf = new w.Docx()
|
|
|
|
.addParagraph(p)
|
|
|
|
.pageMargin({ top: 1000, left: 2000 })
|
|
|
|
.build();
|
2020-12-14 08:01:23 +02:00
|
|
|
const z = new Zip(Buffer.from(buf));
|
|
|
|
for (const e of z.getEntries()) {
|
|
|
|
if (e.entryName.match(/document.xml|numbering.xml/)) {
|
|
|
|
expect(z.readAsText(e)).toMatchSnapshot();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
test("should write default font", () => {
|
|
|
|
const p = new w.Paragraph().addRun(new w.Run().addText("Hello world!!!!"));
|
|
|
|
const fonts = new w.RunFonts()
|
|
|
|
.eastAsia("Arial")
|
|
|
|
.ascii("Arial")
|
|
|
|
.hiAnsi("Arial");
|
|
|
|
const buf = new w.Docx()
|
|
|
|
.addParagraph(p)
|
|
|
|
.defaultSize(40)
|
|
|
|
.defaultFonts(fonts)
|
|
|
|
.build();
|
2020-12-15 08:38:17 +02:00
|
|
|
writeFileSync("../output/default_font.docx", buf);
|
|
|
|
const z = new Zip(Buffer.from(buf));
|
|
|
|
for (const e of z.getEntries()) {
|
|
|
|
if (e.entryName.match(/document.xml|numbering.xml/)) {
|
|
|
|
expect(z.readAsText(e)).toMatchSnapshot();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
test("should write doc vars", () => {
|
|
|
|
const p = new w.Paragraph().addRun(new w.Run().addText("Hello world!!!!"));
|
|
|
|
const buf = new w.Docx().addParagraph(p).addDocVar("foo", "bar").build();
|
|
|
|
writeFileSync("../output/doc_vars.docx", buf);
|
2020-10-12 18:04:09 +03:00
|
|
|
const z = new Zip(Buffer.from(buf));
|
|
|
|
for (const e of z.getEntries()) {
|
|
|
|
if (e.entryName.match(/document.xml|numbering.xml/)) {
|
|
|
|
expect(z.readAsText(e)).toMatchSnapshot();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2020-10-05 16:46:18 +03:00
|
|
|
});
|