Skip to content

Commit

Permalink
[wbt.react18.updates.2] Update data and core packages to use renderHo…
Browse files Browse the repository at this point in the history
…okStatic (#2268)

## Summary:
This is a pre-cursor change to the React 18 work. Here we migrate any uses of `@testing-library/react-hooks/server` imports to use our own `renderHookStatic` function. It's not quite a drop-in replacement, but good enough.

Issue: WB-1730

## Test plan:
`yarn jest`

Author: somewhatabstract

Reviewers: jandrade

Required Reviewers:

Approved By: jandrade

Checks: ⌛ Test (ubuntu-latest, 20.x, 2/2), ⌛ Check build sizes (ubuntu-latest, 20.x), ⌛ Test (ubuntu-latest, 20.x, 1/2), ⌛ Lint (ubuntu-latest, 20.x), ⌛ Chromatic - Build on regular PRs / chromatic (ubuntu-latest, 20.x), ⏭️  Chromatic - Skip on Release PR (changesets), ⌛ Publish npm snapshot (ubuntu-latest, 20.x), ⌛ Check for .changeset entries for all changed files (ubuntu-latest, 20.x), ✅ Prime node_modules cache for primary configuration (ubuntu-latest, 20.x), ✅ gerald, ⏭️  dependabot

Pull Request URL: #2268
  • Loading branch information
somewhatabstract authored Jul 12, 2024
1 parent 2a6c85d commit eab37b8
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 42 deletions.
2 changes: 2 additions & 0 deletions .changeset/metal-cooks-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
6 changes: 6 additions & 0 deletions .changeset/pretty-mangos-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@khanacademy/wonder-blocks-core": patch
"@khanacademy/wonder-blocks-data": patch
---

Test file changes only. No external changes at all.
3 changes: 2 additions & 1 deletion packages/wonder-blocks-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"react-router-dom": "5.3.0"
},
"devDependencies": {
"@khanacademy/wb-dev-build-settings": "^1.0.0"
"@khanacademy/wb-dev-build-settings": "^1.0.0",
"@khanacademy/wonder-blocks-testing-core": "^0.0.1"
},
"author": "",
"license": "MIT"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from "react";
import {renderHook as renderHookOnServer} from "@testing-library/react-hooks/server";
import {renderHookStatic} from "@khanacademy/wonder-blocks-testing-core";
import {renderHook} from "@testing-library/react-hooks";

import {useRenderState} from "../use-render-state";
Expand All @@ -14,7 +14,7 @@ describe("useRenderState", () => {
);

// Act
const {result} = renderHookOnServer(() => useRenderState(), {
const {result} = renderHookStatic(() => useRenderState(), {
wrapper,
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from "react";
import {render} from "@testing-library/react";
import {renderHook} from "@testing-library/react-hooks/server";
import {renderHookStatic} from "@khanacademy/wonder-blocks-testing-core";

import SsrIDFactory from "../../util/ssr-id-factory";
import UniqueIDFactory from "../../util/unique-id-factory";
Expand All @@ -15,7 +15,7 @@ describe("useUniqueIdWithoutMock", () => {
);

// Act
const {result} = renderHook(() => useUniqueIdWithoutMock(), {
const {result} = renderHookStatic(() => useUniqueIdWithoutMock(), {
wrapper,
});

Expand Down Expand Up @@ -93,13 +93,12 @@ describe("useUniqueIdWithoutMock", () => {
// Arrange

// Act
const {result} = renderHook(() => useUniqueIdWithoutMock());
const underTest = () =>
renderHookStatic(() => useUniqueIdWithoutMock());

// Assert
expect(result.error).toEqual(
new Error(
"Components using useUniqueIdWithoutMock() should be descendants of <RenderStateRoot>",
),
expect(underTest).toThrowErrorMatchingInlineSnapshot(
`"Components using useUniqueIdWithoutMock() should be descendants of <RenderStateRoot>"`,
);
});

Expand Down Expand Up @@ -140,7 +139,9 @@ describe("useUniqueIdWithMock", () => {
);

// Act
const {result} = renderHook(() => useUniqueIdWithMock(), {wrapper});
const {result} = renderHookStatic(() => useUniqueIdWithMock(), {
wrapper,
});

// Assert
expect(result.current).toBe(SsrIDFactory);
Expand Down Expand Up @@ -216,13 +217,12 @@ describe("useUniqueIdWithMock", () => {
// Arrange

// Act
const {result} = renderHook(() => useUniqueIdWithMock());
const underTest = () =>
renderHookStatic(() => useUniqueIdWithoutMock());

// Assert
expect(result.error).toEqual(
new Error(
"Components using useUniqueIdWithMock() should be descendants of <RenderStateRoot>",
),
expect(underTest).toThrowErrorMatchingInlineSnapshot(
`"Components using useUniqueIdWithoutMock() should be descendants of <RenderStateRoot>"`,
);
});

Expand Down
4 changes: 3 additions & 1 deletion packages/wonder-blocks-core/tsconfig-build.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@
"outDir": "./dist",
"rootDir": "src",
},
"references": []
"references": [
{"path": "../wonder-blocks-testing-core/tsconfig-build.json"}
]
}
3 changes: 2 additions & 1 deletion packages/wonder-blocks-data/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
},
"devDependencies": {
"@khanacademy/wb-dev-build-settings": "^1.0.0",
"@khanacademy/wonder-stuff-testing": "^3.0.1"
"@khanacademy/wonder-stuff-testing": "^3.0.1",
"@khanacademy/wonder-blocks-testing-core": "^0.0.1"
},
"author": "",
"license": "MIT"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
renderHook as clientRenderHook,
act,
} from "@testing-library/react-hooks";
import {renderHook as serverRenderHook} from "@testing-library/react-hooks/server";
import {renderHookStatic} from "@khanacademy/wonder-blocks-testing-core";
import {render, act as reactAct} from "@testing-library/react";
import {values} from "@khanacademy/wonder-stuff-core";

Expand Down Expand Up @@ -66,7 +66,7 @@ describe("#useCachedEffect", () => {
const fakeHandler = jest.fn();

// Act
serverRenderHook(() => useCachedEffect("ID", fakeHandler));
renderHookStatic(() => useCachedEffect("ID", fakeHandler));

// Assert
expect(useRequestInterceptSpy).toHaveBeenCalledWith(
Expand All @@ -89,7 +89,7 @@ describe("#useCachedEffect", () => {
);

// Act
serverRenderHook(() =>
renderHookStatic(() =>
useCachedEffect("ID", fakeHandler, {scope}),
);

Expand All @@ -108,7 +108,7 @@ describe("#useCachedEffect", () => {
const fakeHandler = jest.fn().mockResolvedValue("data");

// Act
serverRenderHook(() =>
renderHookStatic(() =>
useCachedEffect("ID", fakeHandler, {fetchPolicy}),
);

Expand All @@ -129,7 +129,7 @@ describe("#useCachedEffect", () => {
result: {
current: [result],
},
} = serverRenderHook(() =>
} = renderHookStatic(() =>
useCachedEffect("ID", fakeHandler, {fetchPolicy}),
);

Expand All @@ -149,7 +149,7 @@ describe("#useCachedEffect", () => {
result: {
current: [result],
},
} = serverRenderHook(() =>
} = renderHookStatic(() =>
useCachedEffect("ID", fakeHandler, {
fetchPolicy: FetchPolicy.CacheOnly,
}),
Expand Down Expand Up @@ -177,7 +177,7 @@ describe("#useCachedEffect", () => {
result: {
current: [result],
},
} = serverRenderHook(() =>
} = renderHookStatic(() =>
useCachedEffect("ID", fakeHandler, {fetchPolicy}),
);

Expand All @@ -201,7 +201,7 @@ describe("#useCachedEffect", () => {
result: {
current: [result],
},
} = serverRenderHook(() =>
} = renderHookStatic(() =>
useCachedEffect("ID", fakeHandler, {
fetchPolicy: FetchPolicy.NetworkOnly,
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
renderHook as clientRenderHook,
act,
} from "@testing-library/react-hooks";
import {renderHook as serverRenderHook} from "@testing-library/react-hooks/server";
import {renderHookStatic} from "@khanacademy/wonder-blocks-testing-core";

import {Server} from "@khanacademy/wonder-blocks-core";
import {Status} from "../../util/status";
Expand Down Expand Up @@ -65,7 +65,7 @@ describe("#useHydratableEffect", () => {
const fakeHandler = jest.fn();

// Act
serverRenderHook(() => useHydratableEffect("ID", fakeHandler));
renderHookStatic(() => useHydratableEffect("ID", fakeHandler));

// Assert
expect(useRequestInterceptSpy).toHaveBeenCalledWith(
Expand All @@ -91,7 +91,7 @@ describe("#useHydratableEffect", () => {
const fakeHandler = jest.fn();

// Act
serverRenderHook(() =>
renderHookStatic(() =>
useHydratableEffect("ID", fakeHandler, {
clientBehavior,
}),
Expand Down Expand Up @@ -123,7 +123,7 @@ describe("#useHydratableEffect", () => {
);

// Act
serverRenderHook(() =>
renderHookStatic(() =>
useHydratableEffect("ID", fakeHandler, {scope}),
);

Expand All @@ -141,7 +141,7 @@ describe("#useHydratableEffect", () => {
const fakeHandler = jest.fn().mockResolvedValue("data");

// Act
serverRenderHook(() => useHydratableEffect("ID", fakeHandler));
renderHookStatic(() => useHydratableEffect("ID", fakeHandler));

// Assert
expect(fakeHandler).not.toHaveBeenCalled();
Expand All @@ -155,7 +155,7 @@ describe("#useHydratableEffect", () => {
// Act
const {
result: {current: result},
} = serverRenderHook(() =>
} = renderHookStatic(() =>
useHydratableEffect("ID", fakeHandler),
);

Expand All @@ -176,7 +176,7 @@ describe("#useHydratableEffect", () => {
// Act
const {
result: {current: result},
} = serverRenderHook(() =>
} = renderHookStatic(() =>
useHydratableEffect("ID", fakeHandler),
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {renderHook as clientRenderHook} from "@testing-library/react-hooks";
import {renderHook as serverRenderHook} from "@testing-library/react-hooks/server";
import {renderHookStatic} from "@khanacademy/wonder-blocks-testing-core";

import {Server} from "@khanacademy/wonder-blocks-core";

Expand Down Expand Up @@ -43,7 +43,7 @@ describe("#useServerEffect", () => {
const fakeHandler = jest.fn();

// Act
serverRenderHook(() => useServerEffect("ID", fakeHandler));
renderHookStatic(() => useServerEffect("ID", fakeHandler));

// Assert
expect(useRequestInterceptSpy).toHaveBeenCalledWith("ID", fakeHandler);
Expand All @@ -61,7 +61,7 @@ describe("#useServerEffect", () => {
// Act
const {
result: {current: result},
} = serverRenderHook(() => useServerEffect("ID", fakeHandler));
} = renderHookStatic(() => useServerEffect("ID", fakeHandler));

// Assert
expect(result).toBeNull();
Expand All @@ -76,7 +76,7 @@ describe("#useServerEffect", () => {
);

// Act
serverRenderHook(() => useServerEffect("ID", fakeHandler));
renderHookStatic(() => useServerEffect("ID", fakeHandler));

// Assert
expect(fulfillRequestSpy).not.toHaveBeenCalled();
Expand All @@ -96,7 +96,7 @@ describe("#useServerEffect", () => {
);

// Act
serverRenderHook(() => useServerEffect("ID", fakeHandler), {
renderHookStatic(() => useServerEffect("ID", fakeHandler), {
wrapper: TrackData,
});

Expand All @@ -122,7 +122,7 @@ describe("#useServerEffect", () => {
);

// Act
serverRenderHook(
renderHookStatic(
() => useServerEffect("ID", fakeHandler, {skip: true}),
{
wrapper: TrackData,
Expand Down Expand Up @@ -151,7 +151,7 @@ describe("#useServerEffect", () => {
);

// Act
serverRenderHook(() => useServerEffect("ID", fakeHandler), {
renderHookStatic(() => useServerEffect("ID", fakeHandler), {
wrapper: TrackData,
});

Expand All @@ -170,7 +170,7 @@ describe("#useServerEffect", () => {
// Act
const {
result: {current: result},
} = serverRenderHook(() => useServerEffect("ID", fakeHandler));
} = renderHookStatic(() => useServerEffect("ID", fakeHandler));

// Assert
expect(result).toEqual({status: "success", data: "DATA"});
Expand All @@ -186,7 +186,7 @@ describe("#useServerEffect", () => {
// Act
const {
result: {current: result},
} = serverRenderHook(() => useServerEffect("ID", fakeHandler));
} = renderHookStatic(() => useServerEffect("ID", fakeHandler));

// Assert
expect(result).toEqual({
Expand Down
1 change: 1 addition & 0 deletions packages/wonder-blocks-data/tsconfig-build.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
},
"references": [
{"path": "../wonder-blocks-core/tsconfig-build.json"},
{"path": "../wonder-blocks-testing-core/tsconfig-build.json"}
]
}

0 comments on commit eab37b8

Please sign in to comment.