Skip to content

Commit

Permalink
strict mode call order
Browse files Browse the repository at this point in the history
  • Loading branch information
honzabrecka committed Nov 27, 2023
1 parent 275599a commit 5b61fe8
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions __tests__/react.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import React, { useEffect, StrictMode } from 'react';
import { render, waitFor } from '@testing-library/react';

export const strictWrapper = ({ children }: any) => (
<StrictMode>{children}</StrictMode>
);

test('react: call order in StrictMode', async () => {
const order = jest.fn();

const Child = () => {
useEffect(() => {
order('Child Effect');
return () => {
order('Child Cleanup');
};
}, []);
return null;
};

const Parent = () => {
useEffect(() => {
order('Parent Effect');
return () => {
order('Parent Cleanup');
};
}, []);
return <Child />;
};

const { unmount } = render(<Parent />, { wrapper: strictWrapper });
unmount();

await waitFor(() => {
expect(order.mock.calls).toEqual([
['Child Effect'],
['Parent Effect'],
['Child Cleanup'],
['Parent Cleanup'],
['Child Effect'],
['Parent Effect'],
['Parent Cleanup'],
['Child Cleanup'],
]);
});
});

test('react: call order in non StrictMode', async () => {
const order = jest.fn();

const Child = () => {
useEffect(() => {
order('Child Effect');
return () => {
order('Child Cleanup');
};
}, []);
return null;
};

const Parent = () => {
useEffect(() => {
order('Parent Effect');
return () => {
order('Parent Cleanup');
};
}, []);
return <Child />;
};

const { unmount } = render(<Parent />);
unmount();

await waitFor(() => {
expect(order.mock.calls).toEqual([
['Child Effect'],
['Parent Effect'],
['Parent Cleanup'],
['Child Cleanup'],
]);
});
});

0 comments on commit 5b61fe8

Please sign in to comment.