-
Notifications
You must be signed in to change notification settings - Fork 91
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: { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for enhancing this example. It's also sufficient to invoke a change detection cycle (we don't need to click on the trigger). Thoughts? const { fixture, detectChanges } = await render(MaterialFormsComponent, {
imports: [MaterialModule],
});
fixture.componentInstance.form.setValue({
name: 'Max',
score: 4,
color: 'B'
})
detectChanges();
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);
expect(colorControl).toHaveTextContent('Blue'); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @timdeschryver yes, that is indeed a very good point. I adapted it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While this works, I'm thinking if we can do something about this to provide a better experience. const { fixture } = render(Component);
// this would be new and invokes a CD cycle
invoke(() => {
fixture.componentInstance.form.setValue();
}) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @timdeschryver If we had to update the form values from within the test, this might be an option. However, in a scenario where one uses custom written Angular components for different form fields, there is most likely a Maybe we should consider adding a test scenario where one passes a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Those examples are based on "real-world" scenarios. For now, I think this example shows a user how to do it the best way. |
||
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.