Skip to content

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

Merged
merged 3 commits into from
Jan 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion apps/example-app/src/app/examples/04-forms-with-material.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import userEvent from '@testing-library/user-event';
import { MaterialModule } from '../material.module';
import { MaterialFormsComponent } from './04-forms-with-material';


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, {
imports: [MaterialModule],
Expand Down Expand Up @@ -37,12 +38,40 @@ 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('set and show pre-set form values', async () => {
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');

const form = screen.getByRole('form');
expect(form).toHaveFormValues({
name: 'Max',
score: 4,
});
expect((fixture.componentInstance as MaterialFormsComponent).form?.get('color')?.value).toBe('B');
});
10 changes: 9 additions & 1 deletion apps/example-app/src/app/examples/04-forms-with-material.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import { FormBuilder, Validators } from '@angular/forms';

<mat-form-field>
<mat-select placeholder="Color" name="color" formControlName="color">
<mat-select-trigger>
{{ colorControlDisplayValue }}
</mat-select-trigger>
<mat-option value="">---</mat-option>
<mat-option *ngFor="let color of colors" [value]="color.id">{{ color.value }}</mat-option>
</mat-select>
Expand Down Expand Up @@ -60,11 +63,16 @@ export class MaterialFormsComponent {
form = this.formBuilder.group({
name: ['', Validators.required],
score: [0, [Validators.min(1), Validators.max(10)]],
color: ['', Validators.required],
color: [null, Validators.required],
});

constructor(private formBuilder: FormBuilder) {}

get colorControlDisplayValue(): string | undefined {
const selectedId = this.form.get('color')?.value;
return this.colors.filter(color => color.id === selectedId)[0]?.value;
}

get formErrors() {
return Object.keys(this.form.controls)
.map((formKey) => {
Expand Down