Skip to content
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

Divider: switch to snapshot tests #3579

Merged
merged 2 commits into from
Nov 8, 2022

Conversation

kalinkrustev
Copy link
Contributor

Proposal to switch to snapshot testing. The benefits are:

  1. Test code is much simpler, focusing on running the different test cases
  2. The rendered markup is checked in full, not just some classes
  3. When there is unexpected change, it is easier to track in the snapshots

@github-actions
Copy link

github-actions bot commented Nov 4, 2022

Thanks a lot for your contribution! But, PR does not seem to be linked to any issues. Please manually link to an issue or mention it in the description using #<issue_id>.


test('when tpe has not any property it returns with default class', () => {
// Arrange
const { container } = render(<Divider />);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The original test is more complicated, than it need to be. Did you notice you have the same test multiple times?

Copy link
Contributor

Choose a reason for hiding this comment

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

I noticed this. But here I see the benefit of the function you wrote more than the snapshot benefit. You have created html code for all cases. The only thing we want to check is the existence of a class in render. If we created the same function for the old structure, which one would you think is advantageous? Please let me know if there's anything I missed. I will be happy to learn.

I am talking about this function:
const snapshot = (props, name) => expect(render(<Divider {...props} />).container).toMatchSnapshot(name);

Copy link
Member

Choose a reason for hiding this comment

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

So the real advantage of Snapshot testing over this current test. lets take for example this:

expect(container.getElementsByClassName('p-divider p-component p-divider-horizontal p-divider-center').length).toBe(1);

This test will NOT fail if the following is changed:

  1. If Divider is changed from a DIV to a SPAN this test would still pass and it should not the structure of the component has changed.
  2. If a new style class p-new-class was introduced in the component this test would still pass because the current query will still be true. And to me this test should fail if a NEW class is added to the component or an unexpected one is there like p-filled when it should not be etc.

So the benefits to me are tremendous.

// Act + Assert
expect(container.getElementsByClassName('p-divider p-component p-divider-horizontal p-divider-right').length).toBe(1);
});
test('when layout and align as property it returns with class', () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Did you notice you repeated the same test message? It is good for the test message to reflect the case you are testing.

@kalinkrustev
Copy link
Contributor Author

Currently Code Format check fails on master, that's why it also failed here.

@melloware
Copy link
Member

I fixed it if you want to merge master

@melloware
Copy link
Member

I think we are also missing tests where there is left , right, and center text on the Divider?

@melloware
Copy link
Member

Oh no i see you have those!

@github-actions
Copy link

github-actions bot commented Nov 4, 2022

Thanks a lot for your contribution! But, PR does not seem to be linked to any issues. Please manually link to an issue or mention it in the description using #<issue_id>.

@melloware
Copy link
Member

This is great especially for components like Divider, Badge, Chip etc. But when you need events like my InputText and InputTextArea tests do you still Snapshot those or do you write them like I did with events? Or do you mix and match?

@kalinkrustev
Copy link
Contributor Author

You can mix and match. Snapshots substitute the need to check individual markup details manually. For example there is no need to maintain this verbose code, but instead do a snapshot.

expect(input).toHaveClass('p-inputtextarea p-inputtext p-component p-inputtextarea-resizable');
expect(input.getAttribute('rows')).toBe('3');
expect(input.getAttribute('cols')).toBe('12');

I have not used directly @testing-library/react fireEvent, but use a bit more complicated alternative that allows the testing code to be shared with this storybook play function. This allows also to track the visual snapshots. But anyway in both cases markup snapshots are easier to understand and give you meaningful overview of the full set of changes. They are better than the individual expect() calls, which I think will stop on the first failure and not tell you about all the changes that happened.

Of course snapshots are not a solution for everything and require attention when maintaining, but still I think they are a better choice in many cases, compared to the expect() calls related to markup.

@melloware
Copy link
Member

OK this totally rocks. You can absolutely mix and match so in my InputText tests I am using events to simulate typing and then using Snapshot to validate the results and state of the dom. I am a huge fan of this for validating PrimeReact.

@melloware melloware added the Component: Test Issue or pull request is related to Component Testing label Nov 5, 2022
@melloware melloware changed the title test: switch to snapshot tests Divider: switch to snapshot tests Nov 5, 2022
@github-actions
Copy link

github-actions bot commented Nov 5, 2022

Thanks a lot for your contribution! But, PR does not seem to be linked to any issues. Please manually link to an issue or mention it in the description using #<issue_id>.

@melloware melloware merged commit 0f20078 into primefaces:master Nov 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Test Issue or pull request is related to Component Testing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants