2023-07-27 23:50:11 +05:30
|
|
|
import { vi } from "vitest";
|
2023-09-21 09:28:48 +05:30
|
|
|
import { render, updateSceneData, waitFor } from "../../src/tests/test-utils";
|
|
|
|
import ExcalidrawApp from "../../excalidraw-app";
|
|
|
|
import { API } from "../../src/tests/helpers/api";
|
|
|
|
import { createUndoAction } from "../../src/actions/actionHistory";
|
2020-10-26 15:53:55 +01:00
|
|
|
const { h } = window;
|
|
|
|
|
|
|
|
Object.defineProperty(window, "crypto", {
|
|
|
|
value: {
|
|
|
|
getRandomValues: (arr: number[]) =>
|
|
|
|
arr.forEach((v, i) => (arr[i] = Math.floor(Math.random() * 256))),
|
|
|
|
subtle: {
|
|
|
|
generateKey: () => {},
|
|
|
|
exportKey: () => ({ k: "sTdLvMC_M3V8_vGa3UVRDg" }),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-09-21 09:28:48 +05:30
|
|
|
vi.mock("../../excalidraw-app/data/index.ts", async (importActual) => {
|
2023-07-27 23:50:11 +05:30
|
|
|
const module = (await importActual()) as any;
|
|
|
|
return {
|
|
|
|
__esmodule: true,
|
|
|
|
...module,
|
|
|
|
getCollabServer: vi.fn(() => ({
|
|
|
|
url: /* doesn't really matter */ "http://localhost:3002",
|
|
|
|
})),
|
|
|
|
};
|
|
|
|
});
|
2022-03-06 22:43:02 +01:00
|
|
|
|
2023-09-21 09:28:48 +05:30
|
|
|
vi.mock("../../excalidraw-app/data/firebase.ts", () => {
|
2020-10-26 15:53:55 +01:00
|
|
|
const loadFromFirebase = async () => null;
|
|
|
|
const saveToFirebase = () => {};
|
|
|
|
const isSavedToFirebase = () => true;
|
2021-10-21 22:05:48 +02:00
|
|
|
const loadFilesFromFirebase = async () => ({
|
|
|
|
loadedFiles: [],
|
|
|
|
erroredFiles: [],
|
|
|
|
});
|
|
|
|
const saveFilesToFirebase = async () => ({
|
|
|
|
savedFiles: new Map(),
|
|
|
|
erroredFiles: new Map(),
|
|
|
|
});
|
2020-10-26 15:53:55 +01:00
|
|
|
|
|
|
|
return {
|
|
|
|
loadFromFirebase,
|
|
|
|
saveToFirebase,
|
|
|
|
isSavedToFirebase,
|
2021-10-21 22:05:48 +02:00
|
|
|
loadFilesFromFirebase,
|
|
|
|
saveFilesToFirebase,
|
2020-10-26 15:53:55 +01:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2023-07-27 23:50:11 +05:30
|
|
|
vi.mock("socket.io-client", () => {
|
|
|
|
return {
|
|
|
|
default: () => {
|
|
|
|
return {
|
|
|
|
close: () => {},
|
|
|
|
on: () => {},
|
|
|
|
once: () => {},
|
|
|
|
off: () => {},
|
|
|
|
emit: () => {},
|
|
|
|
};
|
|
|
|
},
|
2020-10-30 21:01:41 +01:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-10-26 15:53:55 +01:00
|
|
|
describe("collaboration", () => {
|
|
|
|
it("creating room should reset deleted elements", async () => {
|
2020-12-05 20:00:53 +05:30
|
|
|
await render(<ExcalidrawApp />);
|
|
|
|
// To update the scene with deleted elements before starting collab
|
|
|
|
updateSceneData({
|
|
|
|
elements: [
|
|
|
|
API.createElement({ type: "rectangle", id: "A" }),
|
|
|
|
API.createElement({
|
|
|
|
type: "rectangle",
|
|
|
|
id: "B",
|
|
|
|
isDeleted: true,
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
2020-10-26 15:53:55 +01:00
|
|
|
await waitFor(() => {
|
|
|
|
expect(h.elements).toEqual([
|
|
|
|
expect.objectContaining({ id: "A" }),
|
|
|
|
expect.objectContaining({ id: "B", isDeleted: true }),
|
|
|
|
]);
|
|
|
|
expect(API.getStateHistory().length).toBe(1);
|
|
|
|
});
|
2022-07-05 16:03:40 +02:00
|
|
|
window.collab.startCollaboration(null);
|
2020-10-26 15:53:55 +01:00
|
|
|
await waitFor(() => {
|
|
|
|
expect(h.elements).toEqual([expect.objectContaining({ id: "A" })]);
|
|
|
|
expect(API.getStateHistory().length).toBe(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
const undoAction = createUndoAction(h.history);
|
|
|
|
// noop
|
|
|
|
h.app.actionManager.executeAction(undoAction);
|
|
|
|
await waitFor(() => {
|
|
|
|
expect(h.elements).toEqual([expect.objectContaining({ id: "A" })]);
|
|
|
|
expect(API.getStateHistory().length).toBe(1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|