2023-12-13 21:51:27 +05:30
|
|
|
import * as utils from ".";
|
2023-12-12 11:32:51 +05:30
|
|
|
import { diagramFactory } from "../excalidraw/tests/fixtures/diagramFixture";
|
2023-07-27 23:50:11 +05:30
|
|
|
import { vi } from "vitest";
|
2023-12-12 11:32:51 +05:30
|
|
|
import * as mockedSceneExportUtils from "../excalidraw/scene/export";
|
2023-07-27 23:50:11 +05:30
|
|
|
|
2023-12-12 11:32:51 +05:30
|
|
|
import { MIME_TYPES } from "../excalidraw/constants";
|
2021-03-31 14:28:25 +02:00
|
|
|
|
2023-07-27 23:50:11 +05:30
|
|
|
const exportToSvgSpy = vi.spyOn(mockedSceneExportUtils, "exportToSvg");
|
2021-03-31 14:28:25 +02:00
|
|
|
|
2023-07-27 23:50:11 +05:30
|
|
|
describe("exportToCanvas", async () => {
|
2021-03-31 14:28:25 +02:00
|
|
|
const EXPORT_PADDING = 10;
|
|
|
|
|
2021-10-21 22:05:48 +02:00
|
|
|
it("with default arguments", async () => {
|
|
|
|
const canvas = await utils.exportToCanvas({
|
2021-03-31 14:28:25 +02:00
|
|
|
...diagramFactory({ elementOverrides: { width: 100, height: 100 } }),
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(canvas.width).toBe(100 + 2 * EXPORT_PADDING);
|
|
|
|
expect(canvas.height).toBe(100 + 2 * EXPORT_PADDING);
|
|
|
|
});
|
|
|
|
|
2021-10-21 22:05:48 +02:00
|
|
|
it("when custom width and height", async () => {
|
|
|
|
const canvas = await utils.exportToCanvas({
|
2021-03-31 14:28:25 +02:00
|
|
|
...diagramFactory({ elementOverrides: { width: 100, height: 100 } }),
|
|
|
|
getDimensions: () => ({ width: 200, height: 200, scale: 1 }),
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(canvas.width).toBe(200);
|
|
|
|
expect(canvas.height).toBe(200);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-07-27 23:50:11 +05:30
|
|
|
describe("exportToBlob", async () => {
|
2021-03-31 14:28:25 +02:00
|
|
|
describe("mime type", () => {
|
2023-07-27 23:50:11 +05:30
|
|
|
// afterEach(vi.restoreAllMocks);
|
2021-03-31 14:28:25 +02:00
|
|
|
it("should change image/jpg to image/jpeg", async () => {
|
|
|
|
const blob = await utils.exportToBlob({
|
|
|
|
...diagramFactory(),
|
|
|
|
getDimensions: (width, height) => ({ width, height, scale: 1 }),
|
2021-10-21 22:05:48 +02:00
|
|
|
// testing typo in MIME type (jpg → jpeg)
|
2021-03-31 14:28:25 +02:00
|
|
|
mimeType: "image/jpg",
|
2021-11-30 22:08:55 +01:00
|
|
|
appState: {
|
|
|
|
exportBackground: true,
|
|
|
|
},
|
2021-03-31 14:28:25 +02:00
|
|
|
});
|
2021-10-21 22:05:48 +02:00
|
|
|
expect(blob?.type).toBe(MIME_TYPES.jpg);
|
2021-03-31 14:28:25 +02:00
|
|
|
});
|
|
|
|
it("should default to image/png", async () => {
|
|
|
|
const blob = await utils.exportToBlob({
|
|
|
|
...diagramFactory(),
|
|
|
|
});
|
2021-10-21 22:05:48 +02:00
|
|
|
expect(blob?.type).toBe(MIME_TYPES.png);
|
2021-03-31 14:28:25 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should warn when using quality with image/png", async () => {
|
2023-07-27 23:50:11 +05:30
|
|
|
const consoleSpy = vi
|
2021-03-31 14:28:25 +02:00
|
|
|
.spyOn(console, "warn")
|
|
|
|
.mockImplementationOnce(() => void 0);
|
|
|
|
await utils.exportToBlob({
|
|
|
|
...diagramFactory(),
|
2021-10-21 22:05:48 +02:00
|
|
|
mimeType: MIME_TYPES.png,
|
2021-03-31 14:28:25 +02:00
|
|
|
quality: 1,
|
|
|
|
});
|
|
|
|
expect(consoleSpy).toHaveBeenCalledWith(
|
2021-10-21 22:05:48 +02:00
|
|
|
`"quality" will be ignored for "${MIME_TYPES.png}" mimeType`,
|
2021-03-31 14:28:25 +02:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("exportToSvg", () => {
|
2023-07-27 23:50:11 +05:30
|
|
|
const passedElements = () => exportToSvgSpy.mock.calls[0][0];
|
|
|
|
const passedOptions = () => exportToSvgSpy.mock.calls[0][1];
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
vi.clearAllMocks();
|
|
|
|
});
|
2021-03-31 14:28:25 +02:00
|
|
|
|
2021-07-03 02:07:01 +05:30
|
|
|
it("with default arguments", async () => {
|
|
|
|
await utils.exportToSvg({
|
2021-03-31 14:28:25 +02:00
|
|
|
...diagramFactory({
|
|
|
|
overrides: { appState: void 0 },
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
|
|
|
const passedOptionsWhenDefault = {
|
|
|
|
...passedOptions(),
|
|
|
|
// To avoid varying snapshots
|
|
|
|
name: "name",
|
|
|
|
};
|
|
|
|
expect(passedElements().length).toBe(3);
|
|
|
|
expect(passedOptionsWhenDefault).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
2023-11-09 17:00:21 +01:00
|
|
|
// FIXME the utils.exportToSvg no longer filters out deleted elements.
|
|
|
|
// It's already supposed to be passed non-deleted elements by we're not
|
|
|
|
// type-checking for it correctly.
|
|
|
|
it.skip("with deleted elements", async () => {
|
2021-07-03 02:07:01 +05:30
|
|
|
await utils.exportToSvg({
|
2021-03-31 14:28:25 +02:00
|
|
|
...diagramFactory({
|
|
|
|
overrides: { appState: void 0 },
|
|
|
|
elementOverrides: { isDeleted: true },
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(passedElements().length).toBe(0);
|
|
|
|
});
|
|
|
|
|
2021-07-03 02:07:01 +05:30
|
|
|
it("with exportPadding", async () => {
|
|
|
|
await utils.exportToSvg({
|
2021-03-31 14:28:25 +02:00
|
|
|
...diagramFactory({ overrides: { appState: { name: "diagram name" } } }),
|
|
|
|
exportPadding: 0,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(passedElements().length).toBe(3);
|
|
|
|
expect(passedOptions()).toEqual(
|
2021-07-03 02:07:01 +05:30
|
|
|
expect.objectContaining({ exportPadding: 0 }),
|
2021-03-31 14:28:25 +02:00
|
|
|
);
|
|
|
|
});
|
2021-07-03 02:07:01 +05:30
|
|
|
|
|
|
|
it("with exportEmbedScene", async () => {
|
|
|
|
await utils.exportToSvg({
|
|
|
|
...diagramFactory({
|
|
|
|
overrides: {
|
|
|
|
appState: { name: "diagram name", exportEmbedScene: true },
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(passedElements().length).toBe(3);
|
|
|
|
expect(passedOptions().exportEmbedScene).toBe(true);
|
|
|
|
});
|
2021-03-31 14:28:25 +02:00
|
|
|
});
|