From f45fb80c74d39eec64f078f7f554a6f8ff8bd5df Mon Sep 17 00:00:00 2001 From: Erwan Leboucher Date: Sun, 9 Aug 2026 20:10:33 +0200 Subject: [PATCH] fix persona picker tests --- .../persona-picker/PersonaPicker.test.tsx | 41 +++++++++++-------- .../room/persona-picker/PersonaPicker.tsx | 2 +- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/src/app/features/room/persona-picker/PersonaPicker.test.tsx b/src/app/features/room/persona-picker/PersonaPicker.test.tsx index efce4bff2..298d834c3 100644 --- a/src/app/features/room/persona-picker/PersonaPicker.test.tsx +++ b/src/app/features/room/persona-picker/PersonaPicker.test.tsx @@ -1,9 +1,9 @@ -import { fireEvent, render, screen, waitFor } from '@testing-library/react'; +import { act, fireEvent, render, renderHook, screen, waitFor } from '@testing-library/react'; import type { ReactNode } from 'react'; import { beforeEach, describe, expect, it, vi } from 'vitest'; import type { MatrixClient } from 'matrix-js-sdk'; import type { PerMessageProfileMsc4461 } from '$app/persona'; -import { PersonaPicker, PersonaPickerTab } from './PersonaPicker'; +import { PersonaPicker, PersonaPickerTab, useProfiles } from './PersonaPicker'; const mocked = vi.hoisted(() => ({ getAll: vi.fn<(mx: MatrixClient) => Promise>(), @@ -49,6 +49,12 @@ vi.mock('$app/persona/catalog', () => ({ }, })); vi.mock('$hooks/useMediaAuthentication.ts', () => ({ useMediaAuthentication: () => false })); +vi.mock('$hooks/usePerMessageProfile.ts', () => ({ + usePersonaCosmetics: () => ({ + avatarUrl: () => undefined, + nameColor: () => undefined, + }), +})); // useActiveTheme reaches for window.matchMedia, which jsdom does not provide. vi.mock('$hooks/useTheme.ts', () => ({ ThemeKind: { Light: 'light', Dark: 'dark' }, @@ -161,22 +167,25 @@ describe('PersonaPicker async flows', () => { client === firstClient ? firstFetch.promise : secondFetch.promise ); - const view = renderPicker(firstClient); - view.rerender( - void>()} - onTabChange={vi.fn<(tab: PersonaPickerTab) => void>()} - latchedPersona={undefined} - /> - ); + const mountedRef = { current: true }; + const generationRef = { current: 0 }; + const view = renderHook(({ mx }) => useProfiles(mx, mountedRef, generationRef), { + initialProps: { mx: firstClient }, + }); + await waitFor(() => expect(mocked.getAll).toHaveBeenCalledWith(firstClient)); + view.rerender({ mx: secondClient }); - secondFetch.resolve([{ id: 'new', displayname: 'New', trigger: { prefix: [] } }]); - expect(await screen.findByText('New')).toBeInTheDocument(); - firstFetch.resolve([{ id: 'old', displayname: 'Old', trigger: { prefix: [] } }]); + await act(async () => { + secondFetch.resolve([{ id: 'new', displayname: 'New', trigger: { prefix: [] } }]); + await secondFetch.promise; + }); + await waitFor(() => expect(view.result.current.profiles?.[0]?.id).toBe('new')); + await act(async () => { + firstFetch.resolve([{ id: 'old', displayname: 'Old', trigger: { prefix: [] } }]); + await firstFetch.promise; + }); - await waitFor(() => expect(screen.queryByText('Old')).not.toBeInTheDocument()); + expect(view.result.current.profiles?.[0]?.id).toBe('new'); }); it('does not update state when an in-flight profile fetch resolves after unmount', async () => { diff --git a/src/app/features/room/persona-picker/PersonaPicker.tsx b/src/app/features/room/persona-picker/PersonaPicker.tsx index 4807af0f0..ac29e77eb 100644 --- a/src/app/features/room/persona-picker/PersonaPicker.tsx +++ b/src/app/features/room/persona-picker/PersonaPicker.tsx @@ -76,7 +76,7 @@ type TemporaryPersonaPickerProps = { anchor?: RectCords; }; -function useProfiles( +export function useProfiles( mx: MatrixClient, mountedRef: MutableRefObject, profileFetchGenerationRef: MutableRefObject