|
| 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 | + let fixture: ReturnType<typeof TestBed.createComponent<AppComponent>>; |
| 7 | + |
| 8 | + beforeEach(async () => { |
| 9 | + fixture = TestBed.createComponent(AppComponent); |
| 10 | + fixture.detectChanges(); |
| 11 | + }); |
| 12 | + |
| 13 | + describe('When component is rendered', () => { |
| 14 | + it('Then should display headings and all form fields', async () => { |
| 15 | + await expect |
| 16 | + .element(page.getByRole('heading', { name: /order/i })) |
| 17 | + .toBeInTheDocument(); |
| 18 | + await expect |
| 19 | + .element(page.getByRole('heading', { name: /information/i })) |
| 20 | + .toBeInTheDocument(); |
| 21 | + await expect |
| 22 | + .element(page.getByRole('heading', { name: /shipping address/i })) |
| 23 | + .toBeInTheDocument(); |
| 24 | + await expect |
| 25 | + .element(page.getByRole('heading', { name: /billing address/i })) |
| 26 | + .toBeInTheDocument(); |
| 27 | + |
| 28 | + await expect |
| 29 | + .element(page.getByLabelText('Last name')) |
| 30 | + .toBeInTheDocument(); |
| 31 | + await expect |
| 32 | + .element(page.getByLabelText('First name')) |
| 33 | + .toBeInTheDocument(); |
| 34 | + await expect.element(page.getByLabelText('Street')).toBeInTheDocument(); |
| 35 | + await expect.element(page.getByLabelText('ZIP code')).toBeInTheDocument(); |
| 36 | + await expect.element(page.getByLabelText('City')).toBeInTheDocument(); |
| 37 | + await expect |
| 38 | + .element(page.getByLabelText(/Billing address same as shipping/i)) |
| 39 | + .toBeInTheDocument(); |
| 40 | + await expect |
| 41 | + .element(page.getByTestId('billing-fields')) |
| 42 | + .toBeInTheDocument(); |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + describe('Given billing toggle', () => { |
| 47 | + it('Then billing fields hide when same-as-shipping is checked', async () => { |
| 48 | + await expect |
| 49 | + .element(page.getByTestId('billing-fields')) |
| 50 | + .toBeInTheDocument(); |
| 51 | + |
| 52 | + await userEvent.click( |
| 53 | + page.getByLabelText(/Billing address same as shipping/i), |
| 54 | + ); |
| 55 | + fixture.detectChanges(); |
| 56 | + |
| 57 | + expect( |
| 58 | + fixture.nativeElement.querySelector('[data-testid="billing-fields"]'), |
| 59 | + ).toBeNull(); |
| 60 | + expect(fixture.componentInstance.billing.disabled).toBe(true); |
| 61 | + }); |
| 62 | + |
| 63 | + it('Then shipping values are copied into billing when locked', async () => { |
| 64 | + await userEvent.type(page.getByLabelText('Street'), '12 Flower Street'); |
| 65 | + await userEvent.type(page.getByLabelText('ZIP code'), '75001'); |
| 66 | + await userEvent.type(page.getByLabelText('City'), 'Paris'); |
| 67 | + |
| 68 | + await userEvent.click( |
| 69 | + page.getByLabelText(/Billing address same as shipping/i), |
| 70 | + ); |
| 71 | + fixture.detectChanges(); |
| 72 | + |
| 73 | + expect(fixture.componentInstance.billing.getRawValue()).toEqual( |
| 74 | + fixture.componentInstance.shipping.getRawValue(), |
| 75 | + ); |
| 76 | + }); |
| 77 | + |
| 78 | + it('Then billing fields become editable again when unchecked', async () => { |
| 79 | + const checkbox = page.getByLabelText(/Billing address same as shipping/i); |
| 80 | + await userEvent.click(checkbox); |
| 81 | + await userEvent.click(checkbox); |
| 82 | + fixture.detectChanges(); |
| 83 | + |
| 84 | + await expect |
| 85 | + .element(page.getByTestId('billing-fields')) |
| 86 | + .toBeInTheDocument(); |
| 87 | + expect(fixture.componentInstance.billing.enabled).toBe(true); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + describe('Given validation', () => { |
| 92 | + it('Then marks all fields as required after submitting an empty form', async () => { |
| 93 | + await userEvent.click(page.getByRole('button', { name: /submit/i })); |
| 94 | + fixture.detectChanges(); |
| 95 | + |
| 96 | + const requiredErrors = Array.from<HTMLElement>( |
| 97 | + fixture.nativeElement.querySelectorAll( |
| 98 | + '.hint', |
| 99 | + ) as NodeListOf<HTMLElement>, |
| 100 | + ).filter((el) => el.textContent?.includes('This field is required')); |
| 101 | + expect(requiredErrors.length).toBe(8); |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + describe('Given a valid form with billing locked to shipping', () => { |
| 106 | + it('Then form is valid and status displays ready', async () => { |
| 107 | + await userEvent.type(page.getByLabelText('Last name'), 'Doe'); |
| 108 | + await userEvent.type(page.getByLabelText('First name'), 'John'); |
| 109 | + await userEvent.type(page.getByLabelText('Street'), '12 Flower Street'); |
| 110 | + await userEvent.type(page.getByLabelText('ZIP code'), '75001'); |
| 111 | + await userEvent.type(page.getByLabelText('City'), 'Paris'); |
| 112 | + |
| 113 | + await userEvent.click( |
| 114 | + page.getByLabelText(/Billing address same as shipping/i), |
| 115 | + ); |
| 116 | + fixture.detectChanges(); |
| 117 | + |
| 118 | + expect(fixture.componentInstance.form.valid).toBe(true); |
| 119 | + await expect |
| 120 | + .element(page.getByText(/Ready to submit/i)) |
| 121 | + .toBeInTheDocument(); |
| 122 | + }); |
| 123 | + }); |
| 124 | +}); |
0 commit comments