2020-10-15 21:31:21 +02:00
|
|
|
import { render, waitFor } from "./test-utils";
|
2020-12-05 20:00:53 +05:30
|
|
|
import ExcalidrawApp from "../excalidraw-app";
|
2020-10-15 21:31:21 +02:00
|
|
|
import { API } from "./helpers/api";
|
|
|
|
import {
|
|
|
|
encodePngMetadata,
|
|
|
|
encodeSvgMetadata,
|
|
|
|
decodeSvgMetadata,
|
|
|
|
} from "../data/image";
|
|
|
|
import { serializeAsJSON } from "../data/json";
|
|
|
|
|
|
|
|
const { h } = window;
|
|
|
|
|
|
|
|
const testElements = [
|
|
|
|
{
|
|
|
|
...API.createElement({
|
|
|
|
type: "text",
|
|
|
|
id: "A",
|
|
|
|
text: "😀",
|
|
|
|
}),
|
|
|
|
// can't get jsdom text measurement to work so this is a temp hack
|
2020-11-05 19:06:18 +02:00
|
|
|
// to ensure the element isn't stripped as invisible
|
2020-10-15 21:31:21 +02:00
|
|
|
width: 16,
|
|
|
|
height: 16,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
// tiny polyfill for TextDecoder.decode on which we depend
|
|
|
|
Object.defineProperty(window, "TextDecoder", {
|
|
|
|
value: class TextDecoder {
|
|
|
|
decode(ab: ArrayBuffer) {
|
|
|
|
return new Uint8Array(ab).reduce(
|
|
|
|
(acc, c) => acc + String.fromCharCode(c),
|
|
|
|
"",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-10-30 21:01:41 +01:00
|
|
|
describe("export", () => {
|
2020-12-05 20:00:53 +05:30
|
|
|
beforeEach(async () => {
|
|
|
|
await render(<ExcalidrawApp />);
|
2020-10-15 21:31:21 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("export embedded png and reimport", async () => {
|
2020-10-30 21:01:41 +01:00
|
|
|
const pngBlob = await API.loadFile("./fixtures/smiley.png");
|
2020-10-15 21:31:21 +02:00
|
|
|
const pngBlobEmbedded = await encodePngMetadata({
|
|
|
|
blob: pngBlob,
|
|
|
|
metadata: serializeAsJSON(testElements, h.state),
|
|
|
|
});
|
2020-10-30 21:01:41 +01:00
|
|
|
API.drop(pngBlobEmbedded);
|
2020-10-15 21:31:21 +02:00
|
|
|
|
|
|
|
await waitFor(() => {
|
|
|
|
expect(h.elements).toEqual([
|
|
|
|
expect.objectContaining({ type: "text", text: "😀" }),
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("test encoding/decoding scene for SVG export", async () => {
|
|
|
|
const encoded = await encodeSvgMetadata({
|
|
|
|
text: serializeAsJSON(testElements, h.state),
|
|
|
|
});
|
|
|
|
const decoded = JSON.parse(await decodeSvgMetadata({ svg: encoded }));
|
|
|
|
expect(decoded.elements).toEqual([
|
|
|
|
expect.objectContaining({ type: "text", text: "😀" }),
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("import embedded png (legacy v1)", async () => {
|
2020-10-30 21:01:41 +01:00
|
|
|
API.drop(await API.loadFile("./fixtures/test_embedded_v1.png"));
|
2020-10-15 21:31:21 +02:00
|
|
|
await waitFor(() => {
|
|
|
|
expect(h.elements).toEqual([
|
|
|
|
expect.objectContaining({ type: "text", text: "test" }),
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("import embedded png (v2)", async () => {
|
2020-10-30 21:01:41 +01:00
|
|
|
API.drop(await API.loadFile("./fixtures/smiley_embedded_v2.png"));
|
2020-10-15 21:31:21 +02:00
|
|
|
await waitFor(() => {
|
|
|
|
expect(h.elements).toEqual([
|
|
|
|
expect.objectContaining({ type: "text", text: "😀" }),
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("import embedded svg (legacy v1)", async () => {
|
2020-10-30 21:01:41 +01:00
|
|
|
API.drop(await API.loadFile("./fixtures/test_embedded_v1.svg"));
|
2020-10-15 21:31:21 +02:00
|
|
|
await waitFor(() => {
|
|
|
|
expect(h.elements).toEqual([
|
|
|
|
expect.objectContaining({ type: "text", text: "test" }),
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("import embedded svg (v2)", async () => {
|
2020-10-30 21:01:41 +01:00
|
|
|
API.drop(await API.loadFile("./fixtures/smiley_embedded_v2.svg"));
|
2020-10-15 21:31:21 +02:00
|
|
|
await waitFor(() => {
|
|
|
|
expect(h.elements).toEqual([
|
|
|
|
expect.objectContaining({ type: "text", text: "😀" }),
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|