Skip to content

Commit 8dfd676

Browse files
committed
feat: Refactor test files to use explicit route and navigation props for EditAgent, EditName, and EditPrompt pages
1 parent 0305e50 commit 8dfd676

4 files changed

Lines changed: 112 additions & 22 deletions

File tree

tests/unit/pages/settings/AgentsListRowTest.tsx

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import {fireEvent, render} from '@testing-library/react-native';
1+
import {fireEvent, render, screen} from '@testing-library/react-native';
22
import React from 'react';
3+
import type ReactNative from 'react-native';
34
import useResponsiveLayout from '@hooks/useResponsiveLayout';
45
import Navigation from '@libs/Navigation/Navigation';
56
import AgentsListRow from '@pages/settings/Agents/AgentsListRow';
@@ -57,10 +58,13 @@ jest.mock('@components/OfflineWithFeedback', () => {
5758
});
5859

5960
jest.mock('@components/Button', () => {
60-
const {TouchableOpacity, Text} = jest.requireActual('react-native') as typeof import('react-native');
61+
const {TouchableOpacity, Text} = jest.requireActual<typeof ReactNative>('react-native');
6162
function MockButton({text, onPress}: {text: string; onPress: () => void}) {
6263
return (
63-
<TouchableOpacity onPress={onPress}>
64+
<TouchableOpacity
65+
accessibilityRole="button"
66+
onPress={onPress}
67+
>
6468
<Text>{text}</Text>
6569
</TouchableOpacity>
6670
);
@@ -69,7 +73,7 @@ jest.mock('@components/Button', () => {
6973
});
7074

7175
jest.mock('@components/Pressable/PressableWithFeedback', () => {
72-
const {TouchableOpacity} = jest.requireActual('react-native') as typeof import('react-native');
76+
const {TouchableOpacity} = jest.requireActual<typeof ReactNative>('react-native');
7377
function MockPressableWithFeedback({children, onPress, role}: {children: React.ReactNode; onPress: () => void; role?: string}) {
7478
return (
7579
<TouchableOpacity
@@ -87,11 +91,6 @@ const mockUseResponsiveLayout = jest.mocked(useResponsiveLayout);
8791
const mockNavigate = jest.mocked(Navigation.navigate);
8892

8993
const TEST_ACCOUNT_ID = 12345;
90-
const DEFAULT_PROPS = {
91-
accountID: TEST_ACCOUNT_ID,
92-
displayName: 'Test Agent',
93-
login: 'agent@example.com',
94-
};
9594

9695
const BASE_LAYOUT = {
9796
shouldUseNarrowLayout: false,
@@ -116,23 +115,41 @@ describe('AgentsListRow', () => {
116115
it('narrow layout: does not show Edit button', () => {
117116
mockUseResponsiveLayout.mockReturnValue({...BASE_LAYOUT, shouldUseNarrowLayout: true});
118117

119-
const {toJSON} = render(<AgentsListRow {...DEFAULT_PROPS} />);
118+
const {toJSON} = render(
119+
<AgentsListRow
120+
accountID={TEST_ACCOUNT_ID}
121+
displayName="Test Agent"
122+
login="agent@example.com"
123+
/>,
124+
);
120125

121126
expect(JSON.stringify(toJSON())).not.toContain('common.edit');
122127
});
123128

124129
it('wide layout: shows Edit button', () => {
125130
mockUseResponsiveLayout.mockReturnValue({...BASE_LAYOUT, shouldUseNarrowLayout: false});
126131

127-
const {toJSON} = render(<AgentsListRow {...DEFAULT_PROPS} />);
132+
const {toJSON} = render(
133+
<AgentsListRow
134+
accountID={TEST_ACCOUNT_ID}
135+
displayName="Test Agent"
136+
login="agent@example.com"
137+
/>,
138+
);
128139

129140
expect(JSON.stringify(toJSON())).toContain('common.edit');
130141
});
131142

132143
it('narrow layout: entire row is pressable', () => {
133144
mockUseResponsiveLayout.mockReturnValue({...BASE_LAYOUT, shouldUseNarrowLayout: true});
134145

135-
const {toJSON} = render(<AgentsListRow {...DEFAULT_PROPS} />);
146+
const {toJSON} = render(
147+
<AgentsListRow
148+
accountID={TEST_ACCOUNT_ID}
149+
displayName="Test Agent"
150+
login="agent@example.com"
151+
/>,
152+
);
136153
const output = JSON.stringify(toJSON());
137154

138155
expect(output).toContain('button');
@@ -141,9 +158,15 @@ describe('AgentsListRow', () => {
141158
it('wide layout: pressing Edit button calls Navigation.navigate with correct route', () => {
142159
mockUseResponsiveLayout.mockReturnValue({...BASE_LAYOUT, shouldUseNarrowLayout: false});
143160

144-
const {getByText} = render(<AgentsListRow {...DEFAULT_PROPS} />);
161+
render(
162+
<AgentsListRow
163+
accountID={TEST_ACCOUNT_ID}
164+
displayName="Test Agent"
165+
login="agent@example.com"
166+
/>,
167+
);
145168

146-
fireEvent.press(getByText('common.edit'));
169+
fireEvent.press(screen.getByText('common.edit'));
147170

148171
expect(mockNavigate).toHaveBeenCalledWith(ROUTES.SETTINGS_AGENTS_EDIT.getRoute(TEST_ACCOUNT_ID));
149172
});

tests/unit/pages/settings/EditAgentPageTest.tsx

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import {render} from '@testing-library/react-native';
22
import React from 'react';
33
import useOnyx from '@hooks/useOnyx';
4+
import type {PlatformStackScreenProps} from '@libs/Navigation/PlatformStackNavigation/types';
5+
import type {SettingsNavigatorParamList} from '@libs/Navigation/types';
46
import EditAgentPage from '@pages/settings/Agents/EditAgentPage';
57
import ONYXKEYS from '@src/ONYXKEYS';
8+
import type SCREENS from '@src/SCREENS';
69

710
jest.mock('@userActions/Agent', () => ({
811
deleteAgent: jest.fn(),
@@ -120,6 +123,12 @@ const mockUseOnyx = jest.mocked(useOnyx);
120123

121124
const TEST_ACCOUNT_ID = 12345;
122125

126+
type EditAgentPageRoute = PlatformStackScreenProps<SettingsNavigatorParamList, typeof SCREENS.SETTINGS.AGENTS.EDIT>['route'];
127+
type EditAgentPageNavigation = PlatformStackScreenProps<SettingsNavigatorParamList, typeof SCREENS.SETTINGS.AGENTS.EDIT>['navigation'];
128+
129+
const mockRoute = {params: {accountID: TEST_ACCOUNT_ID}} as EditAgentPageRoute;
130+
const mockNavigation = {} as EditAgentPageNavigation;
131+
123132
describe('EditAgentPage', () => {
124133
beforeEach(() => {
125134
jest.clearAllMocks();
@@ -134,7 +143,12 @@ describe('EditAgentPage', () => {
134143
return [undefined, {status: 'loaded'}];
135144
});
136145

137-
const {toJSON} = render(<EditAgentPage {...({route: {params: {accountID: TEST_ACCOUNT_ID}}} as any)} />);
146+
const {toJSON} = render(
147+
<EditAgentPage
148+
route={mockRoute}
149+
navigation={mockNavigation}
150+
/>,
151+
);
138152

139153
expect(JSON.stringify(toJSON())).toContain('Test Agent');
140154
});
@@ -147,13 +161,23 @@ describe('EditAgentPage', () => {
147161
return [undefined, {status: 'loaded'}];
148162
});
149163

150-
const {toJSON} = render(<EditAgentPage {...({route: {params: {accountID: TEST_ACCOUNT_ID}}} as any)} />);
164+
const {toJSON} = render(
165+
<EditAgentPage
166+
route={mockRoute}
167+
navigation={mockNavigation}
168+
/>,
169+
);
151170

152171
expect(JSON.stringify(toJSON())).toContain('Reject all gambling expenses.');
153172
});
154173

155174
it('renders delete agent menu item', () => {
156-
const {toJSON} = render(<EditAgentPage {...({route: {params: {accountID: TEST_ACCOUNT_ID}}} as any)} />);
175+
const {toJSON} = render(
176+
<EditAgentPage
177+
route={mockRoute}
178+
navigation={mockNavigation}
179+
/>,
180+
);
157181

158182
expect(JSON.stringify(toJSON())).toContain('editAgentPage.deleteAgent');
159183
});
@@ -166,7 +190,12 @@ describe('EditAgentPage', () => {
166190
return [undefined, {status: 'loaded'}];
167191
});
168192

169-
const {toJSON} = render(<EditAgentPage {...({route: {params: {accountID: TEST_ACCOUNT_ID}}} as any)} />);
193+
const {toJSON} = render(
194+
<EditAgentPage
195+
route={mockRoute}
196+
navigation={mockNavigation}
197+
/>,
198+
);
170199

171200
expect(JSON.stringify(toJSON())).toContain('agentsPage.error.genericUpdate');
172201
});

tests/unit/pages/settings/EditNamePageTest.tsx

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import {render} from '@testing-library/react-native';
22
import React from 'react';
33
import useOnyx from '@hooks/useOnyx';
4+
import type {PlatformStackScreenProps} from '@libs/Navigation/PlatformStackNavigation/types';
5+
import type {SettingsNavigatorParamList} from '@libs/Navigation/types';
46
import EditNamePage from '@pages/settings/Agents/Fields/EditNamePage';
57
import ONYXKEYS from '@src/ONYXKEYS';
8+
import type SCREENS from '@src/SCREENS';
69

710
jest.mock('@userActions/Agent', () => ({
811
updateAgentName: jest.fn(),
@@ -76,14 +79,25 @@ const mockUseOnyx = jest.mocked(useOnyx);
7679

7780
const TEST_ACCOUNT_ID = 12345;
7881

82+
type EditNamePageRoute = PlatformStackScreenProps<SettingsNavigatorParamList, typeof SCREENS.SETTINGS.AGENTS.EDIT_NAME>['route'];
83+
type EditNamePageNavigation = PlatformStackScreenProps<SettingsNavigatorParamList, typeof SCREENS.SETTINGS.AGENTS.EDIT_NAME>['navigation'];
84+
85+
const mockRoute = {params: {accountID: TEST_ACCOUNT_ID}} as EditNamePageRoute;
86+
const mockNavigation = {} as EditNamePageNavigation;
87+
7988
describe('EditNamePage', () => {
8089
beforeEach(() => {
8190
jest.clearAllMocks();
8291
mockUseOnyx.mockReturnValue([undefined, {status: 'loaded'}]);
8392
});
8493

8594
it('renders page title', () => {
86-
const {toJSON} = render(<EditNamePage {...({route: {params: {accountID: TEST_ACCOUNT_ID}}} as any)} />);
95+
const {toJSON} = render(
96+
<EditNamePage
97+
route={mockRoute}
98+
navigation={mockNavigation}
99+
/>,
100+
);
87101

88102
expect(JSON.stringify(toJSON())).toContain('editAgentNamePage.title');
89103
});
@@ -96,7 +110,12 @@ describe('EditNamePage', () => {
96110
return [undefined, {status: 'loaded'}];
97111
});
98112

99-
const {toJSON} = render(<EditNamePage {...({route: {params: {accountID: TEST_ACCOUNT_ID}}} as any)} />);
113+
const {toJSON} = render(
114+
<EditNamePage
115+
route={mockRoute}
116+
navigation={mockNavigation}
117+
/>,
118+
);
100119

101120
expect(JSON.stringify(toJSON())).toContain('firstName::Old Name');
102121
});

tests/unit/pages/settings/EditPromptPageTest.tsx

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import {render} from '@testing-library/react-native';
22
import React from 'react';
33
import useOnyx from '@hooks/useOnyx';
4+
import type {PlatformStackScreenProps} from '@libs/Navigation/PlatformStackNavigation/types';
5+
import type {SettingsNavigatorParamList} from '@libs/Navigation/types';
46
import EditPromptPage from '@pages/settings/Agents/Fields/EditPromptPage';
57
import ONYXKEYS from '@src/ONYXKEYS';
8+
import type SCREENS from '@src/SCREENS';
69

710
jest.mock('@userActions/Agent', () => ({
811
updateAgentPrompt: jest.fn(),
@@ -76,14 +79,25 @@ const mockUseOnyx = jest.mocked(useOnyx);
7679

7780
const TEST_ACCOUNT_ID = 12345;
7881

82+
type EditPromptPageRoute = PlatformStackScreenProps<SettingsNavigatorParamList, typeof SCREENS.SETTINGS.AGENTS.EDIT_PROMPT>['route'];
83+
type EditPromptPageNavigation = PlatformStackScreenProps<SettingsNavigatorParamList, typeof SCREENS.SETTINGS.AGENTS.EDIT_PROMPT>['navigation'];
84+
85+
const mockRoute = {params: {accountID: TEST_ACCOUNT_ID}} as EditPromptPageRoute;
86+
const mockNavigation = {} as EditPromptPageNavigation;
87+
7988
describe('EditPromptPage', () => {
8089
beforeEach(() => {
8190
jest.clearAllMocks();
8291
mockUseOnyx.mockReturnValue([undefined, {status: 'loaded'}]);
8392
});
8493

8594
it('renders page title', () => {
86-
const {toJSON} = render(<EditPromptPage {...({route: {params: {accountID: TEST_ACCOUNT_ID}}} as any)} />);
95+
const {toJSON} = render(
96+
<EditPromptPage
97+
route={mockRoute}
98+
navigation={mockNavigation}
99+
/>,
100+
);
87101

88102
expect(JSON.stringify(toJSON())).toContain('editAgentPromptPage.title');
89103
});
@@ -96,7 +110,12 @@ describe('EditPromptPage', () => {
96110
return [undefined, {status: 'loaded'}];
97111
});
98112

99-
const {toJSON} = render(<EditPromptPage {...({route: {params: {accountID: TEST_ACCOUNT_ID}}} as any)} />);
113+
const {toJSON} = render(
114+
<EditPromptPage
115+
route={mockRoute}
116+
navigation={mockNavigation}
117+
/>,
118+
);
100119

101120
expect(JSON.stringify(toJSON())).toContain('prompt::Old prompt');
102121
});

0 commit comments

Comments
 (0)