2
0

export.test.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import * as utils from ".";
  2. import { diagramFactory } from "../excalidraw/tests/fixtures/diagramFixture";
  3. import { vi } from "vitest";
  4. import * as mockedSceneExportUtils from "../excalidraw/scene/export";
  5. import { MIME_TYPES } from "../excalidraw/constants";
  6. const exportToSvgSpy = vi.spyOn(mockedSceneExportUtils, "exportToSvg");
  7. describe("exportToCanvas", async () => {
  8. const EXPORT_PADDING = 10;
  9. it("with default arguments", async () => {
  10. const canvas = await utils.exportToCanvas({
  11. ...diagramFactory({ elementOverrides: { width: 100, height: 100 } }),
  12. });
  13. expect(canvas.width).toBe(100 + 2 * EXPORT_PADDING);
  14. expect(canvas.height).toBe(100 + 2 * EXPORT_PADDING);
  15. });
  16. it("when custom width and height", async () => {
  17. const canvas = await utils.exportToCanvas({
  18. ...diagramFactory({ elementOverrides: { width: 100, height: 100 } }),
  19. getDimensions: () => ({ width: 200, height: 200, scale: 1 }),
  20. });
  21. expect(canvas.width).toBe(200);
  22. expect(canvas.height).toBe(200);
  23. });
  24. });
  25. describe("exportToBlob", async () => {
  26. describe("mime type", () => {
  27. // afterEach(vi.restoreAllMocks);
  28. it("should change image/jpg to image/jpeg", async () => {
  29. const blob = await utils.exportToBlob({
  30. ...diagramFactory(),
  31. getDimensions: (width, height) => ({ width, height, scale: 1 }),
  32. // testing typo in MIME type (jpg → jpeg)
  33. mimeType: "image/jpg",
  34. appState: {
  35. exportBackground: true,
  36. },
  37. });
  38. expect(blob?.type).toBe(MIME_TYPES.jpg);
  39. });
  40. it("should default to image/png", async () => {
  41. const blob = await utils.exportToBlob({
  42. ...diagramFactory(),
  43. });
  44. expect(blob?.type).toBe(MIME_TYPES.png);
  45. });
  46. it("should warn when using quality with image/png", async () => {
  47. const consoleSpy = vi
  48. .spyOn(console, "warn")
  49. .mockImplementationOnce(() => void 0);
  50. await utils.exportToBlob({
  51. ...diagramFactory(),
  52. mimeType: MIME_TYPES.png,
  53. quality: 1,
  54. });
  55. expect(consoleSpy).toHaveBeenCalledWith(
  56. `"quality" will be ignored for "${MIME_TYPES.png}" mimeType`,
  57. );
  58. });
  59. });
  60. });
  61. describe("exportToSvg", () => {
  62. const passedElements = () => exportToSvgSpy.mock.calls[0][0];
  63. const passedOptions = () => exportToSvgSpy.mock.calls[0][1];
  64. afterEach(() => {
  65. vi.clearAllMocks();
  66. });
  67. it("with default arguments", async () => {
  68. await utils.exportToSvg({
  69. ...diagramFactory({
  70. overrides: { appState: void 0 },
  71. }),
  72. });
  73. const passedOptionsWhenDefault = {
  74. ...passedOptions(),
  75. // To avoid varying snapshots
  76. name: "name",
  77. };
  78. expect(passedElements().length).toBe(3);
  79. expect(passedOptionsWhenDefault).toMatchSnapshot();
  80. });
  81. // FIXME the utils.exportToSvg no longer filters out deleted elements.
  82. // It's already supposed to be passed non-deleted elements by we're not
  83. // type-checking for it correctly.
  84. it.skip("with deleted elements", async () => {
  85. await utils.exportToSvg({
  86. ...diagramFactory({
  87. overrides: { appState: void 0 },
  88. elementOverrides: { isDeleted: true },
  89. }),
  90. });
  91. expect(passedElements().length).toBe(0);
  92. });
  93. it("with exportPadding", async () => {
  94. await utils.exportToSvg({
  95. ...diagramFactory({ overrides: { appState: { name: "diagram name" } } }),
  96. exportPadding: 0,
  97. });
  98. expect(passedElements().length).toBe(3);
  99. expect(passedOptions()).toEqual(
  100. expect.objectContaining({ exportPadding: 0 }),
  101. );
  102. });
  103. it("with exportEmbedScene", async () => {
  104. await utils.exportToSvg({
  105. ...diagramFactory({
  106. overrides: {
  107. appState: { name: "diagram name", exportEmbedScene: true },
  108. },
  109. }),
  110. });
  111. expect(passedElements().length).toBe(3);
  112. expect(passedOptions().exportEmbedScene).toBe(true);
  113. });
  114. });