2018-11-02 00:42:49 +00:00
|
|
|
// tslint:disable:object-literal-key-quotes
|
2018-11-01 02:22:32 +00:00
|
|
|
import { expect } from "chai";
|
2021-03-12 03:58:05 +00:00
|
|
|
import { SinonStub, stub } from "sinon";
|
2018-11-01 02:22:32 +00:00
|
|
|
|
2021-03-12 03:58:05 +00:00
|
|
|
import * as convenienceFunctions from "convenience-functions";
|
2018-11-01 02:22:32 +00:00
|
|
|
import { Formatter } from "export/formatter";
|
|
|
|
|
|
|
|
import { File } from "../file";
|
2019-06-23 22:36:01 +01:00
|
|
|
import { Paragraph } from "../paragraph";
|
2018-11-01 02:22:32 +00:00
|
|
|
import { Media } from "./media";
|
|
|
|
|
|
|
|
describe("Media", () => {
|
2021-03-12 03:58:05 +00:00
|
|
|
before(() => {
|
|
|
|
stub(convenienceFunctions, "uniqueId").callsFake(() => "test");
|
|
|
|
});
|
|
|
|
|
|
|
|
after(() => {
|
|
|
|
(convenienceFunctions.uniqueId as SinonStub).restore();
|
|
|
|
});
|
|
|
|
|
2018-11-01 02:22:32 +00:00
|
|
|
describe("#addImage", () => {
|
2018-11-02 00:42:49 +00:00
|
|
|
it("should add image", () => {
|
2018-11-01 02:22:32 +00:00
|
|
|
const file = new File();
|
|
|
|
const image = Media.addImage(file, "");
|
|
|
|
|
2019-06-23 22:36:01 +01:00
|
|
|
let tree = new Formatter().format(new Paragraph(image));
|
2018-11-01 02:22:32 +00:00
|
|
|
expect(tree["w:p"]).to.be.an.instanceof(Array);
|
|
|
|
|
2019-06-23 22:36:01 +01:00
|
|
|
tree = new Formatter().format(image);
|
2018-11-01 02:22:32 +00:00
|
|
|
expect(tree["w:r"]).to.be.an.instanceof(Array);
|
|
|
|
});
|
2018-11-02 00:42:49 +00:00
|
|
|
|
|
|
|
it("should ensure the correct relationship id is used when adding image", () => {
|
2018-12-24 16:50:53 +00:00
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
|
2018-11-02 00:42:49 +00:00
|
|
|
const file = new File();
|
|
|
|
const image1 = Media.addImage(file, "test");
|
2019-06-23 22:36:01 +01:00
|
|
|
const tree = new Formatter().format(new Paragraph(image1));
|
2019-04-09 05:27:18 -04:00
|
|
|
const inlineElements = tree["w:p"][0]["w:r"][0]["w:drawing"][0]["wp:inline"];
|
2018-11-02 00:42:49 +00:00
|
|
|
const graphicData = inlineElements.find((x) => x["a:graphic"]);
|
|
|
|
|
2019-04-09 05:27:18 -04:00
|
|
|
expect(graphicData["a:graphic"][1]["a:graphicData"][1]["pic:pic"][2]["pic:blipFill"][0]["a:blip"]).to.deep.equal({
|
2018-11-02 00:42:49 +00:00
|
|
|
_attr: {
|
2021-03-12 03:58:05 +00:00
|
|
|
"r:embed": `rId{test.png}`,
|
2018-11-02 00:42:49 +00:00
|
|
|
cstate: "none",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const image2 = Media.addImage(file, "test");
|
2019-06-23 22:36:01 +01:00
|
|
|
const tree2 = new Formatter().format(new Paragraph(image2));
|
2019-04-09 05:27:18 -04:00
|
|
|
const inlineElements2 = tree2["w:p"][0]["w:r"][0]["w:drawing"][0]["wp:inline"];
|
2018-11-02 00:42:49 +00:00
|
|
|
const graphicData2 = inlineElements2.find((x) => x["a:graphic"]);
|
|
|
|
|
2019-04-09 05:27:18 -04:00
|
|
|
expect(graphicData2["a:graphic"][1]["a:graphicData"][1]["pic:pic"][2]["pic:blipFill"][0]["a:blip"]).to.deep.equal({
|
2018-11-02 00:42:49 +00:00
|
|
|
_attr: {
|
2021-03-12 03:58:05 +00:00
|
|
|
"r:embed": `rId{test.png}`,
|
2018-11-02 00:42:49 +00:00
|
|
|
cstate: "none",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
2018-11-01 02:22:32 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("#addMedia", () => {
|
|
|
|
it("should add media", () => {
|
2018-12-24 16:50:53 +00:00
|
|
|
const image = new Media().addMedia("");
|
2018-11-01 02:22:32 +00:00
|
|
|
expect(image.fileName).to.equal("test.png");
|
|
|
|
expect(image.dimensions).to.deep.equal({
|
|
|
|
pixels: {
|
|
|
|
x: 100,
|
|
|
|
y: 100,
|
|
|
|
},
|
|
|
|
emus: {
|
|
|
|
x: 952500,
|
|
|
|
y: 952500,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return UInt8Array if atob is present", () => {
|
2021-03-13 22:43:21 +00:00
|
|
|
global.atob = () => "atob result";
|
2018-11-01 02:22:32 +00:00
|
|
|
|
2018-12-24 16:50:53 +00:00
|
|
|
const image = new Media().addMedia("");
|
2018-11-01 02:22:32 +00:00
|
|
|
expect(image.stream).to.be.an.instanceof(Uint8Array);
|
2021-03-13 22:43:21 +00:00
|
|
|
|
|
|
|
// tslint:disable-next-line: no-any
|
|
|
|
(global as any).atob = undefined;
|
2018-11-01 02:22:32 +00:00
|
|
|
});
|
2019-09-29 04:17:21 +01:00
|
|
|
|
|
|
|
it("should use data as is if its not a string", () => {
|
2021-03-13 22:43:21 +00:00
|
|
|
global.atob = () => "atob result";
|
2019-09-29 04:17:21 +01:00
|
|
|
|
2021-03-13 22:43:21 +00:00
|
|
|
const image = new Media().addMedia(Buffer.from(""));
|
2019-09-29 04:17:21 +01:00
|
|
|
expect(image.stream).to.be.an.instanceof(Uint8Array);
|
2021-03-13 22:43:21 +00:00
|
|
|
|
|
|
|
// tslint:disable-next-line: no-any
|
|
|
|
(global as any).atob = undefined;
|
2019-09-29 04:17:21 +01:00
|
|
|
});
|
2018-11-01 02:22:32 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("#getMedia", () => {
|
|
|
|
it("should get media", () => {
|
|
|
|
const media = new Media();
|
2018-12-24 16:50:53 +00:00
|
|
|
media.addMedia("");
|
2018-11-01 02:22:32 +00:00
|
|
|
|
|
|
|
const image = media.getMedia("test.png");
|
|
|
|
|
|
|
|
expect(image.fileName).to.equal("test.png");
|
|
|
|
expect(image.dimensions).to.deep.equal({
|
|
|
|
pixels: {
|
|
|
|
x: 100,
|
|
|
|
y: 100,
|
|
|
|
},
|
|
|
|
emus: {
|
|
|
|
x: 952500,
|
|
|
|
y: 952500,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Get media", () => {
|
|
|
|
const media = new Media();
|
|
|
|
|
|
|
|
expect(() => media.getMedia("test.png")).to.throw();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("#Array", () => {
|
|
|
|
it("Get images as array", () => {
|
|
|
|
const media = new Media();
|
2018-12-24 16:50:53 +00:00
|
|
|
media.addMedia("");
|
2018-11-01 02:22:32 +00:00
|
|
|
|
|
|
|
const array = media.Array;
|
|
|
|
expect(array).to.be.an.instanceof(Array);
|
|
|
|
expect(array.length).to.equal(1);
|
|
|
|
|
|
|
|
const image = array[0];
|
|
|
|
expect(image.fileName).to.equal("test.png");
|
|
|
|
expect(image.dimensions).to.deep.equal({
|
|
|
|
pixels: {
|
|
|
|
x: 100,
|
|
|
|
y: 100,
|
|
|
|
},
|
|
|
|
emus: {
|
|
|
|
x: 952500,
|
|
|
|
y: 952500,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|