Skip to content

Commit

Permalink
make strict mode hack a little clearer
Browse files Browse the repository at this point in the history
  • Loading branch information
Stanislaw Wilczynski authored and Stanislaw Wilczynski committed Jul 26, 2024
1 parent a87def8 commit 57e85aa
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions packages/nova-react/src/eventing/nova-eventing-provider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ describe("useNovaEventing", () => {
const initialChildren = "initial children";
const updatedChildren = "updated children";

// With React strict mode enabled, in development mode, the component will render twice, check https://react.dev/learn/synchronizing-with-effects#how-to-handle-the-effect-firing-twice-in-development
// for more details. This will cause the renderSpy to be called twice on each render, but in prod mode this will only be called once.
// We add this wrapper to encapsulate the logic for artificially increasing the render count to account for strict mode.
const expectComponentToRenderTimes = (count: number) => {
expect(renderSpy).toHaveBeenCalledTimes(count * 2);
};

beforeEach(() => {
jest.clearAllMocks();

Expand Down Expand Up @@ -105,8 +112,7 @@ describe("useNovaEventing", () => {
initialChildren,
);

// called twice on each render due to strict mode
expect(renderSpy).toHaveBeenCalledTimes(2);
expectComponentToRenderTimes(1);

wrapper.rerender(
<NovaEventingProvider
Expand All @@ -118,8 +124,7 @@ describe("useNovaEventing", () => {
expect(wrapper.queryAllByTestId("children")[0].innerHTML).toBe(
updatedChildren,
);
// called twice on each render due to strict mode
expect(renderSpy).toHaveBeenCalledTimes(4);
expectComponentToRenderTimes(2);
});

test("Takes in children and eventing props, creates a stable wrapped NovaReactEventing instance from eventing across re-renders when children do not change.", () => {
Expand All @@ -137,8 +142,7 @@ describe("useNovaEventing", () => {
expect(wrapper.queryAllByTestId("children")[0].innerHTML).toBe(
initialChildren,
);
// called twice on each render due to strict mode
expect(renderSpy).toHaveBeenCalledTimes(2);
expectComponentToRenderTimes(1);

wrapper.rerender(
<NovaEventingProvider
Expand All @@ -150,7 +154,7 @@ describe("useNovaEventing", () => {
expect(wrapper.queryAllByTestId("children")[0].innerHTML).toBe(
initialChildren,
);
expect(renderSpy).toHaveBeenCalledTimes(2);
expectComponentToRenderTimes(1);

// Update eventing instance to test useRef pathway. This will ensure the wrapped eventing instance
// returned from useEventing is stable from one render to the next.
Expand All @@ -167,7 +171,7 @@ describe("useNovaEventing", () => {
expect(wrapper.queryAllByTestId("children")[0].innerHTML).toBe(
initialChildren,
);
expect(renderSpy).toHaveBeenCalledTimes(2);
expectComponentToRenderTimes(1);

//Trigger a callback on the test child through eventing
eventCallback();
Expand All @@ -190,7 +194,7 @@ describe("useNovaEventing", () => {
expect(wrapper.queryAllByTestId("children")[0].innerHTML).toBe(
initialChildren,
);
expect(renderSpy).toHaveBeenCalledTimes(2);
expectComponentToRenderTimes(1);

//Trigger a callback on the test child through eventing
eventCallback();
Expand Down

0 comments on commit 57e85aa

Please sign in to comment.