utils.unmocked.test.ts 2.2 KB

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