diff --git a/src/app/features/settings/Persona/PerMessageProfileEditor.test.tsx b/src/app/features/settings/Persona/PerMessageProfileEditor.test.tsx new file mode 100644 index 000000000..37ee53708 --- /dev/null +++ b/src/app/features/settings/Persona/PerMessageProfileEditor.test.tsx @@ -0,0 +1,59 @@ +import { fireEvent, render, screen, waitFor } from '@testing-library/react'; +import { beforeEach, describe, expect, it, vi } from 'vitest'; +import type { MatrixClient } from '$types/matrix-sdk'; +import type * as PerMessageProfileModule from '$hooks/usePerMessageProfile'; +import { PerMessageProfileEditor } from './PerMessageProfileEditor'; + +const mocked = vi.hoisted(() => ({ + addOrUpdate: vi.fn(), + rename: vi.fn(), + remove: vi.fn(), +})); + +vi.mock('$hooks/usePerMessageProfile', async (importOriginal) => ({ + ...(await importOriginal()), + addOrUpdatePerMessageProfile: mocked.addOrUpdate, + renamePerMessageProfile: mocked.rename, + deletePerMessageProfile: mocked.remove, +})); + +vi.mock('$hooks/useMediaAuthentication', () => ({ + useMediaAuthentication: () => false, +})); + +describe('PerMessageProfileEditor', () => { + beforeEach(() => { + vi.clearAllMocks(); + mocked.addOrUpdate.mockResolvedValue(undefined); + mocked.rename.mockResolvedValue(undefined); + }); + + it('uses the renamed persona ID for later saves', async () => { + render( + + ); + + fireEvent.change(screen.getByLabelText('profile id'), { target: { value: 'new-id' } }); + fireEvent.click(screen.getByRole('button', { name: 'Save profile changes for old-id' })); + + await waitFor(() => { + expect(mocked.rename).toHaveBeenCalledWith(expect.anything(), 'old-id', 'new-id'); + }); + + fireEvent.change(screen.getByLabelText('Display name for old-id'), { + target: { value: 'Edited Profile' }, + }); + fireEvent.click(screen.getByRole('button', { name: 'Save profile changes for old-id' })); + + await waitFor(() => expect(mocked.addOrUpdate).toHaveBeenCalledTimes(2)); + expect(mocked.addOrUpdate.mock.calls[1]?.[1]).toMatchObject({ + id: 'new-id', + displayname: 'Edited Profile', + }); + }); +}); diff --git a/src/app/features/settings/Persona/PerMessageProfileEditor.tsx b/src/app/features/settings/Persona/PerMessageProfileEditor.tsx index 165f91a48..039aa3dfe 100644 --- a/src/app/features/settings/Persona/PerMessageProfileEditor.tsx +++ b/src/app/features/settings/Persona/PerMessageProfileEditor.tsx @@ -354,7 +354,7 @@ export function PerMessageProfileEditor({ const [saveState, handleSave] = useAsyncCallback( useCallback(async () => { await addOrUpdatePerMessageProfile(mx, { - id: profileId, + id: currentId, displayname: newDisplayName, avatar_url: avatarMxc, [MATRIX_UNSTABLE_PROFILE_PRONOUNS_PROPERTY_NAME]: newPronouns, @@ -374,12 +374,12 @@ export function PerMessageProfileEditor({ setChangingDisplayName(false); setDisableSetDisplayname(false); if (hasIdChange) { - await renamePerMessageProfile(mx, profileId, newId); + await renamePerMessageProfile(mx, currentId, newId); setCurrentId(newId); } }, [ mx, - profileId, + currentId, newDisplayName, avatarMxc, newPronouns, @@ -393,11 +393,11 @@ export function PerMessageProfileEditor({ const [deleteState, handleDelete] = useAsyncCallback( useCallback(async () => { - await deletePerMessageProfile(mx, profileId); + await deletePerMessageProfile(mx, currentId); setCurrentDisplayName(''); setCurrentPronouns([]); - if (onDelete) onDelete(profileId); - }, [mx, profileId, onDelete]) + if (onDelete) onDelete(currentId); + }, [mx, currentId, onDelete]) ); const handleIdChange = useCallback((e: React.ChangeEvent) => {