From 77442842543b4687ab888ab6b7adf42ceaca47a4 Mon Sep 17 00:00:00 2001 From: Marcel Mraz Date: Thu, 20 Mar 2025 23:35:46 +0100 Subject: [PATCH] Fix rest of the tests --- packages/element/tests/sizeHelpers.test.ts | 2 +- packages/excalidraw/components/App.tsx | 55 +++++++++----------- packages/excalidraw/data/blob.ts | 7 ++- packages/excalidraw/tests/clipboard.test.tsx | 2 +- packages/excalidraw/tests/flip.test.tsx | 20 ++++--- packages/excalidraw/tests/helpers/api.ts | 6 +-- packages/excalidraw/tests/move.test.tsx | 2 + 7 files changed, 47 insertions(+), 47 deletions(-) diff --git a/packages/element/tests/sizeHelpers.test.ts b/packages/element/tests/sizeHelpers.test.ts index 89e3da850..168a9a2ad 100644 --- a/packages/element/tests/sizeHelpers.test.ts +++ b/packages/element/tests/sizeHelpers.test.ts @@ -9,7 +9,7 @@ const EPSILON_DIGITS = 3; // below tests. In Jest this wasn't needed as global override was possible // but vite doesn't allow that hence we need to mock vi.mock( - "../constants.ts", + "@excalidraw/common", //@ts-ignore async (importOriginal) => { const module: any = await importOriginal(); diff --git a/packages/excalidraw/components/App.tsx b/packages/excalidraw/components/App.tsx index 76b94516f..db533e9dc 100644 --- a/packages/excalidraw/components/App.tsx +++ b/packages/excalidraw/components/App.tsx @@ -1,9 +1,9 @@ import clsx from "clsx"; import throttle from "lodash.throttle"; -import { nanoid } from "nanoid"; import React, { useContext } from "react"; import { flushSync } from "react-dom"; import rough from "roughjs/bin/rough"; +import { nanoid } from "nanoid"; import { clamp, @@ -73,6 +73,31 @@ import { toValidURL, getGridPoint, getLineHeight, + debounce, + distance, + getFontString, + getNearestScrollableContainer, + isInputLike, + isToolIcon, + isWritableElement, + sceneCoordsToViewportCoords, + tupleToCoors, + viewportCoordsToSceneCoords, + wrapEvent, + updateObject, + updateActiveTool, + getShortcutKey, + isTransparent, + easeToValuesRAF, + muteFSAbortError, + isTestEnv, + easeOut, + updateStable, + addEventListener, + normalizeEOL, + getDateTime, + isShallowEqual, + arrayToMap, type EXPORT_IMAGE_TYPES, } from "@excalidraw/common"; @@ -157,34 +182,6 @@ import { isPathALoop, } from "@excalidraw/element/shapes"; -import { - debounce, - distance, - getFontString, - getNearestScrollableContainer, - isInputLike, - isToolIcon, - isWritableElement, - sceneCoordsToViewportCoords, - tupleToCoors, - viewportCoordsToSceneCoords, - wrapEvent, - updateObject, - updateActiveTool, - getShortcutKey, - isTransparent, - easeToValuesRAF, - muteFSAbortError, - isTestEnv, - easeOut, - updateStable, - addEventListener, - normalizeEOL, - getDateTime, - isShallowEqual, - arrayToMap, -} from "@excalidraw/common"; - import { createSrcDoc, embeddableURLValidator, diff --git a/packages/excalidraw/data/blob.ts b/packages/excalidraw/data/blob.ts index 740e87837..3e5db7c29 100644 --- a/packages/excalidraw/data/blob.ts +++ b/packages/excalidraw/data/blob.ts @@ -9,9 +9,8 @@ import { import { clearElementsForExport } from "@excalidraw/element"; -import type { ExcalidrawElement, FileId } from "@excalidraw/element/types"; - import type { ValueOf } from "@excalidraw/common/utility-types"; +import type { ExcalidrawElement, FileId } from "@excalidraw/element/types"; import { cleanAppStateForExport } from "../appState"; @@ -24,9 +23,9 @@ import { nativeFileSystemSupported } from "./filesystem"; import { isValidExcalidrawData, isValidLibrary } from "./json"; import { restore, restoreLibraryItems } from "./restore"; -import type { FileSystemHandle } from "./filesystem"; - import type { AppState, DataURL, LibraryItem } from "../types"; + +import type { FileSystemHandle } from "./filesystem"; import type { ImportedLibraryData } from "./types"; const parseFileContents = async (blob: Blob | File): Promise => { diff --git a/packages/excalidraw/tests/clipboard.test.tsx b/packages/excalidraw/tests/clipboard.test.tsx index 47d493da7..0759afd94 100644 --- a/packages/excalidraw/tests/clipboard.test.tsx +++ b/packages/excalidraw/tests/clipboard.test.tsx @@ -27,7 +27,7 @@ const { h } = window; const mouse = new Pointer("mouse"); -vi.mock("../keys.ts", async (importOriginal) => { +vi.mock("@excalidraw/common", async (importOriginal) => { const module: any = await importOriginal(); return { __esmodule: true, diff --git a/packages/excalidraw/tests/flip.test.tsx b/packages/excalidraw/tests/flip.test.tsx index 5c3f3e295..22a6c67f8 100644 --- a/packages/excalidraw/tests/flip.test.tsx +++ b/packages/excalidraw/tests/flip.test.tsx @@ -1,8 +1,10 @@ -import { pointFrom, type Radians } from "@excalidraw/math"; import React from "react"; import { vi } from "vitest"; import { ROUNDNESS, KEYS, arrayToMap, cloneJSON } from "@excalidraw/common"; + +import { pointFrom, type Radians } from "@excalidraw/math"; + import { getBoundTextElementPosition } from "@excalidraw/element/textElement"; import { getElementAbsoluteCoords } from "@excalidraw/element/bounds"; import { newLinearElement } from "@excalidraw/element/newElement"; @@ -21,6 +23,9 @@ import { actionFlipHorizontal, actionFlipVertical } from "../actions"; import { createPasteEvent } from "../clipboard"; import { Excalidraw } from "../index"; +// Importing to spy on it and mock the implementation (mocking does not work with simple vi.mock for some reason) +import * as blobModule from "../data/blob"; + import { API } from "./helpers/api"; import { UI, Pointer, Keyboard } from "./helpers/ui"; import { @@ -37,13 +42,12 @@ import type { NormalizedZoomValue } from "../types"; const { h } = window; const mouse = new Pointer("mouse"); -vi.mock("../data/blob", async (actual) => { - const orig: Object = await actual(); - return { - ...orig, - resizeImageFile: (imageFile: File) => imageFile, - generateIdFromFile: () => "fileId" as FileId, - }; +beforeEach(() => { + const generateIdSpy = vi.spyOn(blobModule, "generateIdFromFile"); + const resizeFileSpy = vi.spyOn(blobModule, "resizeImageFile"); + + generateIdSpy.mockImplementation(() => Promise.resolve("fileId" as FileId)); + resizeFileSpy.mockImplementation((file: File) => Promise.resolve(file)); }); beforeEach(async () => { diff --git a/packages/excalidraw/tests/helpers/api.ts b/packages/excalidraw/tests/helpers/api.ts index 2c72aa8e5..09aa308a5 100644 --- a/packages/excalidraw/tests/helpers/api.ts +++ b/packages/excalidraw/tests/helpers/api.ts @@ -4,7 +4,7 @@ import util from "util"; import { pointFrom, type LocalPoint, type Radians } from "@excalidraw/math"; -import { DEFAULT_VERTICAL_ALIGN, ROUNDNESS } from "@excalidraw/common"; +import { DEFAULT_VERTICAL_ALIGN, ROUNDNESS, assertNever } from "@excalidraw/common"; import { mutateElement } from "@excalidraw/element/mutateElement"; import { @@ -19,11 +19,9 @@ import { newMagicFrameElement, newTextElement, } from "@excalidraw/element/newElement"; + import { isLinearElementType } from "@excalidraw/element/typeChecks"; - import { getSelectedElements } from "@excalidraw/element/selection"; -import { assertNever } from "@excalidraw/common"; - import { selectGroupsForSelectedElements } from "@excalidraw/element/groups"; import type { diff --git a/packages/excalidraw/tests/move.test.tsx b/packages/excalidraw/tests/move.test.tsx index f7d41ca44..823056da8 100644 --- a/packages/excalidraw/tests/move.test.tsx +++ b/packages/excalidraw/tests/move.test.tsx @@ -17,6 +17,8 @@ import { Excalidraw } from "../index"; import * as InteractiveCanvas from "../renderer/interactiveScene"; import * as StaticScene from "../renderer/staticScene"; +import "../../utils/test-utils"; + import { UI, Pointer, Keyboard } from "./helpers/ui"; import { render, fireEvent, act, unmountComponent } from "./test-utils";