utils.unmocked.test.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. data: {
  16. elements: sourceElements,
  17. appState: {
  18. viewBackgroundColor: "#ffffff",
  19. gridModeEnabled: false,
  20. exportEmbedScene: true,
  21. },
  22. files: null,
  23. },
  24. });
  25. const svg = svgNode.outerHTML;
  26. const parsedString = decodeSvgMetadata({ svg });
  27. const importedData: ImportedDataState = JSON.parse(parsedString);
  28. expect(sourceElements.map((x) => x.id)).toEqual(
  29. importedData.elements?.map((el) => el.id),
  30. );
  31. });
  32. });
  33. // skipped because we can't test png encoding right now
  34. // (canvas.toBlob not supported in jsdom)
  35. describe.skip("exportToBlob", () => {
  36. it("embedding scene data shouldn't modify them", async () => {
  37. const rectangle = API.createElement({ type: "rectangle" });
  38. const ellipse = API.createElement({ type: "ellipse" });
  39. const sourceElements = [rectangle, ellipse];
  40. const blob = await utils.exportToBlob({
  41. data: {
  42. elements: sourceElements,
  43. appState: {
  44. viewBackgroundColor: "#ffffff",
  45. gridModeEnabled: false,
  46. exportEmbedScene: true,
  47. },
  48. files: null,
  49. },
  50. config: {
  51. mimeType: "image/png",
  52. },
  53. });
  54. const parsedString = await decodePngMetadata(blob);
  55. const importedData: ImportedDataState = JSON.parse(parsedString);
  56. expect(sourceElements.map((x) => x.id)).toEqual(
  57. importedData.elements?.map((el) => el.id),
  58. );
  59. });
  60. });
  61. });