- Notifications
You must be signed in to change notification settings - Fork 92
test: enhance example to verify pre-set form values on a Material sel… #276
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -3,6 +3,9 @@ import userEvent from '@testing-library/user-event'; | |
| | ||
| import { MaterialModule } from '../material.module'; | ||
| import { MaterialFormsComponent } from './04-forms-with-material'; | ||
| import { FormBuilder, Validators } from "@angular/forms"; | ||
| import { By } from "@angular/platform-browser"; | ||
| | ||
| | ||
| test('is possible to fill in a form and verify error messages (with the help of jest-dom https://testing-library.com/docs/ecosystem-jest-dom)', async () => { | ||
| const { fixture } = await render(MaterialFormsComponent, { | ||
| | @@ -37,12 +40,46 @@ test('is possible to fill in a form and verify error messages (with the help of | |
| | ||
| expect(nameControl).toHaveValue('Tim'); | ||
| expect(scoreControl).toHaveValue(7); | ||
| expect(colorControl).toHaveTextContent('Green'); | ||
| | ||
| const form = screen.getByRole('form'); | ||
| expect(form).toHaveFormValues({ | ||
| name: 'Tim', | ||
| score: 7, | ||
| }); | ||
| | ||
| expect((fixture.componentInstance as MaterialFormsComponent).form?.get('color')?.value).toBe('G'); | ||
| }); | ||
| | ||
| test('is should show pre-set form values', async () => { | ||
| const formBuilder = new FormBuilder(); | ||
| | ||
| const { fixture, detectChanges } = await render(MaterialFormsComponent, { | ||
| imports: [MaterialModule], | ||
| componentProperties: { | ||
| ||
| form: formBuilder.group({ | ||
| name: ['Max', Validators.required], | ||
| score: [4, [Validators.min(1), Validators.max(10)]], | ||
| color: ['B', Validators.required], | ||
| }), | ||
| }, | ||
| }); | ||
| | ||
| const nameControl = screen.getByLabelText(/name/i); | ||
| const scoreControl = screen.getByRole('spinbutton', { name: /score/i }); | ||
| const colorControl = screen.getByRole('combobox', { name: /color/i }); | ||
| | ||
| expect(nameControl).toHaveValue('Max'); | ||
| expect(scoreControl).toHaveValue(4); | ||
| | ||
| fixture.debugElement.query(By.css('.mat-select-trigger')).nativeElement.click(); | ||
| detectChanges(); | ||
| expect(colorControl).toHaveTextContent('Blue'); | ||
| | ||
| const form = screen.getByRole('form'); | ||
| expect(form).toHaveFormValues({ | ||
| name: 'Max', | ||
| score: 4, | ||
| }); | ||
| | ||
| expect((fixture.componentInstance as MaterialFormsComponent).form?.get('color')?.value).toBe('B'); | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.