Skip to content

Commit 56bc923

Browse files
committed
Add NotFoundPage handling in EditAgentPage for missing agent and personal details
This update introduces a check to display the NotFoundPage when both the agent and personal details are missing after Onyx data is loaded. Additionally, unit tests have been added to ensure the correct rendering of the NotFoundPage under various loading states.
1 parent 6c06940 commit 56bc923

2 files changed

Lines changed: 53 additions & 3 deletions

File tree

src/pages/settings/Agents/EditAgentPage.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {clearAgentAvatarUpdateError, clearAgentNameUpdateError, clearAgentPrompt
1717
import Navigation from '@libs/Navigation/Navigation';
1818
import type {PlatformStackScreenProps} from '@libs/Navigation/PlatformStackNavigation/types';
1919
import type {SettingsNavigatorParamList} from '@libs/Navigation/types';
20+
import NotFoundPage from '@pages/ErrorPage/NotFoundPage';
2021
import CONST from '@src/CONST';
2122
import ONYXKEYS from '@src/ONYXKEYS';
2223
import ROUTES from '@src/ROUTES';
@@ -29,9 +30,11 @@ function EditAgentPage({route}: EditAgentPageProps) {
2930
const styles = useThemeStyles();
3031
const icons = useMemoizedLazyExpensifyIcons(['Trashcan']);
3132
const accountID = route.params.accountID;
32-
const [agent] = useOnyx(`${ONYXKEYS.COLLECTION.SHARED_NVP_AGENT_PROMPT}${accountID}`);
33-
const [personalDetails] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST, {selector: (list) => list?.[accountID]});
33+
const [agent, agentMetadata] = useOnyx(`${ONYXKEYS.COLLECTION.SHARED_NVP_AGENT_PROMPT}${accountID}`);
34+
const [personalDetails, personalDetailsMetadata] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST, {selector: (list) => list?.[accountID]});
3435
const {showConfirmModal} = useConfirmModal();
36+
const isOnyxLoaded = agentMetadata.status === 'loaded' && personalDetailsMetadata.status === 'loaded';
37+
const shouldShowNotFoundPage = isOnyxLoaded && !agent && !personalDetails;
3538

3639
const handleBackPress = () => Navigation.goBack();
3740
const handleEditAvatarPress = () => Navigation.navigate(ROUTES.SETTINGS_AGENTS_EDIT_AVATAR.getRoute(accountID));
@@ -51,6 +54,10 @@ function EditAgentPage({route}: EditAgentPageProps) {
5154
deleteAgent(accountID);
5255
};
5356

57+
if (shouldShowNotFoundPage) {
58+
return <NotFoundPage onBackButtonPress={() => Navigation.goBack(ROUTES.SETTINGS_AGENTS)} />;
59+
}
60+
5461
return (
5562
<ScreenWrapper
5663
testID={EditAgentPage.displayName}

tests/unit/pages/settings/EditAgentPageTest.tsx

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@ jest.mock('@components/ReportActionAvatars', () => {
131131
return MockReportActionAvatars;
132132
});
133133

134+
jest.mock('@pages/ErrorPage/NotFoundPage', () => {
135+
function MockNotFoundPage() {
136+
return 'notFound.notHere';
137+
}
138+
return MockNotFoundPage;
139+
});
140+
134141
const mockUseOnyx = jest.mocked(useOnyx);
135142

136143
const TEST_ACCOUNT_ID = 12345;
@@ -144,7 +151,15 @@ const mockNavigation = {} as EditAgentPageNavigation;
144151
describe('EditAgentPage', () => {
145152
beforeEach(() => {
146153
jest.clearAllMocks();
147-
mockUseOnyx.mockReturnValue([undefined, {status: 'loaded'}]);
154+
mockUseOnyx.mockImplementation((key, options) => {
155+
if (key === ONYXKEYS.PERSONAL_DETAILS_LIST && options?.selector) {
156+
return [{displayName: 'Default Agent'}, {status: 'loaded'}];
157+
}
158+
if (typeof key === 'string' && key.startsWith(ONYXKEYS.COLLECTION.SHARED_NVP_AGENT_PROMPT)) {
159+
return [{prompt: 'Default prompt'}, {status: 'loaded'}];
160+
}
161+
return [undefined, {status: 'loaded'}];
162+
});
148163
});
149164

150165
it('renders agent name from personalDetails', () => {
@@ -229,4 +244,32 @@ describe('EditAgentPage', () => {
229244

230245
expect(JSON.stringify(toJSON())).toContain('agentsPage.error.updatePrompt');
231246
});
247+
248+
it('renders NotFoundPage when agent and personalDetails are both missing after Onyx is loaded', () => {
249+
mockUseOnyx.mockReturnValue([undefined, {status: 'loaded'}]);
250+
251+
const {toJSON} = render(
252+
<EditAgentPage
253+
route={mockRoute}
254+
navigation={mockNavigation}
255+
/>,
256+
);
257+
258+
const serialized = JSON.stringify(toJSON());
259+
expect(serialized).toContain('notFound.notHere');
260+
expect(serialized).not.toContain('editAgentPage.deleteAgent');
261+
});
262+
263+
it('does not render NotFoundPage while Onyx is still loading', () => {
264+
mockUseOnyx.mockReturnValue([undefined, {status: 'loading'}]);
265+
266+
const {toJSON} = render(
267+
<EditAgentPage
268+
route={mockRoute}
269+
navigation={mockNavigation}
270+
/>,
271+
);
272+
273+
expect(JSON.stringify(toJSON())).not.toContain('notFound.notHere');
274+
});
232275
});

0 commit comments

Comments
 (0)