Skip to content

Commit 2227694

Browse files
authored
chore(deps): remove @testing-library/react-hooks (#6565)
1 parent 280a18c commit 2227694

File tree

57 files changed

+458
-533
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+458
-533
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@
125125
"@size-limit/preset-big-lib": "^11.2.0",
126126
"@testing-library/jest-dom": "^6.1.5",
127127
"@testing-library/react": "^14.0.0",
128-
"@testing-library/react-hooks": "^8.0.1",
129128
"@testing-library/user-event": "^14.5.1",
130129
"@types/fs-extra": "11.0.3",
131130
"@types/jest": "^29.5.5",

packages/react-ai/src/hooks/__tests__/createAIHooks.test.tsx

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { act, renderHook } from '@testing-library/react-hooks';
1+
import { act, renderHook, waitFor } from '@testing-library/react';
22
import { createAIHooks } from '../createAIHooks';
33

44
const listMessageMock = jest.fn().mockResolvedValue({ data: [] });
@@ -62,7 +62,7 @@ describe('createAIHooks', () => {
6262
generateRecipeMock.mockResolvedValueOnce(generateReturn);
6363
const { useAIGeneration } = createAIHooks(client);
6464

65-
const { result: hookResult, waitForNextUpdate } = renderHook(() =>
65+
const { result: hookResult } = renderHook(() =>
6666
useAIGeneration('generateRecipe')
6767
);
6868

@@ -76,10 +76,10 @@ describe('createAIHooks', () => {
7676
const [loadingState] = hookResult.current;
7777
expect(loadingState.isLoading).toBeTruthy();
7878

79-
await waitForNextUpdate();
80-
81-
const [awaitedState] = hookResult.current;
82-
expect(awaitedState.data).toStrictEqual(expectedResult);
79+
await waitFor(() => {
80+
const [awaitedState] = hookResult.current;
81+
expect(awaitedState.data).toStrictEqual(expectedResult);
82+
});
8383
});
8484

8585
it('returns a result with graphqlErrors', async () => {
@@ -101,11 +101,11 @@ describe('createAIHooks', () => {
101101
generateRecipeMock.mockResolvedValueOnce(generateReturn);
102102
const { useAIGeneration } = createAIHooks(client);
103103

104-
const { result: hookResult, waitForNextUpdate } = renderHook(() =>
104+
const { result: hookResult } = renderHook(() =>
105105
useAIGeneration('generateRecipe')
106106
);
107107

108-
const [_result, generate] = hookResult.current;
108+
const generate = hookResult.current[1];
109109
act(() => {
110110
generate({
111111
description: 'I want a recipe for a gluten-free chocolate cake.',
@@ -115,14 +115,14 @@ describe('createAIHooks', () => {
115115
const [loadingState] = hookResult.current;
116116
expect(loadingState.isLoading).toBeTruthy();
117117

118-
await waitForNextUpdate();
119-
120-
const [awaitedState] = hookResult.current;
121-
expect(awaitedState.data).toStrictEqual(expectedResult);
122-
expect(awaitedState.isLoading).toBeFalsy();
123-
expect(awaitedState.hasError).toBeTruthy();
124-
expect(awaitedState.messages).toHaveLength(1);
125-
expect(awaitedState.messages?.[0].message).toContain('error');
118+
await waitFor(() => {
119+
const [awaitedState] = hookResult.current;
120+
expect(awaitedState.data).toStrictEqual(expectedResult);
121+
expect(awaitedState.isLoading).toBeFalsy();
122+
expect(awaitedState.hasError).toBeTruthy();
123+
expect(awaitedState.messages).toHaveLength(1);
124+
expect(awaitedState.messages?.[0].message).toContain('error');
125+
});
126126
});
127127
});
128128
});

packages/react-ai/src/hooks/__tests__/useAIConversation.test.ts

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { act, renderHook } from '@testing-library/react-hooks';
1+
import { act, renderHook, waitFor } from '@testing-library/react';
22
import { createAIHooks } from '../createAIHooks';
33
import { ConversationStreamEvent } from '../../types';
44

@@ -15,7 +15,7 @@ const onStreamEventMock = jest
1515
return { unsubscribe: jest.fn() };
1616
}
1717
);
18-
// .mockReturnValue({ unsubscribe: jest.fn() });
18+
1919
const generateRecipeMock = jest.fn();
2020
const id = 'foobar';
2121

@@ -59,18 +59,17 @@ describe('useAIConverstion', () => {
5959

6060
expect(useAIConversation).toBeDefined();
6161

62-
const { result, waitForNextUpdate } = renderHook(() =>
63-
useAIConversation('pirateChat')
64-
);
65-
await waitForNextUpdate();
66-
const [
67-
{
68-
data: { messages },
69-
},
70-
sendMessage,
71-
] = result.current;
72-
expect(messages).toHaveLength(0);
73-
expect(sendMessage).toBeDefined();
62+
const { result } = renderHook(() => useAIConversation('pirateChat'));
63+
await waitFor(() => {
64+
const [
65+
{
66+
data: { messages },
67+
},
68+
sendMessage,
69+
] = result.current;
70+
expect(messages).toHaveLength(0);
71+
expect(sendMessage).toBeDefined();
72+
});
7473
});
7574

7675
it('hook can send a message which updates state', async () => {
@@ -79,62 +78,70 @@ describe('useAIConverstion', () => {
7978

8079
expect(useAIConversation).toBeDefined();
8180

82-
const { result, waitForNextUpdate } = renderHook(() =>
83-
useAIConversation('pirateChat')
84-
);
81+
const { result } = renderHook(() => useAIConversation('pirateChat'));
82+
83+
const initState = result.current[0];
8584

86-
await waitForNextUpdate();
85+
expect(initState.data.conversation).toBeUndefined();
86+
87+
await waitFor(() => {
88+
const nextState = result.current[0];
89+
expect(nextState.data.conversation).toBeDefined();
90+
});
8791

88-
const [_data, sendMessage] = result.current;
92+
const sendMessage = result.current[1];
8993

9094
act(() => {
9195
sendMessage({ content: [{ text: '' }] });
9296
});
9397

94-
expect(result.current[0].data.messages).toHaveLength(2);
95-
expect(sendMessageMock).toHaveBeenCalled();
98+
await waitFor(() => {
99+
expect(result.current[0].data.messages).toHaveLength(2);
100+
expect(sendMessageMock).toHaveBeenCalled();
101+
});
96102
});
97103

98-
it('can call onInitialize', () => {
104+
it('can call onInitialize', async () => {
99105
const client = new mockClient();
100106
const { useAIConversation } = createAIHooks(client);
107+
const onInitialize = jest.fn();
101108

102109
expect(useAIConversation).toBeDefined();
103110

104-
const { waitForNextUpdate } = renderHook(() =>
111+
renderHook(() =>
105112
useAIConversation('pirateChat', {
106-
onInitialize: (conversation) => {
107-
expect(conversation).toBeDefined();
108-
},
113+
onInitialize,
109114
})
110115
);
111116

112-
waitForNextUpdate();
117+
await waitFor(() => {
118+
expect(onInitialize).toHaveBeenCalledTimes(1);
119+
});
113120
});
114121

115122
it('should fire onMessage', async () => {
116123
const client = new mockClient();
117124
const { useAIConversation } = createAIHooks(client);
118125
const onMessage = jest.fn();
119-
const { result, waitForNextUpdate } = renderHook(() =>
126+
const { result } = renderHook(() =>
120127
useAIConversation('pirateChat', { onMessage })
121128
);
122129

123-
await waitForNextUpdate();
124-
125-
const [_data] = result.current;
126-
127-
act(() => {
128-
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
129-
_next({
130-
stopReason: 'end_turn',
131-
conversationId: 'XXX',
132-
id: '123',
133-
contentBlockIndex: 0,
134-
associatedUserMessageId: 'XXX',
130+
await waitFor(() => {
131+
const [_data] = result.current;
132+
133+
act(() => {
134+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
135+
_next({
136+
stopReason: 'end_turn',
137+
conversationId: 'XXX',
138+
id: '123',
139+
contentBlockIndex: 0,
140+
associatedUserMessageId: 'XXX',
141+
});
135142
});
136-
});
137143

138-
expect(onMessage).toHaveBeenCalled();
144+
expect(onMessage).toHaveBeenCalled();
145+
});
139146
});
140147
});

packages/react-ai/src/hooks/__tests__/useAIGeneration.test.tsx

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { renderHook, act } from '@testing-library/react-hooks';
1+
import { renderHook, act, waitFor } from '@testing-library/react';
22
import { createUseAIGeneration } from '../useAIGeneration';
33
import { INITIAL_STATE, LOADING_STATE, ERROR_STATE } from '../shared';
44

@@ -29,22 +29,17 @@ describe('useAIGeneration', () => {
2929
const mockData = { text: 'generated content' };
3030
mockClient.generations.testRoute.mockResolvedValueOnce({ data: mockData });
3131

32-
const { result, waitForNextUpdate } = renderHook(() =>
33-
useAIGeneration('testRoute')
34-
);
32+
const { result } = renderHook(() => useAIGeneration('testRoute'));
3533

3634
act(() => {
3735
result.current[1]({ prompt: 'test prompt' });
3836
});
3937

40-
await waitForNextUpdate();
41-
42-
expect(mockClient.generations.testRoute).toHaveBeenCalledWith({
43-
prompt: 'test prompt',
44-
});
45-
expect(result.current[0]).toEqual({
46-
...INITIAL_STATE,
47-
data: mockData,
38+
await waitFor(() => {
39+
expect(mockClient.generations.testRoute).toHaveBeenCalledWith({
40+
prompt: 'test prompt',
41+
});
42+
expect(result.current[0]).toEqual({ ...INITIAL_STATE, data: mockData });
4843
});
4944
});
5045

@@ -63,10 +58,7 @@ describe('useAIGeneration', () => {
6358
result.current[1]({ prompt: 'test prompt' });
6459
});
6560

66-
expect(result.current[0]).toEqual({
67-
...LOADING_STATE,
68-
data: undefined,
69-
});
61+
expect(result.current[0]).toEqual({ ...LOADING_STATE, data: undefined });
7062
});
7163

7264
it('should handle error state', async () => {
@@ -76,18 +68,16 @@ describe('useAIGeneration', () => {
7668
errors,
7769
});
7870

79-
const { result, waitForNextUpdate } = renderHook(() =>
80-
useAIGeneration('testRoute')
81-
);
71+
const { result } = renderHook(() => useAIGeneration('testRoute'));
8272

8373
act(() => {
8474
result.current[1]({ prompt: 'test prompt' });
8575
});
8676

87-
await waitForNextUpdate();
88-
89-
expect(mockClient.generations.testRoute).toHaveBeenCalledWith({
90-
prompt: 'test prompt',
77+
await waitFor(() => {
78+
expect(mockClient.generations.testRoute).toHaveBeenCalledWith({
79+
prompt: 'test prompt',
80+
});
9181
});
9282

9383
expect(result.current[0]).toEqual({
@@ -105,30 +95,30 @@ describe('useAIGeneration', () => {
10595
data: initialData,
10696
});
10797

108-
const { result, waitForNextUpdate } = renderHook(() =>
109-
useAIGeneration('testRoute')
110-
);
98+
const { result } = renderHook(() => useAIGeneration('testRoute'));
11199

112100
act(() => {
113101
result.current[1]({ prompt: 'first prompt' });
114102
});
115103

116-
await waitForNextUpdate();
104+
await waitFor(() => {
105+
expect(result.current[0]).toEqual({
106+
...INITIAL_STATE,
107+
data: initialData,
108+
});
109+
});
117110

118-
mockClient.generations.testRoute.mockImplementation(
119-
() =>
120-
new Promise((resolve) =>
121-
setTimeout(() => resolve({ data: newData }), 100)
122-
)
123-
);
111+
mockClient.generations.testRoute.mockResolvedValue({ data: newData });
124112

125113
act(() => {
126114
result.current[1]({ prompt: 'second prompt' });
127115
});
128116

129-
expect(result.current[0]).toEqual({
130-
...LOADING_STATE,
131-
data: initialData,
117+
await waitFor(() => {
118+
expect(result.current[0]).toEqual({
119+
...LOADING_STATE,
120+
data: initialData,
121+
});
132122
});
133123
});
134124
});

packages/react-core-auth/src/components/Authenticator/context/ComponentRoute/__tests__/ComponentsRouteContext.spec.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { renderHook } from '@testing-library/react-hooks';
1+
import { renderHook } from '@testing-library/react';
22
import { mockUseMachineOutput } from '../../Machine/__mock__/useMachine';
33
import * as MachineContext from '../../Machine';
44
import {
@@ -32,7 +32,7 @@ describe('useComponentRoute', () => {
3232
wrapper: ComponentRouteProvider,
3333
});
3434

35-
expect(result.current).toBeUndefined();
35+
expect(result.current).toBeNull();
3636
});
3737

3838
it('calls useMachine with routeSelector', () => {

packages/react-core-auth/src/components/Authenticator/context/Machine/__tests__/MachineContext.spec.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { renderHook } from '@testing-library/react-hooks';
2+
import { renderHook } from '@testing-library/react';
33
import { NextAuthenticatorServiceFacade } from '@aws-amplify/ui';
44
import * as UIModule from '@aws-amplify/ui';
55

@@ -59,9 +59,10 @@ describe('useMachine', () => {
5959
});
6060

6161
it('throws an error when used outside an MachineProvider', () => {
62-
const { result } = renderHook(useMachine);
62+
// turn off console.error logging for unhappy path test case
63+
jest.spyOn(console, 'error').mockImplementation(() => {});
6364

64-
expect(result.error?.message).toBe(USE_MACHINE_ERROR);
65+
expect(() => renderHook(useMachine)).toThrow(USE_MACHINE_ERROR);
6566
});
6667

6768
it('returns the expected values', () => {

packages/react-core-auth/src/components/Authenticator/context/Primitives/__tests__/PrimitivesContext.spec.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import { renderHook } from '@testing-library/react-hooks';
1+
import { renderHook } from '@testing-library/react';
22

33
import { PrimitivesProvider, usePrimitives } from '../PrimitivesContext';
44

55
describe('usePrimitives', () => {
66
it('returns the expected values', () => {
77
const { result } = renderHook(() => usePrimitives(), {
8-
wrapper: PrimitivesProvider,
8+
wrapper: PrimitivesProvider as React.ComponentType<{
9+
children?: React.ReactNode;
10+
}>,
911
});
1012

1113
expect(result.current).toStrictEqual({});

packages/react-core/src/Authenticator/hooks/useAuthenticator/__tests__/__snapshots__/useAuthenticator.spec.tsx.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ exports[`useAuthenticator returns the expected values 1`] = `
77
"EMAIL",
88
"TOTP",
99
],
10-
"authStatus": "authenticated",
10+
"authStatus": "configuring",
1111
"challengeName": "SELECT_MFA_TYPE",
1212
"codeDeliveryDetails": {},
1313
"error": undefined,

0 commit comments

Comments
 (0)