Skip to content

Commit e95649d

Browse files
refactor(mosaic): simplify password loading
1 parent 7e2a51b commit e95649d

2 files changed

Lines changed: 7 additions & 18 deletions

File tree

‎packages/mosaic/src/features/user-profile/__tests__/user-profile-password-loading.test.tsx‎

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import type * as SharedReact from '@clerk/shared/react';
22
import { ClerkInstanceContext } from '@clerk/shared/react';
33
import type { LoadedClerk } from '@clerk/shared/types';
44
import { render, screen } from '@testing-library/react';
5-
import userEvent from '@testing-library/user-event';
65
import { expect, it, vi } from 'vitest';
76

87
import { MosaicProvider } from '../../../MosaicProvider';
@@ -50,24 +49,19 @@ function tree() {
5049
return (
5150
<ClerkInstanceContext.Provider value={{ value: clerk as unknown as LoadedClerk }}>
5251
<MosaicProvider>
53-
<UserProfilePasswordSection />
52+
<UserProfilePasswordSection fallback={<div>Loading password section</div>} />
5453
</MosaicProvider>
5554
</ClerkInstanceContext.Provider>
5655
);
5756
}
5857

59-
it('keeps the draft while session data briefly reloads', async () => {
58+
it('shows the fallback when session data starts loading after the section is ready', () => {
6059
isSessionLoaded = true;
6160
const { rerender } = render(tree());
62-
const events = userEvent.setup();
63-
await events.click(screen.getByRole('button', { name: 'Change password' }));
64-
await events.type(screen.getByLabelText('New password'), 'new-password-123');
61+
expect(screen.getByRole('button', { name: 'Change password' })).toBeInTheDocument();
6562

6663
isSessionLoaded = false;
6764
rerender(tree());
68-
expect(screen.getByLabelText('New password')).toHaveValue('new-password-123');
69-
70-
isSessionLoaded = true;
71-
rerender(tree());
72-
expect(screen.getByLabelText('New password')).toHaveValue('new-password-123');
65+
expect(screen.getByText('Loading password section')).toBeInTheDocument();
66+
expect(screen.queryByRole('button', { name: 'Change password' })).not.toBeInTheDocument();
7367
});

‎packages/mosaic/src/features/user-profile/user-profile-password-section/user-profile-password-section.tsx‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { ReactNode } from 'react';
2-
import { useCallback, useRef } from 'react';
2+
import { useCallback } from 'react';
33

44
import { Button } from '../../../components/button';
55
import type { FieldFeedback } from '../../../components/form';
@@ -20,12 +20,7 @@ export interface UserProfilePasswordSectionProps {
2020
}
2121

2222
export function UserProfilePasswordSection({ fallback = null }: UserProfilePasswordSectionProps) {
23-
const currentModel = useUserProfilePasswordModel();
24-
const settledModel = useRef<UserProfilePasswordModel>(currentModel);
25-
if (currentModel.status !== 'loading') {
26-
settledModel.current = currentModel;
27-
}
28-
const model = currentModel.status === 'loading' ? settledModel.current : currentModel;
23+
const model = useUserProfilePasswordModel();
2924
const m = useMessages('userProfilePasswordSection');
3025
if (model.status === 'loading') {
3126
return fallback;

0 commit comments

Comments
 (0)