collab.test.tsx 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import React from "react";
  2. import { render, updateSceneData, waitFor } from "./test-utils";
  3. import ExcalidrawApp from "../excalidraw-app";
  4. import { API } from "./helpers/api";
  5. import { createUndoAction } from "../actions/actionHistory";
  6. const { h } = window;
  7. Object.defineProperty(window, "crypto", {
  8. value: {
  9. getRandomValues: (arr: number[]) =>
  10. arr.forEach((v, i) => (arr[i] = Math.floor(Math.random() * 256))),
  11. subtle: {
  12. generateKey: () => {},
  13. exportKey: () => ({ k: "sTdLvMC_M3V8_vGa3UVRDg" }),
  14. },
  15. },
  16. });
  17. jest.mock("../excalidraw-app/data/firebase.ts", () => {
  18. const loadFromFirebase = async () => null;
  19. const saveToFirebase = () => {};
  20. const isSavedToFirebase = () => true;
  21. return {
  22. loadFromFirebase,
  23. saveToFirebase,
  24. isSavedToFirebase,
  25. };
  26. });
  27. jest.mock("socket.io-client", () => {
  28. return () => {
  29. return {
  30. close: () => {},
  31. on: () => {},
  32. off: () => {},
  33. emit: () => {},
  34. };
  35. };
  36. });
  37. describe("collaboration", () => {
  38. it("creating room should reset deleted elements", async () => {
  39. await render(<ExcalidrawApp />);
  40. // To update the scene with deleted elements before starting collab
  41. updateSceneData({
  42. elements: [
  43. API.createElement({ type: "rectangle", id: "A" }),
  44. API.createElement({
  45. type: "rectangle",
  46. id: "B",
  47. isDeleted: true,
  48. }),
  49. ],
  50. });
  51. await waitFor(() => {
  52. expect(h.elements).toEqual([
  53. expect.objectContaining({ id: "A" }),
  54. expect.objectContaining({ id: "B", isDeleted: true }),
  55. ]);
  56. expect(API.getStateHistory().length).toBe(1);
  57. });
  58. window.collab.openPortal();
  59. await waitFor(() => {
  60. expect(h.elements).toEqual([expect.objectContaining({ id: "A" })]);
  61. expect(API.getStateHistory().length).toBe(1);
  62. });
  63. const undoAction = createUndoAction(h.history);
  64. // noop
  65. h.app.actionManager.executeAction(undoAction);
  66. await waitFor(() => {
  67. expect(h.elements).toEqual([expect.objectContaining({ id: "A" })]);
  68. expect(API.getStateHistory().length).toBe(1);
  69. });
  70. });
  71. });