Skip to content

Commit 20ae40d

Browse files
author
mleimer
authored
docs: add example that presets form values (#276)
1 parent c9af6ef commit 20ae40d

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

apps/example-app/src/app/examples/04-forms-with-material.spec.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import userEvent from '@testing-library/user-event';
44
import { MaterialModule } from '../material.module';
55
import { MaterialFormsComponent } from './04-forms-with-material';
66

7+
78
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 () => {
89
const { fixture } = await render(MaterialFormsComponent, {
910
imports: [MaterialModule],
@@ -37,12 +38,40 @@ test('is possible to fill in a form and verify error messages (with the help of
3738

3839
expect(nameControl).toHaveValue('Tim');
3940
expect(scoreControl).toHaveValue(7);
41+
expect(colorControl).toHaveTextContent('Green');
4042

4143
const form = screen.getByRole('form');
4244
expect(form).toHaveFormValues({
4345
name: 'Tim',
4446
score: 7,
4547
});
46-
4748
expect((fixture.componentInstance as MaterialFormsComponent).form?.get('color')?.value).toBe('G');
4849
});
50+
51+
test('set and show pre-set form values', async () => {
52+
const { fixture, detectChanges } = await render(MaterialFormsComponent, {
53+
imports: [MaterialModule],
54+
});
55+
56+
fixture.componentInstance.form.setValue({
57+
name: 'Max',
58+
score: 4,
59+
color: 'B'
60+
})
61+
detectChanges();
62+
63+
const nameControl = screen.getByLabelText(/name/i);
64+
const scoreControl = screen.getByRole('spinbutton', { name: /score/i });
65+
const colorControl = screen.getByRole('combobox', { name: /color/i });
66+
67+
expect(nameControl).toHaveValue('Max');
68+
expect(scoreControl).toHaveValue(4);
69+
expect(colorControl).toHaveTextContent('Blue');
70+
71+
const form = screen.getByRole('form');
72+
expect(form).toHaveFormValues({
73+
name: 'Max',
74+
score: 4,
75+
});
76+
expect((fixture.componentInstance as MaterialFormsComponent).form?.get('color')?.value).toBe('B');
77+
});

apps/example-app/src/app/examples/04-forms-with-material.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ import { FormBuilder, Validators } from '@angular/forms';
2424
2525
<mat-form-field>
2626
<mat-select placeholder="Color" name="color" formControlName="color">
27+
<mat-select-trigger>
28+
{{ colorControlDisplayValue }}
29+
</mat-select-trigger>
2730
<mat-option value="">---</mat-option>
2831
<mat-option *ngFor="let color of colors" [value]="color.id">{{ color.value }}</mat-option>
2932
</mat-select>
@@ -60,11 +63,16 @@ export class MaterialFormsComponent {
6063
form = this.formBuilder.group({
6164
name: ['', Validators.required],
6265
score: [0, [Validators.min(1), Validators.max(10)]],
63-
color: ['', Validators.required],
66+
color: [null, Validators.required],
6467
});
6568

6669
constructor(private formBuilder: FormBuilder) {}
6770

71+
get colorControlDisplayValue(): string | undefined {
72+
const selectedId = this.form.get('color')?.value;
73+
return this.colors.filter(color => color.id === selectedId)[0]?.value;
74+
}
75+
6876
get formErrors() {
6977
return Object.keys(this.form.controls)
7078
.map((formKey) => {

0 commit comments

Comments
 (0)