@truedat/test 8.12.6 → 8.12.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,3 +1,16 @@
1
1
  # Truedat Web Test
2
2
 
3
3
  Testing support for Truedat Web Modules.
4
+
5
+ Use `renderWithIntl` from `@truedat/test/renderWithIntl` for components that only
6
+ need internationalization. It accepts `locale`, `messages` keyed by locale, and
7
+ Testing Library render options. Pass `fallback` to add a Suspense boundary for
8
+ components that suspend. It does not install fake timers; configure timers and
9
+ the clock explicitly in tests that need them.
10
+
11
+ Import `waitForLoad` from `@truedat/test/waitForLoad` when a component has loading
12
+ indicators. This standalone helper does not mount providers or install timers.
13
+
14
+ Use Testing Library's plain `render` for components without provider dependencies.
15
+ Use `render` from `@truedat/test/render` when tests need the full Redux, Apollo,
16
+ router, and internationalization setup.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@truedat/test",
3
- "version": "8.12.6",
3
+ "version": "8.12.8",
4
4
  "description": "Truedat Web Test",
5
5
  "sideEffects": false,
6
6
  "module": "src/index.js",
@@ -66,5 +66,5 @@
66
66
  "dependencies": {
67
67
  "react-router": "^8.3.0"
68
68
  },
69
- "gitHead": "fe7375c2d3c6ebdaf3dacd985f56433eda477712"
69
+ "gitHead": "682b801555be3f898e0502fd70a48ea32110c327"
70
70
  }
@@ -1,33 +1,58 @@
1
- import { screen, waitFor } from "@testing-library/react";
1
+ import { lazy } from "react";
2
+ import { MockedProvider } from "@apollo/client/testing";
3
+ import { configureStore } from "@reduxjs/toolkit";
4
+ import { screen } from "@testing-library/react";
5
+ import { FormattedMessage } from "react-intl";
6
+ import { Provider } from "react-redux";
7
+ import { MemoryRouter } from "react-router";
2
8
  import { render, waitForLoad } from "../render";
9
+ import { renderWithIntl } from "../renderWithIntl";
3
10
 
4
- describe("render utility", () => {
5
- it("renders components with default options", () => {
6
- const TestComponent = () => <div>Test Component</div>;
7
- render(<TestComponent />);
8
- expect(screen.getByText("Test Component")).toBeInTheDocument();
9
- });
10
-
11
- it("provides redux store with initial state", () => {
12
- const initialState = { user: { name: "Test User" } };
13
- const TestComponent = () => {
14
- return <div>State Test</div>;
15
- };
11
+ jest.mock("@apollo/client/testing", () => ({
12
+ MockedProvider: jest.fn(({ children }) => children),
13
+ }));
14
+ jest.mock("@reduxjs/toolkit", () => ({
15
+ configureStore: jest.fn(() => ({ dispatch: jest.fn() })),
16
+ }));
17
+ jest.mock("react-redux", () => ({
18
+ Provider: jest.fn(({ children }) => children),
19
+ }));
20
+ jest.mock("react-router", () => ({
21
+ MemoryRouter: jest.fn(({ children }) => children),
22
+ }));
16
23
 
17
- render(<TestComponent />, { state: initialState });
18
- expect(screen.getByText("State Test")).toBeInTheDocument();
19
- });
24
+ describe("render utility", () => {
25
+ it("configures its providers and renders the component", () => {
26
+ const state = { user: { name: "Test User" } };
27
+ const reducer = (currentState) => currentState;
28
+ const routes = ["/test-route"];
29
+ const mocks = [];
30
+ const dispatch = jest.fn();
20
31
 
21
- it("handles routing with MemoryRouter", () => {
22
- const TestComponent = () => <div>Route Test</div>;
23
- render(<TestComponent />, { routes: ["/test-route"] });
24
- expect(screen.getByText("Route Test")).toBeInTheDocument();
25
- });
32
+ render(<div>Test Component</div>, {
33
+ state,
34
+ reducer,
35
+ routes,
36
+ routeIndex: 0,
37
+ mocks,
38
+ dispatch,
39
+ });
26
40
 
27
- it("handles Apollo mocks", async () => {
28
- const TestComponent = () => <div>Apollo Test</div>;
29
- render(<TestComponent />, { mocks: [] });
30
- expect(screen.getByText("Apollo Test")).toBeInTheDocument();
41
+ expect(configureStore).toHaveBeenCalledWith({
42
+ preloadedState: state,
43
+ reducer,
44
+ });
45
+ expect(configureStore.mock.results[0].value.dispatch).toBe(dispatch);
46
+ expect(MemoryRouter.mock.calls[0][0]).toEqual(
47
+ expect.objectContaining({ initialEntries: routes, initialIndex: 0 }),
48
+ );
49
+ expect(Provider.mock.calls[0][0]).toEqual(
50
+ expect.objectContaining({ store: configureStore.mock.results[0].value }),
51
+ );
52
+ expect(MockedProvider.mock.calls[0][0]).toEqual(
53
+ expect.objectContaining({ mocks, addTypename: false }),
54
+ );
55
+ expect(screen.getByText("Test Component")).toBeInTheDocument();
31
56
  });
32
57
 
33
58
  describe("waitForLoad", () => {
@@ -42,6 +67,7 @@ describe("render utility", () => {
42
67
  );
43
68
 
44
69
  const renderResult = render(<TestComponent />);
70
+ const defaultReducer = configureStore.mock.calls.at(-1)[0].reducer;
45
71
 
46
72
  // Simulate loader disappearing
47
73
  setTimeout(() => {
@@ -50,7 +76,25 @@ describe("render utility", () => {
50
76
  }, 100);
51
77
 
52
78
  await waitForLoad(renderResult);
79
+ expect(defaultReducer({ ready: true })).toEqual({ ready: true });
53
80
  expect(screen.getByText("Content")).toBeInTheDocument();
54
81
  });
55
82
  });
56
83
  });
84
+
85
+ describe("renderWithIntl", () => {
86
+ it("keeps the Suspense boundary and messages when rerendering", async () => {
87
+ const LazyMessage = lazy(async () => ({
88
+ default: () => <FormattedMessage id="ready" />,
89
+ }));
90
+ const rendered = renderWithIntl(<div>Initial</div>, {
91
+ messages: { en: { ready: "Ready" } },
92
+ fallback: <div>Loading</div>,
93
+ });
94
+
95
+ rendered.rerender(<LazyMessage />);
96
+
97
+ expect(rendered.getByText("Loading")).toBeInTheDocument();
98
+ expect(await rendered.findByText("Ready")).toBeInTheDocument();
99
+ });
100
+ });
package/src/render.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Suspense } from "react";
2
2
  import { MockedProvider } from "@apollo/client/testing";
3
- import { render, waitFor } from "@testing-library/react";
3
+ import { render } from "@testing-library/react";
4
4
  import { IntlProvider } from "react-intl";
5
5
  import { configureStore } from "@reduxjs/toolkit";
6
6
  import { Provider } from "react-redux";
@@ -82,16 +82,5 @@ const customRender = (
82
82
  return render(component, { wrapper: Wrapper, ...renderOptions });
83
83
  };
84
84
 
85
- export const waitForLoad = async (
86
- { container, queryByText },
87
- fallback = /loading/i,
88
- options,
89
- ) => {
90
- await waitFor(() => {
91
- if (fallback) expect(queryByText(fallback)).not.toBeInTheDocument();
92
- expect(container.querySelector(".loader")).not.toBeInTheDocument();
93
- expect(container.querySelector(".loading")).not.toBeInTheDocument();
94
- }, options);
95
- };
96
-
85
+ export { waitForLoad } from "./waitForLoad";
97
86
  export { customRender as render };
@@ -0,0 +1,28 @@
1
+ import { Suspense } from "react";
2
+ import { render } from "@testing-library/react";
3
+ import { IntlProvider } from "react-intl";
4
+
5
+ export const renderWithIntl = (
6
+ component,
7
+ { locale = "en", messages = { en: {} }, fallback, ...renderOptions } = {},
8
+ ) => {
9
+ // eslint-disable-next-line react/prop-types
10
+ function Wrapper({ children }) {
11
+ return (
12
+ <IntlProvider
13
+ onError={() => {}}
14
+ locale={locale}
15
+ defaultLocale={locale}
16
+ messages={messages[locale]}
17
+ >
18
+ {fallback === undefined ? (
19
+ children
20
+ ) : (
21
+ <Suspense fallback={fallback}>{children}</Suspense>
22
+ )}
23
+ </IntlProvider>
24
+ );
25
+ }
26
+
27
+ return render(component, { wrapper: Wrapper, ...renderOptions });
28
+ };
@@ -0,0 +1,13 @@
1
+ import { waitFor } from "@testing-library/react";
2
+
3
+ export const waitForLoad = async (
4
+ { container, queryByText },
5
+ fallback = /loading/i,
6
+ options,
7
+ ) => {
8
+ await waitFor(() => {
9
+ if (fallback) expect(queryByText(fallback)).not.toBeInTheDocument();
10
+ expect(container.querySelector(".loader")).not.toBeInTheDocument();
11
+ expect(container.querySelector(".loading")).not.toBeInTheDocument();
12
+ }, options);
13
+ };