Skip to content

Commit

Permalink
chore: Update tests to use jest.mocked
Browse files Browse the repository at this point in the history
  • Loading branch information
mlaursen committed Dec 31, 2021
1 parent 87fe77c commit 4bb25fb
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { mocked } from "ts-jest/utils";
import { render, fireEvent } from "utils/tests";

import DemoSandbox from "../DemoSandbox";
import useSandbox from "../useSandbox";

jest.mock("../useSandbox");

const useSandboxMock = mocked(useSandbox);
const useSandboxMock = jest.mocked(useSandbox);

beforeEach(() => {
useSandboxMock.mockImplementation((defaultSandbox) => ({
Expand Down
4 changes: 2 additions & 2 deletions packages/documentation/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
"isolatedModules": true,
"incremental": true
},
"include": ["next-env.d.ts", "src"],
"exclude": ["src/server", "**/__tests__"]
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}
3 changes: 1 addition & 2 deletions packages/form/src/text-field/__tests__/TextArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
// tests with cypress
import { ReactElement, useState } from "react";
import { act, fireEvent, render } from "@testing-library/react";
import { mocked } from "ts-jest/utils";
import { ResizeObserver } from "@juggle/resize-observer";

import { TextArea } from "../TextArea";

jest.mock("@juggle/resize-observer");

const ResizeObserverMock = mocked(ResizeObserver);
const ResizeObserverMock = jest.mocked(ResizeObserver);
const DEFAULT_DOM_RECT: DOMRectReadOnly = {
x: 0,
y: 0,
Expand Down
9 changes: 4 additions & 5 deletions packages/layout/src/__tests__/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
Routes,
Route,
} from "react-router-dom";
import { mocked } from "ts-jest/utils";
import {
RenderOptions,
fireEvent,
Expand Down Expand Up @@ -46,8 +45,9 @@ jest.mock("@react-md/utils", () => ({
AppSizeListener: jest.fn(),
}));

mocked(AppSizeListener).mockImplementation(
({ children, defaultSize = DEFAULT_APP_SIZE }) => {
jest
.mocked(AppSizeListener)
.mockImplementation(({ children, defaultSize = DEFAULT_APP_SIZE }) => {
const [value, setValue] = useState({ ...defaultSize, __initialized: true });
return (
<AppSizeContext.Provider value={value}>
Expand All @@ -73,8 +73,7 @@ mocked(AppSizeListener).mockImplementation(
)}
</AppSizeContext.Provider>
);
}
);
});

const render = (ui: ReactElement, options?: RenderOptions) =>
baseRender(ui, {
Expand Down
5 changes: 2 additions & 3 deletions packages/utils/src/__tests__/useInterval.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { render } from "@testing-library/react";
import { act, renderHook } from "@testing-library/react-hooks";
import { mocked } from "ts-jest/utils";

import { useInterval } from "../useInterval";

jest.useFakeTimers();
const setInterval = mocked(window.setInterval);
const clearInterval = mocked(window.clearInterval);
const setInterval = jest.mocked(window.setInterval);
const clearInterval = jest.mocked(window.clearInterval);
beforeEach(() => {
setInterval.mockClear();
clearInterval.mockClear();
Expand Down
5 changes: 2 additions & 3 deletions packages/utils/src/__tests__/useTimeout.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { act, renderHook } from "@testing-library/react-hooks";
import { mocked } from "ts-jest/utils";

import { useTimeout } from "../useTimeout";

jest.useFakeTimers();

const setTimeout = mocked(window.setTimeout);
const clearTimeout = mocked(window.clearTimeout);
const setTimeout = jest.mocked(window.setTimeout);
const clearTimeout = jest.mocked(window.clearTimeout);

afterEach(() => {
jest.clearAllMocks();
Expand Down
3 changes: 1 addition & 2 deletions packages/utils/src/layout/__tests__/GridList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
import { ReactElement } from "react";
import { act, render } from "@testing-library/react";
import { ResizeObserver } from "@juggle/resize-observer";
import { mocked } from "ts-jest/utils";

import { GridList, GridListProps } from "../GridList";
import { useGridListSize } from "../useGridList";

jest.mock("@juggle/resize-observer");

const ResizeObserverMock = mocked(ResizeObserver);
const ResizeObserverMock = jest.mocked(ResizeObserver);

const DEFAULT_DOM_RECT: DOMRectReadOnly = {
x: 100,
Expand Down
3 changes: 1 addition & 2 deletions packages/utils/src/sizing/__tests__/useResizeObserver.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { act, render } from "@testing-library/react";
import { ResizeObserver } from "@juggle/resize-observer";
import { mocked } from "ts-jest/utils";

import {
OnResizeObserverChange,
Expand All @@ -10,7 +9,7 @@ import {

jest.mock("@juggle/resize-observer");

const ResizeObserverMock = mocked(ResizeObserver);
const ResizeObserverMock = jest.mocked(ResizeObserver);
const observe = jest.fn();
const unobserve = jest.fn();
const disconnect = jest.fn();
Expand Down
6 changes: 2 additions & 4 deletions packages/utils/src/wia-aria/__tests__/focusElementWithin.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { mocked } from "ts-jest/utils";

import { scrollIntoView as scrollIntoView_ } from "../../scrollIntoView";
import { focusElementWithin } from "../focusElementWithin";
import { getFocusableElements as getFocusableElements_ } from "../getFocusableElements";

jest.mock("../getFocusableElements");
jest.mock("../../scrollIntoView");

const getFocusableElements = mocked(getFocusableElements_);
const scrollIntoView = mocked(scrollIntoView_);
const getFocusableElements = jest.mocked(getFocusableElements_);
const scrollIntoView = jest.mocked(scrollIntoView_);
const ELEMENT = document.createElement("div");
const element1 = document.createElement("button");
element1.className = "element element--1";
Expand Down

0 comments on commit 4bb25fb

Please sign in to comment.