Skip to content

Commit 73f7c22

Browse files
committed
fix: address CodeRabbit review - isolate button state via avatar pre-selection and use fireEvent
1 parent b7f4fa2 commit 73f7c22

1 file changed

Lines changed: 40 additions & 39 deletions

File tree

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { render, screen } from '@/test-utils';
2-
import userEvent from '@testing-library/user-event';
1+
import { render, screen, fireEvent } from '@/test-utils';
32
import { AvatarSelectionStep } from '@/components/OnboardingSteps/AvatarSelectionStep';
43
import AccountSettingsCard from '@/pages/SettingsPage/components/AccountSettingsCard';
54

@@ -9,88 +8,90 @@ const ERROR_MSG = 'A single word in your name cannot exceed 30 characters.';
98

109
beforeEach(() => localStorage.clear());
1110

12-
// AvatarSelectionStep
11+
// ─── AvatarSelectionStep ───────────────────────────────────────────────────
1312

1413
describe('Name validation - AvatarSelectionStep', () => {
1514
const setup = () => {
16-
const user = userEvent.setup();
1715
render(
1816
<AvatarSelectionStep
1917
stepIndex={0}
2018
totalSteps={4}
2119
currentStepDisplayIndex={0}
2220
/>,
2321
);
22+
fireEvent.click(screen.getAllByAltText('Avatar')[0]);
2423
const input = screen.getByPlaceholderText('Enter your name');
25-
return { user, input };
24+
return { input };
2625
};
2726

28-
test('30-character word is valid - no error shown', async () => {
29-
const { user, input } = setup();
30-
await user.type(input, VALID_30);
27+
test('30-character word is valid - no error shown', () => {
28+
const { input } = setup();
29+
fireEvent.change(input, { target: { value: VALID_30 } });
3130
expect(screen.queryByText(ERROR_MSG)).not.toBeInTheDocument();
31+
expect(screen.getByRole('button', { name: /next/i })).not.toBeDisabled();
3232
});
3333

34-
test('31-character word shows error and disables Next button', async () => {
35-
const { user, input } = setup();
36-
await user.type(input, INVALID_31);
34+
test('31-character word shows error and disables Next button', () => {
35+
const { input } = setup();
36+
fireEvent.change(input, { target: { value: INVALID_31 } });
3737
expect(screen.getByText(ERROR_MSG)).toBeInTheDocument();
3838
expect(screen.getByRole('button', { name: /next/i })).toBeDisabled();
3939
});
4040

41-
test('multi-space input is handled gracefully - no error', async () => {
42-
const { user, input } = setup();
43-
await user.type(input, 'John Doe');
41+
test('multi-space input is handled gracefully - no error', () => {
42+
const { input } = setup();
43+
fireEvent.change(input, { target: { value: 'John Doe' } });
4444
expect(screen.queryByText(ERROR_MSG)).not.toBeInTheDocument();
4545
});
4646

47-
test('recovery - valid input after invalid clears error', async () => {
48-
const { user, input } = setup();
49-
await user.type(input, INVALID_31);
47+
test('recovery - valid input after invalid clears error and re-enables Next', () => {
48+
const { input } = setup();
49+
fireEvent.change(input, { target: { value: INVALID_31 } });
5050
expect(screen.getByText(ERROR_MSG)).toBeInTheDocument();
51-
await user.clear(input);
52-
await user.type(input, 'John');
51+
expect(screen.getByRole('button', { name: /next/i })).toBeDisabled();
52+
fireEvent.change(input, { target: { value: 'John' } });
5353
expect(screen.queryByText(ERROR_MSG)).not.toBeInTheDocument();
54+
expect(screen.getByRole('button', { name: /next/i })).not.toBeDisabled();
5455
});
5556
});
5657

57-
// AccountSettingsCard
58+
// ─── AccountSettingsCard ───────────────────────────────────────────────────
5859

5960
describe('Name validation - AccountSettingsCard', () => {
6061
const setup = () => {
61-
const user = userEvent.setup();
6262
render(<AccountSettingsCard />);
63+
fireEvent.click(screen.getAllByAltText('Avatar')[0]);
6364
const input = screen.getByPlaceholderText('Enter your name');
64-
return { user, input };
65+
return { input };
6566
};
6667

67-
test('30 character word is valid no error shown', async () => {
68-
const { user, input } = setup();
69-
await user.type(input, VALID_30);
68+
test('30 character word is valid - no error shown', () => {
69+
const { input } = setup();
70+
fireEvent.change(input, { target: { value: VALID_30 } });
7071
expect(screen.queryByText(ERROR_MSG)).not.toBeInTheDocument();
72+
expect(screen.getByRole('button', { name: /save changes/i })).not.toBeDisabled();
7173
});
7274

73-
test('31 character word shows error and disables Save Changes button', async () => {
74-
const { user, input } = setup();
75-
await user.type(input, INVALID_31);
75+
test('31 character word shows error and disables Save Changes button', () => {
76+
const { input } = setup();
77+
fireEvent.change(input, { target: { value: INVALID_31 } });
7678
expect(screen.getByText(ERROR_MSG)).toBeInTheDocument();
77-
expect(
78-
screen.getByRole('button', { name: /save changes/i }),
79-
).toBeDisabled();
79+
expect(screen.getByRole('button', { name: /save changes/i })).toBeDisabled();
8080
});
8181

82-
test('multi-space input is handled gracefully - no error', async () => {
83-
const { user, input } = setup();
84-
await user.type(input, 'John Doe');
82+
test('multi-space input is handled gracefully - no error', () => {
83+
const { input } = setup();
84+
fireEvent.change(input, { target: { value: 'John Doe' } });
8585
expect(screen.queryByText(ERROR_MSG)).not.toBeInTheDocument();
8686
});
8787

88-
test('recovery - valid input after invalid clears error', async () => {
89-
const { user, input } = setup();
90-
await user.type(input, INVALID_31);
88+
test('recovery - valid input after invalid clears error and re-enables Save', () => {
89+
const { input } = setup();
90+
fireEvent.change(input, { target: { value: INVALID_31 } });
9191
expect(screen.getByText(ERROR_MSG)).toBeInTheDocument();
92-
await user.clear(input);
93-
await user.type(input, 'John');
92+
expect(screen.getByRole('button', { name: /save changes/i })).toBeDisabled();
93+
fireEvent.change(input, { target: { value: 'John' } });
9494
expect(screen.queryByText(ERROR_MSG)).not.toBeInTheDocument();
95+
expect(screen.getByRole('button', { name: /save changes/i })).not.toBeDisabled();
9596
});
9697
});

0 commit comments

Comments
 (0)