|
| 1 | +import VueRouter from 'vue-router'; |
| 2 | +import { render, screen, fireEvent } from '@testing-library/vue'; |
| 3 | +import StudioEmailField from '../StudioEmailField.vue'; |
| 4 | + |
| 5 | +const renderComponent = (props = {}) => |
| 6 | + render(StudioEmailField, { |
| 7 | + router: new VueRouter(), |
| 8 | + props: { |
| 9 | + value: '', |
| 10 | + ...props, |
| 11 | + }, |
| 12 | + }); |
| 13 | + |
| 14 | +describe('StudioEmailField', () => { |
| 15 | + describe('rendering', () => { |
| 16 | + it('renders with the default "Email address" label', () => { |
| 17 | + renderComponent(); |
| 18 | + expect(screen.getByLabelText(/email address/i)).toBeInTheDocument(); |
| 19 | + }); |
| 20 | + |
| 21 | + it('renders with a custom label when provided', () => { |
| 22 | + renderComponent({ label: 'Work email' }); |
| 23 | + expect(screen.getByLabelText(/work email/i)).toBeInTheDocument(); |
| 24 | + }); |
| 25 | + |
| 26 | + it('is disabled when the disabled prop is true', () => { |
| 27 | + renderComponent({ disabled: true }); |
| 28 | + expect(screen.getByLabelText(/email address/i)).toBeDisabled(); |
| 29 | + }); |
| 30 | + }); |
| 31 | + |
| 32 | + describe('input handling', () => { |
| 33 | + it('emits trimmed value — strips leading and trailing whitespace', async () => { |
| 34 | + const { emitted } = renderComponent(); |
| 35 | + const input = screen.getByLabelText(/email address/i); |
| 36 | + await fireEvent.update(input, ' test@example.com '); |
| 37 | + expect(emitted().input).toBeTruthy(); |
| 38 | + expect(emitted().input[0][0]).toBe('test@example.com'); |
| 39 | + }); |
| 40 | + |
| 41 | + it('emits blur event when the field loses focus', async () => { |
| 42 | + const { emitted } = renderComponent(); |
| 43 | + const input = screen.getByLabelText(/email address/i); |
| 44 | + await fireEvent.blur(input); |
| 45 | + expect(emitted().blur).toBeTruthy(); |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + describe('error display', () => { |
| 50 | + it('shows the first error message when errorMessages is non-empty', () => { |
| 51 | + renderComponent({ errorMessages: ['Please enter a valid email address'] }); |
| 52 | + expect(screen.getByText('Please enter a valid email address')).toBeVisible(); |
| 53 | + }); |
| 54 | + |
| 55 | + it('shows no error text when errorMessages is empty', () => { |
| 56 | + renderComponent({ errorMessages: [] }); |
| 57 | + expect(screen.queryByText('Please enter a valid email address')).not.toBeInTheDocument(); |
| 58 | + }); |
| 59 | + |
| 60 | + it('shows only the first error when multiple messages are provided', () => { |
| 61 | + renderComponent({ errorMessages: ['First error', 'Second error'] }); |
| 62 | + expect(screen.getByText('First error')).toBeVisible(); |
| 63 | + expect(screen.queryByText('Second error')).not.toBeInTheDocument(); |
| 64 | + }); |
| 65 | + }); |
| 66 | +}); |
0 commit comments