Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/pages/settings/Agents/NewAgentPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import Navigation from '@libs/Navigation/Navigation';
import type {PlatformStackScreenProps} from '@libs/Navigation/PlatformStackNavigation/types';
import type {SettingsNavigatorParamList} from '@libs/Navigation/types';

import {clearNewAgentTemplate, setNewAgentTemplate, getAgentTemplates} from '@userActions/Agent';
import {clearNewAgentAvatarDraft, clearNewAgentTemplate, setNewAgentTemplate, getAgentTemplates} from '@userActions/Agent';

import CONST from '@src/CONST';
import ROUTES from '@src/ROUTES';
Expand Down Expand Up @@ -92,15 +92,15 @@ function NewAgentPage({route}: NewAgentPageProps) {
}, [isOffline]);

const handleBuildCustomAgent = () => {
// Start from scratch — drop any previously stashed template
clearNewAgentTemplate().then(() => {
// Start from scratch — drop any previously stashed template and avatar draft.
Promise.all([clearNewAgentTemplate(), clearNewAgentAvatarDraft()]).then(() => {
Navigation.navigate(ROUTES.SETTINGS_AGENTS_ADD.getRoute(policyID ? {policyID} : undefined));
});
};

const handleAddTemplate = (template: SuggestedAgent, avatarID: AgentAvatarID) => {
// Stash the template in Onyx (persists across refresh)
setNewAgentTemplate({name: template.name, prompt: template.prompt, avatarID}).then(() => {
// Stash the template in Onyx (persists across refresh) and drop any stale avatar draft.
Promise.all([setNewAgentTemplate({name: template.name, prompt: template.prompt, avatarID}), clearNewAgentAvatarDraft()]).then(() => {
Navigation.navigate(ROUTES.SETTINGS_AGENTS_ADD.getRoute(policyID ? {policyID} : undefined));
});
};
Expand Down
11 changes: 8 additions & 3 deletions tests/unit/pages/settings/NewAgentPageTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type {SettingsNavigatorParamList} from '@libs/Navigation/types';

import NewAgentPage from '@pages/settings/Agents/NewAgentPage';

import {clearNewAgentTemplate, setNewAgentTemplate} from '@userActions/Agent';
import {clearNewAgentAvatarDraft, clearNewAgentTemplate, setNewAgentTemplate} from '@userActions/Agent';

import ROUTES from '@src/ROUTES';
import SCREENS from '@src/SCREENS';
Expand Down Expand Up @@ -44,6 +44,7 @@ jest.mock('@hooks/useThemeStyles', () =>
jest.mock('@userActions/Agent', () => ({
setNewAgentTemplate: jest.fn(() => Promise.resolve()),
clearNewAgentTemplate: jest.fn(() => Promise.resolve()),
clearNewAgentAvatarDraft: jest.fn(() => Promise.resolve()),
getAgentTemplates: jest.fn(),
}));
jest.mock('@libs/Navigation/Navigation', () => ({
Expand Down Expand Up @@ -104,6 +105,7 @@ const mockedUseNetwork = jest.mocked(useNetwork);
const mockNavigate = jest.mocked(Navigation.navigate);
const mockSetNewAgentTemplate = jest.mocked(setNewAgentTemplate);
const mockClearNewAgentTemplate = jest.mocked(clearNewAgentTemplate);
const mockClearNewAgentAvatarDraft = jest.mocked(clearNewAgentAvatarDraft);

type NewAgentRouteProp = PlatformStackRouteProp<SettingsNavigatorParamList, typeof SCREENS.SETTINGS.AGENTS.NEW>;
type NewAgentNavigationProp = PlatformStackScreenProps<SettingsNavigatorParamList, typeof SCREENS.SETTINGS.AGENTS.NEW>['navigation'];
Expand Down Expand Up @@ -134,6 +136,7 @@ describe('NewAgentPage', () => {
mockedUseSuggestedAgents.mockReturnValue({data: TEMPLATES, isLoading: false});
mockSetNewAgentTemplate.mockResolvedValue(undefined);
mockClearNewAgentTemplate.mockResolvedValue(undefined);
mockClearNewAgentAvatarDraft.mockResolvedValue(undefined);
});

it('always renders the build custom agent button', () => {
Expand Down Expand Up @@ -187,7 +190,7 @@ describe('NewAgentPage', () => {
expect(screen.queryByText('newAgentPage.emptyTemplatesSubtitle')).toBeNull();
});

it('stashes the picked template and opens the builder when Add is pressed', async () => {
it('stashes the picked template, clears the stale avatar draft and opens the builder when Add is pressed', async () => {
renderNewAgentPage();

const firstAddButton = screen.getAllByText('common.add').at(0);
Expand All @@ -202,17 +205,19 @@ describe('NewAgentPage', () => {
prompt: 'Reject pricey expenses.',
avatarID: AGENT_AVATARS.ordered.at(0)?.id,
});
expect(mockClearNewAgentAvatarDraft).toHaveBeenCalledTimes(1);
await waitFor(() => {
expect(mockNavigate).toHaveBeenCalledWith(ROUTES.SETTINGS_AGENTS_ADD.getRoute());
});
});

it('clears any stashed template and opens a blank builder when Build custom agent is pressed', async () => {
it('clears any stashed template and avatar draft and opens a blank builder when Build custom agent is pressed', async () => {
renderNewAgentPage();

fireEvent.press(screen.getByText('newAgentPage.buildCustomAgent'));

expect(mockClearNewAgentTemplate).toHaveBeenCalledTimes(1);
expect(mockClearNewAgentAvatarDraft).toHaveBeenCalledTimes(1);
expect(mockSetNewAgentTemplate).not.toHaveBeenCalled();
await waitFor(() => {
expect(mockNavigate).toHaveBeenCalledWith(ROUTES.SETTINGS_AGENTS_ADD.getRoute());
Expand Down
Loading