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

improve snapshot tests #3597

Merged
merged 1 commit into from
Nov 9, 2022
Merged

Conversation

kalinkrustev
Copy link
Contributor

@kalinkrustev kalinkrustev commented Nov 9, 2022

I will put comments in the Files changed tab about the ideas of the changes.

@github-actions
Copy link

github-actions bot commented Nov 9, 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>.

snapshot({ severity: 'danger' }, 'severity danger');
snapshot({ severity: 'invalid' }, 'severity invalid');
});
snapshot(<Badge />, 'default');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

To make the snapshot function easier to use across components, it expects the element, not the properties only.

@@ -1,78 +1,78 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Badge check snapshots: default 1`] = `
exports[`Badge default 1`] = `
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here the 'check snapshots: ' string was not really useful, as it is clear that snapshots are being checked.

snapshot(<Button />, 'when label is true it renders Button with default aria label');
snapshot(<Button label={'test'} />, 'when aria-label prop is not exist aria-label prop should be equal to label prop ');
snapshot(<Button aria-label={'test'} />, 'when label prop is not exist label prop should be equal to aria-label prop');
snapshot(<Button label={'test'} badge={'lost'} />, 'when using badge and label the aria-label should contain both values');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here, the snapshot calls replaced the old code, that was repetitive. It is now more clearly visible, that some tests are doing the same thing and that test names might be a bit incorrect. Three of them are rendering just <Button />.

test('when removable is true it returns with remove icon', () => {
// Arrange
const removeOn = jest.fn();
const { container } = render(<Chip removable onRemove={removeOn} />);

expect(container).toMatchSnapshot('before remove');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here is example how to pass a name to the toMatchSnapshot call, so that two snapshots are made in the same test. Here is makes sense to snapshot before and after the action, as the component rendered differently.

/>
</div>
</div>
`;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

You can observe the above how the before remove and after remove snapshots rendered different results (as expected)

@@ -27,7 +27,6 @@ describe('InputText', () => {

// Act
expect(input).toBeEnabled();
expect(input).not.toHaveClass('p-filled');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is not needed, as the snapshot will cover this check.

Copy link
Member

@melloware melloware Nov 9, 2022

Choose a reason for hiding this comment

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

I left this here because it was explicitly what i was testing not meant to be implicitly figured out from the SNAPSHOT. I am proving that its not FILLED before checking the SNAPSHOT.

@@ -40,7 +40,6 @@ describe('InputTextarea', () => {

// Act
expect(input).toBeEnabled();
expect(input).not.toHaveClass('p-filled');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is not needed, as the snapshot will cover this check.

Copy link
Member

Choose a reason for hiding this comment

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

Explicit test since its in the description of the test its testing

test(name, () => {
expect(render(element).container).toMatchSnapshot();
});
}
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 reusable function, to avoid repetitive calls to test, expect, render and toMatchSnapshot

@github-actions
Copy link

github-actions bot commented Nov 9, 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

Just a couple of comments.

Copy link
Member

@melloware melloware left a comment

Choose a reason for hiding this comment

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

changes requested

Copy link
Member

@melloware melloware left a comment

Choose a reason for hiding this comment

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

Actually I am OK with this.

@melloware melloware merged commit 68563c3 into primefaces:master Nov 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants