utils.unmocked.test.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { decodePngMetadata } from "@excalidraw/excalidraw/data/image";
  2. import { decodeSvgBase64Payload } from "@excalidraw/excalidraw/scene/export";
  3. import { API } from "@excalidraw/excalidraw/tests/helpers/api";
  4. import type { ImportedDataState } from "@excalidraw/excalidraw/data/types";
  5. import * as utils from "../src";
  6. // NOTE this test file is using the actual API, unmocked. Hence splitting it
  7. // from the other test file, because I couldn't figure out how to test
  8. // mocked and unmocked API in the same file.
  9. describe("embedding scene data", () => {
  10. describe("exportToSvg", () => {
  11. it("embedding scene data shouldn't modify them", async () => {
  12. const rectangle = API.createElement({ type: "rectangle" });
  13. const ellipse = API.createElement({ type: "ellipse" });
  14. const sourceElements = [rectangle, ellipse];
  15. const svgNode = await utils.exportToSvg({
  16. elements: sourceElements,
  17. appState: {
  18. viewBackgroundColor: "#ffffff",
  19. gridModeEnabled: false,
  20. exportEmbedScene: true,
  21. },
  22. files: null,
  23. });
  24. const svg = svgNode.outerHTML;
  25. const parsedString = decodeSvgBase64Payload({ svg });
  26. const importedData: ImportedDataState = JSON.parse(parsedString);
  27. expect(sourceElements.map((x) => x.id)).toEqual(
  28. importedData.elements?.map((el) => el.id),
  29. );
  30. });
  31. });
  32. // skipped because we can't test png encoding right now
  33. // (canvas.toBlob not supported in jsdom)
  34. describe.skip("exportToBlob", () => {
  35. it("embedding scene data shouldn't modify them", async () => {
  36. const rectangle = API.createElement({ type: "rectangle" });
  37. const ellipse = API.createElement({ type: "ellipse" });
  38. const sourceElements = [rectangle, ellipse];
  39. const blob = await utils.exportToBlob({
  40. mimeType: "image/png",
  41. elements: sourceElements,
  42. appState: {
  43. viewBackgroundColor: "#ffffff",
  44. gridModeEnabled: false,
  45. exportEmbedScene: true,
  46. },
  47. files: null,
  48. });
  49. const parsedString = await decodePngMetadata(blob);
  50. const importedData: ImportedDataState = JSON.parse(parsedString);
  51. expect(sourceElements.map((x) => x.id)).toEqual(
  52. importedData.elements?.map((el) => el.id),
  53. );
  54. });
  55. });
  56. });