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 c911d85
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
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'],
]);
});
});
2 changes: 1 addition & 1 deletion src/useField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export default function useField({
to = identity,
// NOTE: should be unchanged - used only when initializing, any other update has no effect
dirtyComparator,
preserveStateAfterUnmount = true,
preserveStateAfterUnmount = false,
}: UseFieldProps): UseFieldResult {
const formId = useFormId(formIdProp);

Expand Down

0 comments on commit c911d85

Please sign in to comment.