Skip to content

Commit ba12927

Browse files
Issue #50 use-async-effect.ts test flakes
Update use-async-effect tests to call 'unmount' function returned by component render, vs. relying on globally provided 'cleanup' function
2 parents c5d148b + 9e4d380 commit ba12927

1 file changed

Lines changed: 8 additions & 9 deletions

File tree

src/hooks/use-async-effect.test.tsx

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from "react";
2-
import { cleanup, render, waitFor } from "@testing-library/react";
2+
import { render, waitFor } from "@testing-library/react";
33
import { useAsyncEffect } from "./use-async-effect";
44
import { CoreUtils } from "andculturecode-javascript-core";
55
import { AsyncEffectCallback } from "../types/async-effect-callback-type";
@@ -11,7 +11,7 @@ describe("useAsyncEffect", () => {
1111
return null;
1212
};
1313

14-
render(<TestComponent />);
14+
return render(<TestComponent />);
1515
};
1616

1717
test("executes async method", async () => {
@@ -26,7 +26,6 @@ describe("useAsyncEffect", () => {
2626

2727
// Assert
2828
await waitFor(() => expect(mockedMethod).toBeCalledTimes(1));
29-
await cleanup();
3029
});
3130

3231
test("executes cleanup method", async () => {
@@ -35,15 +34,15 @@ describe("useAsyncEffect", () => {
3534
const mockedCleanupMethod = jest.fn();
3635

3736
// Act
38-
setupUseAsyncEffect(async () => {
37+
const { unmount } = setupUseAsyncEffect(async () => {
3938
await CoreUtils.sleep(1);
4039
mockedMethod();
4140
return mockedCleanupMethod;
4241
});
4342

4443
// Assert
4544
await waitFor(() => expect(mockedMethod).toBeCalledTimes(1));
46-
await cleanup();
45+
unmount();
4746
await waitFor(() => expect(mockedCleanupMethod).toBeCalledTimes(1));
4847
});
4948

@@ -53,14 +52,14 @@ describe("useAsyncEffect", () => {
5352
const expectedIsMountedValue: boolean = true;
5453

5554
// Act
56-
setupUseAsyncEffect(async (isMounted) => {
55+
const { unmount } = setupUseAsyncEffect(async (isMounted) => {
5756
actualIsMountedValue = isMounted();
5857
await CoreUtils.sleep(1);
5958
});
6059

6160
// Assert
6261
expect(actualIsMountedValue).toBe(expectedIsMountedValue);
63-
await cleanup();
62+
unmount();
6463
});
6564

6665
test("isMounted equals false after cleanup", async () => {
@@ -70,14 +69,14 @@ describe("useAsyncEffect", () => {
7069
const mockedMethod = jest.fn();
7170

7271
// Act
73-
setupUseAsyncEffect(async (isMounted) => {
72+
const { unmount } = setupUseAsyncEffect(async (isMounted) => {
7473
await CoreUtils.sleep(1);
7574
actualIsMountedValue = isMounted();
7675
mockedMethod();
7776
});
7877

7978
// Assert
80-
await cleanup();
79+
unmount();
8180
await waitFor(() => expect(mockedMethod).toBeCalledTimes(1));
8281
expect(actualIsMountedValue).toBe(expectedIsMountedValue);
8382
});

0 commit comments

Comments
 (0)