Fix tests

This commit is contained in:
Dolan Miu
2023-06-07 15:11:47 +01:00
parent 972fa77265
commit a0a88412ff
5 changed files with 942 additions and 49 deletions

893
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -32,7 +32,6 @@
"style": "prettier -l \"{src,scripts,demo}/**/*.{ts,html}\"", "style": "prettier -l \"{src,scripts,demo}/**/*.{ts,html}\"",
"style.fix": "npm run style -- --write", "style.fix": "npm run style -- --write",
"cspell": "cspell \"{src,demo,docs,scripts}/**/*.{ts,scss,html,md}\" && cspell \"./*.*\"", "cspell": "cspell \"{src,demo,docs,scripts}/**/*.{ts,scss,html,md}\" && cspell \"./*.*\"",
"fix-types": "ts-node --skip-project scripts/types-absolute-fixer.ts",
"e2e": "ts-node scripts/e2e.ts", "e2e": "ts-node scripts/e2e.ts",
"serve.docs": "cd docs && docsify serve", "serve.docs": "cd docs && docsify serve",
"extract": "ts-node --project tsconfig.spec.json scripts/extract-document.ts", "extract": "ts-node --project tsconfig.spec.json scripts/extract-document.ts",
@ -99,6 +98,7 @@
"eslint-plugin-unicorn": "^46.0.0", "eslint-plugin-unicorn": "^46.0.0",
"execa": "^7.1.1", "execa": "^7.1.1",
"glob": "^9.3.0", "glob": "^9.3.0",
"jsdom": "^22.1.0",
"jszip": "^3.1.5", "jszip": "^3.1.5",
"mocha": "^10.0.0", "mocha": "^10.0.0",
"nyc": "^15.1.0", "nyc": "^15.1.0",

View File

@ -1,6 +1,7 @@
/* tslint:disable:typedef space-before-function-paren */ /* tslint:disable:typedef space-before-function-paren */
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import * as sinon from "sinon"; import * as sinon from "sinon";
import * as fflate from "fflate";
import { File } from "@file/file"; import { File } from "@file/file";
import { Footer, Header } from "@file/header"; import { Footer, Header } from "@file/header";
@ -9,6 +10,21 @@ import * as convenienceFunctions from "@util/convenience-functions";
import { Compiler } from "./next-compiler"; import { Compiler } from "./next-compiler";
const unzip = (zipFile: Uint8Array): Promise<ReadonlySet<string>> => {
const set = new Set<string>();
const unzipper = new fflate.Unzip((file) => {
set.add(file.name);
});
return new Promise<ReadonlySet<string>>((resolve) => {
setTimeout(() => {
resolve(set);
}, 1000);
unzipper.push(zipFile, true);
});
};
describe("Compiler", () => { describe("Compiler", () => {
let compiler: Compiler; let compiler: Compiler;
@ -35,10 +51,10 @@ describe("Compiler", () => {
}, },
}); });
const zipFile = await compiler.compile(file); const zipFile = await compiler.compile(file);
const fileNames = Object.keys(zipFile.files).map((f) => zipFile.files[f].name); const fileNames = await unzip(zipFile);
expect(fileNames).is.an.instanceof(Array); expect(fileNames).has.length(13);
expect(fileNames).has.length(17); expect(fileNames).to.include("word/document.xml");
expect(fileNames).to.include("word/document.xml"); expect(fileNames).to.include("word/document.xml");
expect(fileNames).to.include("word/styles.xml"); expect(fileNames).to.include("word/styles.xml");
expect(fileNames).to.include("docProps/core.xml"); expect(fileNames).to.include("docProps/core.xml");
@ -60,7 +76,7 @@ describe("Compiler", () => {
it( it(
"should pack all additional headers and footers", "should pack all additional headers and footers",
() => { async () => {
const file = new File({ const file = new File({
sections: [ sections: [
{ {
@ -92,12 +108,10 @@ describe("Compiler", () => {
], ],
}); });
const zipFile = compiler.compile(file); const zipFile = await compiler.compile(file);
const fileNames = Object.keys(zipFile.files).map((f) => zipFile.files[f].name); const fileNames = await unzip(zipFile);
expect(fileNames).is.an.instanceof(Array);
expect(fileNames).has.length(25);
expect(fileNames).has.length(21);
expect(fileNames).to.include("word/header1.xml"); expect(fileNames).to.include("word/header1.xml");
expect(fileNames).to.include("word/_rels/header1.xml.rels"); expect(fileNames).to.include("word/_rels/header1.xml.rels");
expect(fileNames).to.include("word/header2.xml"); expect(fileNames).to.include("word/header2.xml");

View File

@ -1,6 +1,5 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import * as chai from "chai"; import * as chai from "chai";
import * as sinon from "sinon";
import JSZip from "jszip"; import JSZip from "jszip";
import chaiAsPromised from "chai-as-promised"; import chaiAsPromised from "chai-as-promised";
@ -207,15 +206,14 @@ describe("from-docx", () => {
describe("patchDocument", () => { describe("patchDocument", () => {
describe("document.xml and [Content_Types].xml", () => { describe("document.xml and [Content_Types].xml", () => {
beforeEach(() => { beforeEach(() => {
sinon.stub(JSZip, "loadAsync").callsFake( vi.spyOn(JSZip, "loadAsync").mockReturnValue(
() => new Promise<JSZip>((resolve) => {
new Promise<JSZip>((resolve) => { const zip = new JSZip();
const zip = new JSZip();
zip.file("word/document.xml", MOCK_XML); zip.file("word/document.xml", MOCK_XML);
zip.file("[Content_Types].xml", `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>`); zip.file("[Content_Types].xml", `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>`);
resolve(zip); resolve(zip);
}), }),
); );
}); });
@ -292,16 +290,15 @@ describe("from-docx", () => {
describe("document.xml and [Content_Types].xml with relationships", () => { describe("document.xml and [Content_Types].xml with relationships", () => {
beforeEach(() => { beforeEach(() => {
sinon.stub(JSZip, "loadAsync").callsFake( vi.spyOn(JSZip, "loadAsync").mockReturnValue(
() => new Promise<JSZip>((resolve) => {
new Promise<JSZip>((resolve) => { const zip = new JSZip();
const zip = new JSZip();
zip.file("word/document.xml", MOCK_XML); zip.file("word/document.xml", MOCK_XML);
zip.file("word/_rels/document.xml.rels", `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>`); zip.file("word/_rels/document.xml.rels", `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>`);
zip.file("[Content_Types].xml", `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>`); zip.file("[Content_Types].xml", `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>`);
resolve(zip); resolve(zip);
}), }),
); );
}); });
@ -338,14 +335,13 @@ describe("from-docx", () => {
describe("document.xml", () => { describe("document.xml", () => {
beforeEach(() => { beforeEach(() => {
sinon.stub(JSZip, "loadAsync").callsFake( vi.spyOn(JSZip, "loadAsync").mockReturnValue(
() => new Promise<JSZip>((resolve) => {
new Promise<JSZip>((resolve) => { const zip = new JSZip();
const zip = new JSZip();
zip.file("word/document.xml", MOCK_XML); zip.file("word/document.xml", MOCK_XML);
resolve(zip); resolve(zip);
}), }),
); );
}); });
@ -374,16 +370,15 @@ describe("from-docx", () => {
describe("Images", () => { describe("Images", () => {
beforeEach(() => { beforeEach(() => {
sinon.stub(JSZip, "loadAsync").callsFake( vi.spyOn(JSZip, "loadAsync").mockReturnValue(
() => new Promise<JSZip>((resolve) => {
new Promise<JSZip>((resolve) => { const zip = new JSZip();
const zip = new JSZip();
zip.file("word/document.xml", MOCK_XML); zip.file("word/document.xml", MOCK_XML);
zip.file("word/document.bmp", ""); zip.file("word/document.bmp", "");
resolve(zip); resolve(zip);
}), }),
); );
}); });

View File

@ -33,4 +33,7 @@ export default defineConfig({
include: [/node_modules/], include: [/node_modules/],
}, },
}, },
test: {
environment: "jsdom",
},
}); });