export.test.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. it("should change image/jpg to image/jpeg", async () => {
  28. const blob = await utils.exportToBlob({
  29. ...diagramFactory(),
  30. getDimensions: (width, height) => ({ width, height, scale: 1 }),
  31. // testing typo in MIME type (jpg → jpeg)
  32. mimeType: "image/jpg",
  33. appState: {
  34. exportBackground: true,
  35. },
  36. });
  37. expect(blob?.type).toBe(MIME_TYPES.jpg);
  38. });
  39. it("should default to image/png", async () => {
  40. const blob = await utils.exportToBlob({
  41. ...diagramFactory(),
  42. });
  43. expect(blob?.type).toBe(MIME_TYPES.png);
  44. });
  45. it("should warn when using quality with image/png", async () => {
  46. const consoleSpy = vi
  47. .spyOn(console, "warn")
  48. .mockImplementationOnce(() => void 0);
  49. await utils.exportToBlob({
  50. ...diagramFactory(),
  51. mimeType: MIME_TYPES.png,
  52. quality: 1,
  53. });
  54. expect(consoleSpy).toHaveBeenCalledWith(
  55. `"quality" will be ignored for "${MIME_TYPES.png}" mimeType`,
  56. );
  57. });
  58. });
  59. });
  60. describe("exportToSvg", () => {
  61. const passedElements = () => exportToSvgSpy.mock.calls[0][0];
  62. const passedOptions = () => exportToSvgSpy.mock.calls[0][1];
  63. afterEach(() => {
  64. vi.clearAllMocks();
  65. });
  66. it("with default arguments", async () => {
  67. await utils.exportToSvg({
  68. ...diagramFactory({
  69. overrides: { appState: void 0 },
  70. }),
  71. });
  72. const passedOptionsWhenDefault = {
  73. ...passedOptions(),
  74. // To avoid varying snapshots
  75. name: "name",
  76. };
  77. expect(passedElements().length).toBe(3);
  78. expect(passedOptionsWhenDefault).toMatchSnapshot();
  79. });
  80. // FIXME the utils.exportToSvg no longer filters out deleted elements.
  81. // It's already supposed to be passed non-deleted elements by we're not
  82. // type-checking for it correctly.
  83. it.skip("with deleted elements", async () => {
  84. await utils.exportToSvg({
  85. ...diagramFactory({
  86. overrides: { appState: void 0 },
  87. elementOverrides: { isDeleted: true },
  88. }),
  89. });
  90. expect(passedElements().length).toBe(0);
  91. });
  92. it("with exportPadding", async () => {
  93. await utils.exportToSvg({
  94. ...diagramFactory({ overrides: { appState: { name: "diagram name" } } }),
  95. exportPadding: 0,
  96. });
  97. expect(passedElements().length).toBe(3);
  98. expect(passedOptions()).toEqual(
  99. expect.objectContaining({ exportPadding: 0 }),
  100. );
  101. });
  102. it("with exportEmbedScene", async () => {
  103. await utils.exportToSvg({
  104. ...diagramFactory({
  105. overrides: {
  106. appState: { name: "diagram name", exportEmbedScene: true },
  107. },
  108. }),
  109. });
  110. expect(passedElements().length).toBe(3);
  111. expect(passedOptions().exportEmbedScene).toBe(true);
  112. });
  113. });