fix persona edits after renaming

This commit is contained in:
Erwan Leboucher committed 2026-08-09 17:02:39 +02:00
1 parent 31fa6c42e8
commit aa86c1a87b
2 files changed
+65 -6

No files matched your search

@@ -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<typeof PerMessageProfileModule.addOrUpdatePerMessageProfile>(),
rename: vi.fn<typeof PerMessageProfileModule.renamePerMessageProfile>(),
remove: vi.fn<typeof PerMessageProfileModule.deletePerMessageProfile>(),
}));
vi.mock('$hooks/usePerMessageProfile', async (importOriginal) => ({
...(await importOriginal<typeof PerMessageProfileModule>()),
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(
<PerMessageProfileEditor
mx={{} as MatrixClient}
profileId="old-id"
displayName="New Profile"
shorthands={{ prefix: [] }}
/>
);
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',
});
});
});
@@ -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<HTMLInputElement>) => {