|
1 | 1 | import { fireEvent, render, screen, waitFor } from '@testing-library/react'; |
2 | 2 | import { userEvent } from '@testing-library/user-event'; |
3 | 3 | import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 4 | +import type { Mock } from 'vitest'; |
4 | 5 | import { z } from 'zod'; |
5 | 6 |
|
6 | 7 | import { Form } from './Form'; |
@@ -114,4 +115,68 @@ describe('Form', () => { |
114 | 115 | expect(onSubmit.mock.lastCall?.[0].b).toBeUndefined(); |
115 | 116 | }); |
116 | 117 | }); |
| 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 | + }); |
117 | 182 | }); |
0 commit comments