|
| 1 | +import { TestBed } from '@angular/core/testing'; |
| 2 | +import { page, userEvent } from 'vitest/browser'; |
| 3 | +import { AppComponent } from './app.component'; |
| 4 | + |
| 5 | +describe('AppComponent', () => { |
| 6 | + beforeEach(async () => { |
| 7 | + TestBed.createComponent(AppComponent); |
| 8 | + }); |
| 9 | + |
| 10 | + describe('When component is rendered', () => { |
| 11 | + it('Then should display headings and required fields', async () => { |
| 12 | + await expect |
| 13 | + .element(page.getByRole('heading', { name: /registration form/i })) |
| 14 | + .toBeInTheDocument(); |
| 15 | + await expect |
| 16 | + .element(page.getByRole('heading', { name: /profile/i })) |
| 17 | + .toBeInTheDocument(); |
| 18 | + await expect |
| 19 | + .element(page.getByRole('heading', { name: /contacts/i })) |
| 20 | + .toBeInTheDocument(); |
| 21 | + await expect |
| 22 | + .element(page.getByRole('heading', { name: /emails/i })) |
| 23 | + .toBeInTheDocument(); |
| 24 | + |
| 25 | + await expect |
| 26 | + .element(page.getByLabelText(/^Name\s*$/i)) |
| 27 | + .toBeInTheDocument(); |
| 28 | + await expect |
| 29 | + .element(page.getByLabelText(/^Pseudo\s*$/i)) |
| 30 | + .toBeInTheDocument(); |
| 31 | + }); |
| 32 | + |
| 33 | + it('Then should show the form as incomplete initially', async () => { |
| 34 | + await expect |
| 35 | + .element(page.getByText(/form incomplete/i)) |
| 36 | + .toBeInTheDocument(); |
| 37 | + await expect |
| 38 | + .element(page.getByRole('button', { name: /submit/i })) |
| 39 | + .toBeEnabled(); |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + describe('Given dynamic arrays', () => { |
| 44 | + it('Then should add and remove contacts and emails', async () => { |
| 45 | + await userEvent.click(page.getByRole('button', { name: /add contact/i })); |
| 46 | + await userEvent.click(page.getByRole('button', { name: /add contact/i })); |
| 47 | + await expect.element(page.getByText(/contact 2/i)).toBeInTheDocument(); |
| 48 | + |
| 49 | + await userEvent.click( |
| 50 | + page.getByRole('button', { name: /remove contact 2/i }), |
| 51 | + ); |
| 52 | + await expect |
| 53 | + .element(page.getByText(/contact 2/i)) |
| 54 | + .not.toBeInTheDocument(); |
| 55 | + |
| 56 | + await userEvent.click(page.getByRole('button', { name: /add email/i })); |
| 57 | + await userEvent.click(page.getByRole('button', { name: /add email/i })); |
| 58 | + await expect.element(page.getByText(/email 2/i)).toBeInTheDocument(); |
| 59 | + |
| 60 | + await userEvent.click( |
| 61 | + page.getByRole('button', { name: /remove email 2/i }), |
| 62 | + ); |
| 63 | + await expect.element(page.getByText(/email 2/i)).not.toBeInTheDocument(); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + describe('Given valid form data', () => { |
| 68 | + it('Then should submit and display the submitted data', async () => { |
| 69 | + await userEvent.click(page.getByRole('button', { name: /add contact/i })); |
| 70 | + await userEvent.type(page.getByLabelText(/^Name\s*$/i), 'Alex'); |
| 71 | + await userEvent.type(page.getByLabelText(/^Pseudo\s*$/i), 'Nexus'); |
| 72 | + await userEvent.type(page.getByLabelText(/^First name\s*$/i), 'Jamie'); |
| 73 | + await userEvent.type(page.getByLabelText(/^Last name\s*$/i), 'Doe'); |
| 74 | + await userEvent.type(page.getByLabelText(/^Relation\s*$/i), 'Friend'); |
| 75 | + await userEvent.type( |
| 76 | + page.getByLabelText(/^Email\s*$/i), |
| 77 | + 'jamie@example.com', |
| 78 | + ); |
| 79 | + |
| 80 | + await userEvent.click(page.getByRole('button', { name: /submit/i })); |
| 81 | + |
| 82 | + await expect |
| 83 | + .element(page.getByRole('heading', { name: /submitted data/i })) |
| 84 | + .toBeInTheDocument(); |
| 85 | + await expect |
| 86 | + .element(page.getByText(/"name": "Alex"/)) |
| 87 | + .toBeInTheDocument(); |
| 88 | + await expect.element(page.getByText(/"contacts":/)).toBeInTheDocument(); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + describe('Given invalid form data', () => { |
| 93 | + it('Then should show required errors on submit', async () => { |
| 94 | + await userEvent.click(page.getByRole('button', { name: /submit/i })); |
| 95 | + |
| 96 | + await expect |
| 97 | + .element(page.getByText(/this field is required/i)) |
| 98 | + .toBeInTheDocument(); |
| 99 | + await expect |
| 100 | + .element(page.getByText(/at least one contact is required/i)) |
| 101 | + .toBeInTheDocument(); |
| 102 | + }); |
| 103 | + |
| 104 | + it('Then should show email format error for a contact', async () => { |
| 105 | + await userEvent.click(page.getByRole('button', { name: /add contact/i })); |
| 106 | + await userEvent.type(page.getByLabelText(/^First name\s*$/i), 'Jamie'); |
| 107 | + await userEvent.type(page.getByLabelText(/^Last name\s*$/i), 'Doe'); |
| 108 | + await userEvent.type(page.getByLabelText(/^Relation\s*$/i), 'Friend'); |
| 109 | + await userEvent.type(page.getByLabelText(/^Email\s*$/i), 'invalid'); |
| 110 | + |
| 111 | + await expect |
| 112 | + .element(page.getByText(/enter a valid email/i)) |
| 113 | + .toBeInTheDocument(); |
| 114 | + }); |
| 115 | + }); |
| 116 | +}); |
0 commit comments