|
1 | 1 | import { TestBed } from '@angular/core/testing'; |
| 2 | +import { Router } from '@angular/router'; |
2 | 3 | import { page } from 'vitest/browser'; |
3 | 4 | import { AppComponent } from './app.component'; |
| 5 | +import { appConfig } from './app.config'; |
4 | 6 |
|
5 | 7 | describe('AppComponent', () => { |
6 | 8 | beforeEach(async () => { |
| 9 | + TestBed.configureTestingModule({ |
| 10 | + providers: appConfig.providers, |
| 11 | + }); |
| 12 | + const router = TestBed.inject(Router); |
| 13 | + router.initialNavigation(); |
7 | 14 | TestBed.createComponent(AppComponent); |
8 | 15 | }); |
9 | 16 |
|
10 | | - test('...', async () => { |
11 | | - const heading = page.getByRole('heading', { |
12 | | - name: /registration form/i, |
| 17 | + describe('When component is rendered', () => { |
| 18 | + it('Then should display the portal title', async () => { |
| 19 | + const heading = page.getByRole('heading', { |
| 20 | + name: /user management portal/i, |
| 21 | + }); |
| 22 | + await expect.element(heading).toBeInTheDocument(); |
| 23 | + }); |
| 24 | + |
| 25 | + it('Then should display correct information in the user list', async () => { |
| 26 | + await expect |
| 27 | + .element(page.getByText('Max Mustermann')) |
| 28 | + .toBeInTheDocument(); |
| 29 | + await expect.element(page.getByText('John Doe')).toBeInTheDocument(); |
| 30 | + await expect.element(page.getByText('Jane Smith')).toBeInTheDocument(); |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + describe('Given a user wants to add a new user', () => { |
| 35 | + it('Then should navigate to add form and create user', async () => { |
| 36 | + const addButton = page.getByRole('button', { name: /add user/i }).first(); |
| 37 | + await addButton.click(); |
| 38 | + |
| 39 | + await expect |
| 40 | + .element(page.getByRole('heading', { name: /add new user/i })) |
| 41 | + .toBeInTheDocument(); |
| 42 | + |
| 43 | + await page.getByLabelText(/firstname/i).fill('Antigravity'); |
| 44 | + await page.getByLabelText(/lastname/i).fill('AI'); |
| 45 | + await page.getByLabelText(/age/i).fill('1'); |
| 46 | + await page.getByLabelText(/grade/i).fill('10'); |
| 47 | + |
| 48 | + await page.getByRole('button', { name: /add/i }).click(); |
| 49 | + |
| 50 | + await expect |
| 51 | + .element(page.getByText('Antigravity AI')) |
| 52 | + .toBeInTheDocument(); |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + describe('Given a user wants to edit an existing user', () => { |
| 57 | + it('Then should update the user successfully', async () => { |
| 58 | + await expect.element(page.getByText('Jane Smith')).toBeInTheDocument(); |
| 59 | + |
| 60 | + const editButtons = await page |
| 61 | + .getByRole('button', { name: /edit/i }) |
| 62 | + .all(); |
| 63 | + // Jane Smith is the 3rd user in list (id 3) |
| 64 | + await editButtons[2].click(); |
| 65 | + |
| 66 | + await expect |
| 67 | + .element(page.getByRole('heading', { name: /edit user/i })) |
| 68 | + .toBeInTheDocument(); |
| 69 | + await expect |
| 70 | + .element(page.getByLabelText(/firstname/i)) |
| 71 | + .toHaveValue('Jane'); |
| 72 | + |
| 73 | + await page.getByLabelText(/firstname/i).fill('Janet'); |
| 74 | + await page.getByRole('button', { name: /update/i }).click(); |
| 75 | + |
| 76 | + await expect.element(page.getByText('Janet Smith')).toBeInTheDocument(); |
| 77 | + await expect |
| 78 | + .element(page.getByText('Jane Smith')) |
| 79 | + .not.toBeInTheDocument(); |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + describe('Given a user wants to delete a user', () => { |
| 84 | + it('Then should remove the user from the list', async () => { |
| 85 | + await expect.element(page.getByText('John Doe')).toBeInTheDocument(); |
| 86 | + |
| 87 | + const deleteButtons = await page |
| 88 | + .getByRole('button', { name: /delete/i }) |
| 89 | + .all(); |
| 90 | + // John Doe is the 2nd user in list |
| 91 | + await deleteButtons[1].click(); |
| 92 | + |
| 93 | + await expect.element(page.getByText('John Doe')).not.toBeInTheDocument(); |
13 | 94 | }); |
14 | | - await expect.element(heading).toBeInTheDocument(); |
15 | 95 | }); |
16 | 96 | }); |
0 commit comments