From 52a25864de96c24d908abd1462adc1169c76fb6b Mon Sep 17 00:00:00 2001 From: Tim Deschryver <28659384+timdeschryver@users.noreply.github.com> Date: Sat, 28 Jan 2023 16:03:49 +0100 Subject: [PATCH] add checkbox example (#363) --- .../app/examples/04-forms-with-material.spec.ts | 17 +++++++++++++++++ .../src/app/examples/04-forms-with-material.ts | 3 +++ apps/example-app/src/app/material.module.ts | 3 ++- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/apps/example-app/src/app/examples/04-forms-with-material.spec.ts b/apps/example-app/src/app/examples/04-forms-with-material.spec.ts index 64ace4a..e1e16e3 100644 --- a/apps/example-app/src/app/examples/04-forms-with-material.spec.ts +++ b/apps/example-app/src/app/examples/04-forms-with-material.spec.ts @@ -13,12 +13,14 @@ test('is possible to fill in a form and verify error messages (with the help of const scoreControl = screen.getByRole('spinbutton', { name: /score/i }); const colorControl = screen.getByPlaceholderText(/color/i); const dateControl = screen.getByRole('textbox', { name: /Choose a date/i }); + const checkboxControl = screen.getByRole('checkbox', { name: /agree/i }); const errors = screen.getByRole('alert'); expect(errors).toContainElement(screen.queryByText('name is required')); expect(errors).toContainElement(screen.queryByText('score must be greater than 1')); expect(errors).toContainElement(screen.queryByText('color is required')); + expect(errors).toContainElement(screen.queryByText('agree is required')); userEvent.type(nameControl, 'Tim'); userEvent.clear(scoreControl); @@ -26,9 +28,15 @@ test('is possible to fill in a form and verify error messages (with the help of userEvent.click(colorControl); userEvent.click(screen.getByText(/green/i)); + expect(checkboxControl).not.toBeChecked(); + userEvent.click(checkboxControl); + expect(checkboxControl).toBeChecked(); + expect(checkboxControl).toBeValid(); + expect(screen.queryByText('name is required')).not.toBeInTheDocument(); expect(screen.getByText('score must be lesser than 10')).toBeInTheDocument(); expect(screen.queryByText('color is required')).not.toBeInTheDocument(); + expect(screen.queryByText('agree is required')).not.toBeInTheDocument(); expect(scoreControl).toBeInvalid(); userEvent.clear(scoreControl); @@ -42,6 +50,7 @@ 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'); + expect(checkboxControl).toBeChecked(); const form = screen.getByRole('form'); expect(form).toHaveFormValues({ @@ -50,6 +59,7 @@ test('is possible to fill in a form and verify error messages (with the help of }); // material doesn't add these to the form + expect((fixture.componentInstance as MaterialFormsComponent).form?.get('agree')?.value).toBe(true); expect((fixture.componentInstance as MaterialFormsComponent).form?.get('color')?.value).toBe('G'); expect((fixture.componentInstance as MaterialFormsComponent).form?.get('date')?.value).toEqual(new Date(2022, 7, 11)); }); @@ -64,22 +74,29 @@ test('set and show pre-set form values', async () => { score: 4, color: 'B', date: new Date(2022, 7, 11), + agree: true, }); detectChanges(); const nameControl = screen.getByLabelText(/name/i); const scoreControl = screen.getByRole('spinbutton', { name: /score/i }); const colorControl = screen.getByPlaceholderText(/color/i); + const checkboxControl = screen.getByRole('checkbox', { name: /agree/i }); expect(nameControl).toHaveValue('Max'); expect(scoreControl).toHaveValue(4); expect(colorControl).toHaveTextContent('Blue'); + expect(checkboxControl).toBeChecked(); + userEvent.click(checkboxControl); const form = screen.getByRole('form'); expect(form).toHaveFormValues({ name: 'Max', score: 4, }); + + // material doesn't add these to the form + expect((fixture.componentInstance as MaterialFormsComponent).form?.get('agree')?.value).toBe(false); expect((fixture.componentInstance as MaterialFormsComponent).form?.get('color')?.value).toBe('B'); expect((fixture.componentInstance as MaterialFormsComponent).form?.get('date')?.value).toEqual(new Date(2022, 7, 11)); }); diff --git a/apps/example-app/src/app/examples/04-forms-with-material.ts b/apps/example-app/src/app/examples/04-forms-with-material.ts index ed510d1..48b1410 100644 --- a/apps/example-app/src/app/examples/04-forms-with-material.ts +++ b/apps/example-app/src/app/examples/04-forms-with-material.ts @@ -10,6 +10,8 @@ import { UntypedFormBuilder, Validators } from '@angular/forms'; + I Agree + Score