2020-09-09 21:08:06 +02:00
|
|
|
import React from "react";
|
2020-10-13 13:46:52 +02:00
|
|
|
import { render } from "./test-utils";
|
2020-12-05 20:00:53 +05:30
|
|
|
import ExcalidrawApp from "../excalidraw-app";
|
2020-09-09 21:08:06 +02:00
|
|
|
import { UI } from "./helpers/ui";
|
|
|
|
import { API } from "./helpers/api";
|
|
|
|
import { getDefaultAppState } from "../appState";
|
2020-10-13 13:46:52 +02:00
|
|
|
import { waitFor } from "@testing-library/react";
|
2020-09-09 21:08:06 +02:00
|
|
|
import { createUndoAction, createRedoAction } from "../actions/actionHistory";
|
2021-03-20 20:20:47 +01:00
|
|
|
import { EXPORT_DATA_TYPES } from "../constants";
|
2020-09-09 21:08:06 +02:00
|
|
|
|
|
|
|
const { h } = window;
|
|
|
|
|
|
|
|
describe("history", () => {
|
|
|
|
it("initializing scene should end up with single history entry", async () => {
|
2020-12-05 20:00:53 +05:30
|
|
|
await render(<ExcalidrawApp />, {
|
|
|
|
localStorageData: {
|
|
|
|
elements: [API.createElement({ type: "rectangle", id: "A" })],
|
|
|
|
appState: {
|
|
|
|
zenModeEnabled: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2020-09-09 21:08:06 +02:00
|
|
|
|
|
|
|
await waitFor(() => expect(h.state.zenModeEnabled).toBe(true));
|
|
|
|
await waitFor(() =>
|
|
|
|
expect(h.elements).toEqual([expect.objectContaining({ id: "A" })]),
|
|
|
|
);
|
|
|
|
const undoAction = createUndoAction(h.history);
|
|
|
|
const redoAction = createRedoAction(h.history);
|
|
|
|
h.app.actionManager.executeAction(undoAction);
|
|
|
|
expect(h.elements).toEqual([
|
|
|
|
expect.objectContaining({ id: "A", isDeleted: false }),
|
|
|
|
]);
|
|
|
|
const rectangle = UI.createElement("rectangle");
|
|
|
|
expect(h.elements).toEqual([
|
|
|
|
expect.objectContaining({ id: "A" }),
|
|
|
|
expect.objectContaining({ id: rectangle.id }),
|
|
|
|
]);
|
|
|
|
h.app.actionManager.executeAction(undoAction);
|
|
|
|
expect(h.elements).toEqual([
|
|
|
|
expect.objectContaining({ id: "A", isDeleted: false }),
|
|
|
|
expect.objectContaining({ id: rectangle.id, isDeleted: true }),
|
|
|
|
]);
|
|
|
|
|
|
|
|
// noop
|
|
|
|
h.app.actionManager.executeAction(undoAction);
|
|
|
|
expect(h.elements).toEqual([
|
|
|
|
expect.objectContaining({ id: "A", isDeleted: false }),
|
|
|
|
expect.objectContaining({ id: rectangle.id, isDeleted: true }),
|
|
|
|
]);
|
|
|
|
expect(API.getStateHistory().length).toBe(1);
|
|
|
|
|
|
|
|
h.app.actionManager.executeAction(redoAction);
|
|
|
|
expect(h.elements).toEqual([
|
|
|
|
expect.objectContaining({ id: "A", isDeleted: false }),
|
|
|
|
expect.objectContaining({ id: rectangle.id, isDeleted: false }),
|
|
|
|
]);
|
|
|
|
expect(API.getStateHistory().length).toBe(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("scene import via drag&drop should create new history entry", async () => {
|
2020-12-05 20:00:53 +05:30
|
|
|
await render(<ExcalidrawApp />, {
|
|
|
|
localStorageData: {
|
|
|
|
elements: [API.createElement({ type: "rectangle", id: "A" })],
|
|
|
|
appState: {
|
|
|
|
viewBackgroundColor: "#FFF",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2020-09-09 21:08:06 +02:00
|
|
|
|
|
|
|
await waitFor(() => expect(h.state.viewBackgroundColor).toBe("#FFF"));
|
|
|
|
await waitFor(() =>
|
|
|
|
expect(h.elements).toEqual([expect.objectContaining({ id: "A" })]),
|
|
|
|
);
|
2020-10-13 13:46:52 +02:00
|
|
|
|
2020-10-30 21:01:41 +01:00
|
|
|
API.drop(
|
|
|
|
new Blob(
|
|
|
|
[
|
|
|
|
JSON.stringify({
|
2021-03-20 20:20:47 +01:00
|
|
|
type: EXPORT_DATA_TYPES.excalidraw,
|
2020-10-30 21:01:41 +01:00
|
|
|
appState: {
|
|
|
|
...getDefaultAppState(),
|
|
|
|
viewBackgroundColor: "#000",
|
|
|
|
},
|
|
|
|
elements: [API.createElement({ type: "rectangle", id: "B" })],
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
{ type: "application/json" },
|
|
|
|
),
|
|
|
|
);
|
2020-09-09 21:08:06 +02:00
|
|
|
|
|
|
|
await waitFor(() => expect(API.getStateHistory().length).toBe(2));
|
|
|
|
expect(h.state.viewBackgroundColor).toBe("#000");
|
|
|
|
expect(h.elements).toEqual([
|
|
|
|
expect.objectContaining({ id: "B", isDeleted: false }),
|
|
|
|
]);
|
|
|
|
|
|
|
|
const undoAction = createUndoAction(h.history);
|
|
|
|
const redoAction = createRedoAction(h.history);
|
|
|
|
h.app.actionManager.executeAction(undoAction);
|
|
|
|
expect(h.elements).toEqual([
|
|
|
|
expect.objectContaining({ id: "A", isDeleted: false }),
|
|
|
|
expect.objectContaining({ id: "B", isDeleted: true }),
|
|
|
|
]);
|
|
|
|
expect(h.state.viewBackgroundColor).toBe("#FFF");
|
|
|
|
h.app.actionManager.executeAction(redoAction);
|
|
|
|
expect(h.state.viewBackgroundColor).toBe("#000");
|
|
|
|
expect(h.elements).toEqual([
|
|
|
|
expect.objectContaining({ id: "B", isDeleted: false }),
|
|
|
|
expect.objectContaining({ id: "A", isDeleted: true }),
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|