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 BuyNow and CartCheckout buttons to Vitest #1915

Merged
merged 2 commits into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import React from 'react';
import {ShopifyTestProviders} from '../../../utilities/tests/provider-helpers.js';
import {render, screen} from '@testing-library/react';
import {vi} from 'vitest';
import userEvent from '@testing-library/user-event';

const mockCreateInstantCheckout = vi.fn();
const mockUseInstantCheckout = vi.fn();
const mockUseCartFetch = vi.fn();

import {BuyNowButton} from '../BuyNowButton.client.js';

vi.mock('../../CartProvider/index.js', () => ({
...(vi.importActual('../../CartProvider/index.js') as {}),
useInstantCheckout: mockUseInstantCheckout,
useCartFetch: mockUseCartFetch,
}));

describe('<BuyNowButton/>', () => {
beforeEach(() => {
mockUseInstantCheckout.mockReturnValue({
createInstantCheckout: mockCreateInstantCheckout,
checkoutUrl: undefined,
});
});

it('renders a button', () => {
render(<BuyNowButton variantId="1">Buy now</BuyNowButton>, {
wrapper: ShopifyTestProviders,
});
expect(screen.getByRole('button')).toHaveTextContent('Buy now');
});

it('can optionally disable the button', () => {
render(
<BuyNowButton disabled={true} variantId="1">
Buy now
</BuyNowButton>,
{
wrapper: ShopifyTestProviders,
}
);

expect(screen.getByRole('button')).toBeDisabled();
});

it('allows pass-through props', () => {
render(
<BuyNowButton className="fancy-button" variantId="1">
Buy now
</BuyNowButton>,
{
wrapper: ShopifyTestProviders,
}
);

expect(screen.getByRole('button')).toHaveClass('fancy-button');
});

describe('when the button is clicked', () => {
it('uses useCartCreateCallback with the correct arguments', async () => {
const user = userEvent.setup();

render(
<BuyNowButton
attributes={[
{key: 'color', value: 'blue'},
{key: 'size', value: 'large'},
]}
quantity={4}
variantId="SKU123"
>
Buy now
</BuyNowButton>,
{
wrapper: ShopifyTestProviders,
}
);

await user.click(screen.getByRole('button'));

expect(mockCreateInstantCheckout).toHaveBeenCalledTimes(1);
expect(mockCreateInstantCheckout).toHaveBeenCalledWith({
lines: [
{
quantity: 4,
merchandiseId: 'SKU123',
attributes: [
{key: 'color', value: 'blue'},
{key: 'size', value: 'large'},
],
},
],
});
});

it('disables the button', async () => {
const user = userEvent.setup();

render(<BuyNowButton variantId="1">Buy now</BuyNowButton>, {
wrapper: ShopifyTestProviders,
});

const button = screen.getByRole('button');

expect(button).not.toBeDisabled();

await user.click(button);

expect(button).toBeDisabled();
});
});

describe('when a checkout URL is available', () => {
const {location} = window;
const mockSetHref = vi.fn((href) => href);

beforeEach(() => {
mockUseInstantCheckout.mockReturnValue({
createInstantCheckout: mockCreateInstantCheckout,
checkoutUrl: '/checkout?id=123',
});

delete (window as Partial<Window>).location;
window.location = {...window.location};
Object.defineProperty(window.location, 'href', {
set: mockSetHref,
});
});

afterEach(() => {
window.location = location;
mockUseInstantCheckout.mockRestore();
});

it('redirects to checkout', () => {
render(<BuyNowButton variantId="1">Buy now</BuyNowButton>, {
wrapper: ShopifyTestProviders,
});

expect(mockSetHref).toHaveBeenCalledTimes(1);
expect(mockSetHref).toHaveBeenCalledWith('/checkout?id=123');
});
});
});

This file was deleted.

Loading