appState.test.tsx 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { queryByTestId, render, waitFor } from "./test-utils";
  2. import ExcalidrawApp from "../excalidraw-app";
  3. import { API } from "./helpers/api";
  4. import { getDefaultAppState } from "../appState";
  5. import { EXPORT_DATA_TYPES, MIME_TYPES } from "../constants";
  6. import { Pointer, UI } from "./helpers/ui";
  7. import { ExcalidrawTextElement } from "../element/types";
  8. const { h } = window;
  9. describe("appState", () => {
  10. it("drag&drop file doesn't reset non-persisted appState", async () => {
  11. const defaultAppState = getDefaultAppState();
  12. const exportBackground = !defaultAppState.exportBackground;
  13. await render(<ExcalidrawApp />, {
  14. localStorageData: {
  15. appState: {
  16. exportBackground,
  17. viewBackgroundColor: "#F00",
  18. },
  19. },
  20. });
  21. await waitFor(() => {
  22. expect(h.state.exportBackground).toBe(exportBackground);
  23. expect(h.state.viewBackgroundColor).toBe("#F00");
  24. });
  25. API.drop(
  26. new Blob(
  27. [
  28. JSON.stringify({
  29. type: EXPORT_DATA_TYPES.excalidraw,
  30. appState: {
  31. viewBackgroundColor: "#000",
  32. },
  33. elements: [API.createElement({ type: "rectangle", id: "A" })],
  34. }),
  35. ],
  36. { type: MIME_TYPES.json },
  37. ),
  38. );
  39. await waitFor(() => {
  40. expect(h.elements).toEqual([expect.objectContaining({ id: "A" })]);
  41. // non-imported prop → retain
  42. expect(h.state.exportBackground).toBe(exportBackground);
  43. // imported prop → overwrite
  44. expect(h.state.viewBackgroundColor).toBe("#000");
  45. });
  46. });
  47. it("changing fontSize with text tool selected (no element created yet)", async () => {
  48. const { container } = await render(<ExcalidrawApp />, {
  49. localStorageData: {
  50. appState: {
  51. currentItemFontSize: 30,
  52. },
  53. },
  54. });
  55. UI.clickTool("text");
  56. expect(h.state.currentItemFontSize).toBe(30);
  57. queryByTestId(container, "fontSize-small")!.click();
  58. expect(h.state.currentItemFontSize).toBe(16);
  59. const mouse = new Pointer("mouse");
  60. mouse.clickAt(100, 100);
  61. expect((h.elements[0] as ExcalidrawTextElement).fontSize).toBe(16);
  62. });
  63. });