test: add tests for loading library from file picker (#5206)

This commit is contained in:
David Luzar 2022-05-19 00:37:36 +02:00 committed by GitHub
parent 605aa554d0
commit 92f30f7ed6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,4 @@
import { render, waitFor } from "./test-utils"; import { fireEvent, render, waitFor } from "./test-utils";
import ExcalidrawApp from "../excalidraw-app"; import ExcalidrawApp from "../excalidraw-app";
import { API } from "./helpers/api"; import { API } from "./helpers/api";
import { MIME_TYPES } from "../constants"; import { MIME_TYPES } from "../constants";
@ -8,9 +8,31 @@ import { serializeLibraryAsJSON } from "../data/json";
import { distributeLibraryItemsOnSquareGrid } from "../data/library"; import { distributeLibraryItemsOnSquareGrid } from "../data/library";
import { ExcalidrawGenericElement } from "../element/types"; import { ExcalidrawGenericElement } from "../element/types";
import { getCommonBoundingBox } from "../element/bounds"; import { getCommonBoundingBox } from "../element/bounds";
import { parseLibraryJSON } from "../data/blob";
const { h } = window; const { h } = window;
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),
}));
describe("library", () => { describe("library", () => {
beforeEach(async () => { beforeEach(async () => {
await render(<ExcalidrawApp />); await render(<ExcalidrawApp />);
@ -37,11 +59,9 @@ describe("library", () => {
// NOTE: mocked to test logic, not actual drag&drop via UI // NOTE: mocked to test logic, not actual drag&drop via UI
it("drop library item onto canvas", async () => { it("drop library item onto canvas", async () => {
expect(h.elements).toEqual([]); expect(h.elements).toEqual([]);
const libraryItems: LibraryItem = JSON.parse( const libraryItems = parseLibraryJSON(await libraryJSONPromise);
await API.readFile("./fixtures/fixture_library.excalidrawlib", "utf8"),
).library[0];
await API.drop( await API.drop(
new Blob([serializeLibraryAsJSON([libraryItems])], { new Blob([serializeLibraryAsJSON(libraryItems)], {
type: MIME_TYPES.excalidrawlib, type: MIME_TYPES.excalidrawlib,
}), }),
); );
@ -53,11 +73,9 @@ describe("library", () => {
it("inserting library item should revert to selection tool", async () => { it("inserting library item should revert to selection tool", async () => {
UI.clickTool("rectangle"); UI.clickTool("rectangle");
expect(h.elements).toEqual([]); expect(h.elements).toEqual([]);
const libraryItems: LibraryItem = JSON.parse( const libraryItems = parseLibraryJSON(await libraryJSONPromise);
await API.readFile("./fixtures/fixture_library.excalidrawlib", "utf8"),
).library[0];
await API.drop( await API.drop(
new Blob([serializeLibraryAsJSON([libraryItems])], { new Blob([serializeLibraryAsJSON(libraryItems)], {
type: MIME_TYPES.excalidrawlib, type: MIME_TYPES.excalidrawlib,
}), }),
); );
@ -68,6 +86,36 @@ describe("library", () => {
}); });
}); });
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);
const libraryButton = container.querySelector(".ToolIcon__library");
fireEvent.click(libraryButton!);
const loadLibraryButton = container.querySelector(
".library-actions .library-actions--load",
);
fireEvent.click(loadLibraryButton!);
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);
});
});
describe("distributeLibraryItemsOnSquareGrid()", () => { describe("distributeLibraryItemsOnSquareGrid()", () => {
it("should distribute items on a grid", async () => { it("should distribute items on a grid", async () => {
const createLibraryItem = ( const createLibraryItem = (