utils.test.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import * as utils from "../../packages/utils";
  2. import { diagramFactory } from "../fixtures/diagramFixture";
  3. import { vi } from "vitest";
  4. import * as mockedSceneExportUtils from "../../scene/export";
  5. import { MIME_TYPES } from "../../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. it("with deleted elements", async () => {
  82. await utils.exportToSvg({
  83. ...diagramFactory({
  84. overrides: { appState: void 0 },
  85. elementOverrides: { isDeleted: true },
  86. }),
  87. });
  88. expect(passedElements().length).toBe(0);
  89. });
  90. it("with exportPadding", async () => {
  91. await utils.exportToSvg({
  92. ...diagramFactory({ overrides: { appState: { name: "diagram name" } } }),
  93. exportPadding: 0,
  94. });
  95. expect(passedElements().length).toBe(3);
  96. expect(passedOptions()).toEqual(
  97. expect.objectContaining({ exportPadding: 0 }),
  98. );
  99. });
  100. it("with exportEmbedScene", async () => {
  101. await utils.exportToSvg({
  102. ...diagramFactory({
  103. overrides: {
  104. appState: { name: "diagram name", exportEmbedScene: true },
  105. },
  106. }),
  107. });
  108. expect(passedElements().length).toBe(3);
  109. expect(passedOptions().exportEmbedScene).toBe(true);
  110. });
  111. });