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

Migrate tests for AddToCart button to Vite #1899

Merged
merged 4 commits into from
Jul 26, 2022
Merged

Conversation

frehner
Copy link
Contributor

@frehner frehner commented Jul 25, 2022

Description

In replacing the old providers, this is one way of doing it now with RTL. Not sure if this is better or not, but it does seem more clear to me?

Part of #670

Additional context


Before submitting the PR, please make sure you do the following:

  • Read the Contributing Guidelines
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123)
  • Update docs in this repository according to your change
  • Run yarn changeset add if this PR cause a version bump based on Keep a Changelog and adheres to Semantic Versioning

In replacing the old providers, this is one way of doing it now with RTL. Not sure if this is better or not, but it does seem more clear to me?
@github-actions
Copy link
Contributor

We detected some changes in packages/*/package.json or packages/*/src, and there are no updates in the .changeset.
If the changes are user-facing and should cause a version bump, run "yarn changeset add" to track your changes and include them in the next release CHANGELOG.
If you are making simple updates to examples or documentation, you do not need to add a changeset.

{
wrapper: ({children}) => (
<CartTestProviders
cartConfig={{linesAdd: mockLinesAdd, cart: {id: '456'}}}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

In more complicated situations, it's clear what props we're passing to the Provider to overwrite things

@@ -0,0 +1,95 @@
import React from 'react';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

A lot of this file is taking what is found in

import {mountWithProviders} from '../../../utilities/tests/shopifyMount.js';
import {mountWithCartProvider} from '../../CartProvider/tests/utilities.js';

and combining them into a new file. The reason for the new file is that the version of shopify's testing library that we have isn't compatible with Vitest, and there are issues where it's in the import path of a Vitest file.

}
);

expect(screen.getByRole('button')).toHaveAttribute('class', 'bg-blue-600');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note that in these tests, we want to getByRole instead of using a testId

@frehner
Copy link
Contributor Author

frehner commented Jul 25, 2022

@cartogram @mrkldshv @lordofthecactus Would like your thoughts on this one!

Copy link
Contributor

@mrkldshv mrkldshv left a comment

Choose a reason for hiding this comment

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

Left one minor comment. Everything looks good to me!

@frehner frehner requested a review from a team July 26, 2022 16:55
Really, there's no need to add a level of abstraction when we can just do it directly.
@frehner
Copy link
Contributor Author

frehner commented Jul 26, 2022

Reduced a layer of abstraction - the goal is to make these tests easy to read and understand, so personally I think removing the wrapper abstraction and just rendering the test provider directly helps with that. It also makes it easier to customize as well, which is why I'm avoiding writing a custom Testing-Library render function too.

render(
<CartTestProviders
cartProviderValues={{linesAdd: mockLinesAdd, cart: {id: '456'}}}
>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

easy to customize, and easy to tell what provider is being used. Just render directly instead of using wrapper

<ProductOptionsProvider data={product}>
<AddToCartButton variantId={null}>Add to cart</AddToCartButton>
</ProductOptionsProvider>,
{wrapper: CartTestProviders}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

When customization isn't needed, then we can use wrapper if you want

@frehner frehner merged commit a89ce2b into v1.x-2022-07 Jul 26, 2022
@frehner frehner deleted the add-to-cart-vite branch July 26, 2022 21:04
@frehner frehner mentioned this pull request Jul 26, 2022
4 tasks
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