Skip to content

Commit 5e1c044

Browse files
committed
test: add test for new form feature
1 parent e311485 commit 5e1c044

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

src/components/Form/ErrorMessage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const ErrorMessage: React.FC<{ className?: string; error?: null | string[
1010
{error.map((message) => (
1111
<div className={cn('text-destructive flex w-full items-center text-sm font-medium', className)} key={message}>
1212
<CircleAlertIcon className="mr-1" style={{ strokeWidth: '2px' }} />
13-
<span>{message}</span>
13+
<span data-testid="error-message-text">{message}</span>
1414
</div>
1515
)) ?? null}
1616
</div>

src/components/Form/Form.test.tsx

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
22
import { userEvent } from '@testing-library/user-event';
33
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
4+
import type { Mock } from 'vitest';
45
import { z } from 'zod';
56

67
import { Form } from './Form';
@@ -114,4 +115,68 @@ describe('Form', () => {
114115
expect(onSubmit.mock.lastCall?.[0].b).toBeUndefined();
115116
});
116117
});
118+
119+
describe('custom beforeSubmit error', () => {
120+
let beforeSubmit: Mock;
121+
122+
beforeEach(() => {
123+
beforeSubmit = vi.fn();
124+
render(
125+
<Form
126+
beforeSubmit={beforeSubmit}
127+
content={{
128+
value: {
129+
kind: 'number',
130+
label: 'Value',
131+
variant: 'input'
132+
}
133+
}}
134+
data-testid={testid}
135+
validationSchema={z.object({
136+
value: z.number({ message: 'Please enter a number' })
137+
})}
138+
onError={onError}
139+
onSubmit={onSubmit}
140+
/>
141+
);
142+
});
143+
144+
afterEach(() => {
145+
vi.clearAllMocks();
146+
});
147+
148+
it('should render', () => {
149+
expect(screen.getByTestId(testid)).toBeInTheDocument();
150+
});
151+
152+
it('should not allow submitting the form with a zod error', async () => {
153+
fireEvent.submit(screen.getByTestId(testid));
154+
await waitFor(() =>
155+
expect(screen.getAllByTestId('error-message-text').map((e) => e.innerHTML)).toMatchObject([
156+
'Please enter a number'
157+
])
158+
);
159+
expect(beforeSubmit).not.toHaveBeenCalled();
160+
expect(onSubmit).not.toHaveBeenCalled();
161+
});
162+
163+
it('should not allow submitting the form with the beforeSubmit error', async () => {
164+
beforeSubmit.mockResolvedValueOnce({ errorMessage: 'Invalid!', success: false });
165+
const field: HTMLInputElement = screen.getByLabelText('Value');
166+
await userEvent.type(field, '-1');
167+
fireEvent.submit(screen.getByTestId(testid));
168+
await waitFor(() =>
169+
expect(screen.getAllByTestId('error-message-text').map((e) => e.innerHTML)).toMatchObject(['Invalid!'])
170+
);
171+
expect(onSubmit).not.toHaveBeenCalled();
172+
});
173+
174+
it('should allow submitting the form if beforeSubmit returns true', async () => {
175+
beforeSubmit.mockResolvedValueOnce({ success: true });
176+
const field: HTMLInputElement = screen.getByLabelText('Value');
177+
await userEvent.type(field, '-1');
178+
fireEvent.submit(screen.getByTestId(testid));
179+
await waitFor(() => expect(onSubmit).toHaveBeenCalledOnce());
180+
});
181+
});
117182
});

0 commit comments

Comments
 (0)