mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-24 02:51:17 -08:00
fix persona picker tests
This commit is contained in:
2 files changed
+26
-17
No files matched your search
@@ -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<PerMessageProfileMsc4461[]>>(),
|
||||
@@ -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(
|
||||
<PersonaPicker
|
||||
mx={secondClient}
|
||||
roomId="!room:example.org"
|
||||
suppressEditorRefocus={vi.fn<() => 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 () => {
|
||||
|
||||
@@ -76,7 +76,7 @@ type TemporaryPersonaPickerProps = {
|
||||
anchor?: RectCords;
|
||||
};
|
||||
|
||||
function useProfiles(
|
||||
export function useProfiles(
|
||||
mx: MatrixClient,
|
||||
mountedRef: MutableRefObject<boolean>,
|
||||
profileFetchGenerationRef: MutableRefObject<number>
|
||||
|
||||
Reference in new issue
Block a user