2022-05-19 00:37:36 +02:00
|
|
|
import { fireEvent, render, waitFor } from "./test-utils";
|
2022-11-04 18:22:21 +05:30
|
|
|
import { queryByTestId } from "@testing-library/react";
|
|
|
|
|
2020-12-05 20:00:53 +05:30
|
|
|
import ExcalidrawApp from "../excalidraw-app";
|
2020-10-30 21:01:41 +01:00
|
|
|
import { API } from "./helpers/api";
|
|
|
|
import { MIME_TYPES } from "../constants";
|
2022-05-11 15:51:02 +02:00
|
|
|
import { LibraryItem, LibraryItems } from "../types";
|
2021-06-28 06:00:33 -04:00
|
|
|
import { UI } from "./helpers/ui";
|
2022-05-11 15:51:02 +02:00
|
|
|
import { serializeLibraryAsJSON } from "../data/json";
|
|
|
|
import { distributeLibraryItemsOnSquareGrid } from "../data/library";
|
|
|
|
import { ExcalidrawGenericElement } from "../element/types";
|
|
|
|
import { getCommonBoundingBox } from "../element/bounds";
|
2022-05-19 00:37:36 +02:00
|
|
|
import { parseLibraryJSON } from "../data/blob";
|
2020-10-30 21:01:41 +01:00
|
|
|
|
|
|
|
const { h } = window;
|
|
|
|
|
2022-05-19 00:37:36 +02:00
|
|
|
const libraryJSONPromise = API.readFile(
|
|
|
|
"./fixtures/fixture_library.excalidrawlib",
|
|
|
|
"utf8",
|
|
|
|
);
|
|
|
|
|
|
|
|
const mockLibraryFilePromise = new Promise<Blob>(async (resolve, reject) => {
|
|
|
|
try {
|
|
|
|
resolve(
|
|
|
|
new Blob([await libraryJSONPromise], { type: MIME_TYPES.excalidrawlib }),
|
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
jest.mock("../data/filesystem.ts", () => ({
|
|
|
|
__esmodule: true,
|
|
|
|
...jest.requireActual("../data/filesystem.ts"),
|
|
|
|
fileOpen: jest.fn(() => mockLibraryFilePromise),
|
|
|
|
}));
|
|
|
|
|
2020-10-30 21:01:41 +01:00
|
|
|
describe("library", () => {
|
2020-12-05 20:00:53 +05:30
|
|
|
beforeEach(async () => {
|
|
|
|
await render(<ExcalidrawApp />);
|
2021-04-21 23:38:24 +05:30
|
|
|
h.app.library.resetLibrary();
|
2020-10-30 21:01:41 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("import library via drag&drop", async () => {
|
2022-04-29 16:45:02 +02:00
|
|
|
expect(await h.app.library.getLatestLibrary()).toEqual([]);
|
2020-10-30 21:01:41 +01:00
|
|
|
await API.drop(
|
|
|
|
await API.loadFile("./fixtures/fixture_library.excalidrawlib"),
|
|
|
|
);
|
|
|
|
await waitFor(async () => {
|
2022-04-29 16:45:02 +02:00
|
|
|
expect(await h.app.library.getLatestLibrary()).toEqual([
|
2021-11-17 23:53:43 +05:30
|
|
|
{
|
|
|
|
status: "unpublished",
|
|
|
|
elements: [expect.objectContaining({ id: "A" })],
|
|
|
|
id: "id0",
|
|
|
|
created: expect.any(Number),
|
|
|
|
},
|
2020-10-30 21:01:41 +01:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// NOTE: mocked to test logic, not actual drag&drop via UI
|
|
|
|
it("drop library item onto canvas", async () => {
|
|
|
|
expect(h.elements).toEqual([]);
|
2022-05-19 00:37:36 +02:00
|
|
|
const libraryItems = parseLibraryJSON(await libraryJSONPromise);
|
2020-10-30 21:01:41 +01:00
|
|
|
await API.drop(
|
2022-05-19 00:37:36 +02:00
|
|
|
new Blob([serializeLibraryAsJSON(libraryItems)], {
|
2020-10-30 21:01:41 +01:00
|
|
|
type: MIME_TYPES.excalidrawlib,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
await waitFor(() => {
|
|
|
|
expect(h.elements).toEqual([expect.objectContaining({ id: "A_copy" })]);
|
|
|
|
});
|
|
|
|
});
|
2021-06-28 06:00:33 -04:00
|
|
|
|
|
|
|
it("inserting library item should revert to selection tool", async () => {
|
|
|
|
UI.clickTool("rectangle");
|
|
|
|
expect(h.elements).toEqual([]);
|
2022-05-19 00:37:36 +02:00
|
|
|
const libraryItems = parseLibraryJSON(await libraryJSONPromise);
|
2021-06-28 06:00:33 -04:00
|
|
|
await API.drop(
|
2022-05-19 00:37:36 +02:00
|
|
|
new Blob([serializeLibraryAsJSON(libraryItems)], {
|
2021-06-28 06:00:33 -04:00
|
|
|
type: MIME_TYPES.excalidrawlib,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
await waitFor(() => {
|
|
|
|
expect(h.elements).toEqual([expect.objectContaining({ id: "A_copy" })]);
|
|
|
|
});
|
2022-03-25 20:46:01 +05:30
|
|
|
expect(h.state.activeTool.type).toBe("selection");
|
2021-06-28 06:00:33 -04:00
|
|
|
});
|
2020-10-30 21:01:41 +01:00
|
|
|
});
|
2022-05-11 15:51:02 +02:00
|
|
|
|
2022-05-19 00:37:36 +02:00
|
|
|
describe("library menu", () => {
|
|
|
|
it("should load library from file picker", async () => {
|
|
|
|
const { container } = await render(<ExcalidrawApp />);
|
|
|
|
|
|
|
|
const latestLibrary = await h.app.library.getLatestLibrary();
|
|
|
|
expect(latestLibrary.length).toBe(0);
|
|
|
|
|
2022-11-04 18:22:21 +05:30
|
|
|
const libraryButton = container.querySelector(".library-button");
|
2022-05-19 00:37:36 +02:00
|
|
|
|
|
|
|
fireEvent.click(libraryButton!);
|
2022-11-04 18:22:21 +05:30
|
|
|
fireEvent.click(container.querySelector(".Sidebar__dropdown-btn")!);
|
|
|
|
queryByTestId(container, "lib-dropdown--load")!.click();
|
2022-05-19 00:37:36 +02:00
|
|
|
|
|
|
|
const libraryItems = parseLibraryJSON(await libraryJSONPromise);
|
|
|
|
|
|
|
|
await waitFor(async () => {
|
|
|
|
const latestLibrary = await h.app.library.getLatestLibrary();
|
|
|
|
expect(latestLibrary.length).toBeGreaterThan(0);
|
|
|
|
expect(latestLibrary.length).toBe(libraryItems.length);
|
|
|
|
expect(latestLibrary[0].elements).toEqual(libraryItems[0].elements);
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(true).toBe(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-05-11 15:51:02 +02:00
|
|
|
describe("distributeLibraryItemsOnSquareGrid()", () => {
|
|
|
|
it("should distribute items on a grid", async () => {
|
|
|
|
const createLibraryItem = (
|
|
|
|
elements: ExcalidrawGenericElement[],
|
|
|
|
): LibraryItem => {
|
|
|
|
return {
|
|
|
|
id: `id-${Date.now()}`,
|
|
|
|
elements,
|
|
|
|
status: "unpublished",
|
|
|
|
created: Date.now(),
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const PADDING = 50;
|
|
|
|
|
|
|
|
const el1 = API.createElement({
|
|
|
|
id: "id1",
|
|
|
|
width: 100,
|
|
|
|
height: 100,
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
});
|
|
|
|
|
|
|
|
const el2 = API.createElement({
|
|
|
|
id: "id2",
|
|
|
|
width: 100,
|
|
|
|
height: 80,
|
|
|
|
x: -100,
|
|
|
|
y: -50,
|
|
|
|
});
|
|
|
|
|
|
|
|
const el3 = API.createElement({
|
|
|
|
id: "id3",
|
|
|
|
width: 40,
|
|
|
|
height: 50,
|
|
|
|
x: -100,
|
|
|
|
y: -50,
|
|
|
|
});
|
|
|
|
|
|
|
|
const el4 = API.createElement({
|
|
|
|
id: "id4",
|
|
|
|
width: 50,
|
|
|
|
height: 50,
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
});
|
|
|
|
|
|
|
|
const el5 = API.createElement({
|
|
|
|
id: "id5",
|
|
|
|
width: 70,
|
|
|
|
height: 100,
|
|
|
|
x: 40,
|
|
|
|
y: 0,
|
|
|
|
});
|
|
|
|
|
|
|
|
const libraryItems: LibraryItems = [
|
|
|
|
createLibraryItem([el1]),
|
|
|
|
createLibraryItem([el2]),
|
|
|
|
createLibraryItem([el3]),
|
|
|
|
createLibraryItem([el4, el5]),
|
|
|
|
];
|
|
|
|
|
|
|
|
const distributed = distributeLibraryItemsOnSquareGrid(libraryItems);
|
|
|
|
// assert the returned library items are flattened to elements
|
|
|
|
expect(distributed.length).toEqual(
|
|
|
|
libraryItems.map((x) => x.elements).flat().length,
|
|
|
|
);
|
|
|
|
expect(distributed).toEqual(
|
|
|
|
expect.arrayContaining([
|
|
|
|
expect.objectContaining({
|
|
|
|
id: el1.id,
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
}),
|
|
|
|
expect.objectContaining({
|
|
|
|
id: el2.id,
|
|
|
|
x:
|
|
|
|
el1.width +
|
|
|
|
PADDING +
|
|
|
|
(getCommonBoundingBox([el4, el5]).width - el2.width) / 2,
|
|
|
|
y: Math.abs(el1.height - el2.height) / 2,
|
|
|
|
}),
|
|
|
|
expect.objectContaining({
|
|
|
|
id: el3.id,
|
|
|
|
x: Math.abs(el1.width - el3.width) / 2,
|
|
|
|
y:
|
|
|
|
Math.max(el1.height, el2.height) +
|
|
|
|
PADDING +
|
|
|
|
Math.abs(el3.height - Math.max(el4.height, el5.height)) / 2,
|
|
|
|
}),
|
|
|
|
expect.objectContaining({
|
|
|
|
id: el4.id,
|
|
|
|
x: Math.max(el1.width, el2.width) + PADDING,
|
|
|
|
y: Math.max(el1.height, el2.height) + PADDING,
|
|
|
|
}),
|
|
|
|
expect.objectContaining({
|
|
|
|
id: el5.id,
|
|
|
|
x: Math.max(el1.width, el2.width) + PADDING + Math.abs(el5.x - el4.x),
|
|
|
|
y:
|
|
|
|
Math.max(el1.height, el2.height) +
|
|
|
|
PADDING +
|
|
|
|
Math.abs(el5.y - el4.y),
|
|
|
|
}),
|
|
|
|
]),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|