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

test: enhance example to verify pre-set form values on a Material sel… #276

merged 3 commits into from
Jan 5, 2022

Conversation

mleimer
Copy link
Contributor

@mleimer mleimer commented Dec 31, 2021

This PR enhances the example test for Angular Material Forms.
It shows how to read and verify any pre-set form and display values on a select box.

Based on #275


const { fixture, detectChanges } = await render(MaterialFormsComponent, {
imports: [MaterialModule],
componentProperties: {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for enhancing this example.
I would like to change a few things, the most important one to not overwrite the form.
Because this also overwrites the validators, which can be problematic if requirements change.

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');

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@timdeschryver yes, that is indeed a very good point. I adapted it.

Copy link
Member

Choose a reason for hiding this comment

The 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.
Not sure if this is better though 😅
If you have an idea, feel free to drop it here.

const { fixture } = render(Component);
// this would be new and invokes a CD cycle 
invoke(() => {
  fixture.componentInstance.form.setValue();
})

Copy link
Contributor Author

@mleimer mleimer Jan 5, 2022

Choose a reason for hiding this comment

The 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 formControl being passed into it through its input properties. If we now follow the example from https://github.com/testing-library/angular-testing-library/blob/main/apps/example-app/src/app/examples/02-input-output.spec.ts#L10 how to pass input properties, one would not think of having to call detectChanges() to validate any pre-set value within the formControl.

Maybe we should consider adding a test scenario where one passes a formControl into the test component?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those examples are based on "real-world" scenarios.
I didn't had the need (yet) to pass form controls to child components - and editing the code just for the tests doesn't feel right. The same problem also applies when validators are added to the control.

For now, I think this example shows a user how to do it the best way.
We can revisit and modify this later if we can think of a better solution.

@timdeschryver timdeschryver merged commit 20ae40d into testing-library:main Jan 5, 2022
@github-actions
Copy link

🎉 This PR is included in version 11.0.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants