Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardcho1231 committed Mar 26, 2024
1 parent cf324d1 commit 36d2d5d
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const CheckoutCardDetail = ({ type, summary, children, link, className, isOpen,
);
};

CheckoutCardDetail.PropTypes = {
CheckoutCardDetail.propTypes = {
type: PropTypes.oneOf([ACCOUNT, BILLING_ADDRESS, PAYMENT, REVIEW]).isRequired,
summary: PropTypes.string,
children: PropTypes.any,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import React from "react";
import { render, screen, act } from "@testing-library/react";

Check failure on line 2 in blocks/subscriptions-block/components/CheckoutCardDetail/index.test.jsx

View workflow job for this annotation

GitHub Actions / ensure_minimum_test_coverage_linting

'act' is defined but never used
import '@testing-library/jest-dom';

import CheckoutCardDetail from "./index";

describe('CheckoutCardDetail component', () => {

it("summary is shown when card is open", async () => {
render(
<CheckoutCardDetail
type='Account'
isOpen={true}

Check failure on line 13 in blocks/subscriptions-block/components/CheckoutCardDetail/index.test.jsx

View workflow job for this annotation

GitHub Actions / ensure_minimum_test_coverage_linting

Value must be omitted for boolean attributes
>
<p>Account placeholder</p>
</CheckoutCardDetail>
)
expect(await screen.getByText('1. checkout-block.account')).toHaveTextContent('1. checkout-block.account');

Check failure on line 18 in blocks/subscriptions-block/components/CheckoutCardDetail/index.test.jsx

View workflow job for this annotation

GitHub Actions / ensure_minimum_test_coverage_linting

`getByText` query is sync so it does not need to be awaited
expect(await screen.getByText('Account placeholder')).toHaveTextContent('Account placeholder');

Check failure on line 19 in blocks/subscriptions-block/components/CheckoutCardDetail/index.test.jsx

View workflow job for this annotation

GitHub Actions / ensure_minimum_test_coverage_linting

`getByText` query is sync so it does not need to be awaited
});
it("do not show summary if card is closed", async () => {
render(
<CheckoutCardDetail
type='Account'
isOpen={false}
>
<p>Account placeholder</p>
</CheckoutCardDetail>
)
expect(await screen.getByText('1. checkout-block.account')).toHaveTextContent('1. checkout-block.account');

Check failure on line 30 in blocks/subscriptions-block/components/CheckoutCardDetail/index.test.jsx

View workflow job for this annotation

GitHub Actions / ensure_minimum_test_coverage_linting

`getByText` query is sync so it does not need to be awaited
expect(await screen.queryByText('Account placeholder')).toBe(null);

Check failure on line 31 in blocks/subscriptions-block/components/CheckoutCardDetail/index.test.jsx

View workflow job for this annotation

GitHub Actions / ensure_minimum_test_coverage_linting

`queryByText` query is sync so it does not need to be awaited
});
it("renders billing address card", async () => {
render(
<CheckoutCardDetail

Check failure on line 35 in blocks/subscriptions-block/components/CheckoutCardDetail/index.test.jsx

View workflow job for this annotation

GitHub Actions / ensure_minimum_test_coverage_linting

Empty components are self-closing
type='Billing Address'
isOpen={true}

Check failure on line 37 in blocks/subscriptions-block/components/CheckoutCardDetail/index.test.jsx

View workflow job for this annotation

GitHub Actions / ensure_minimum_test_coverage_linting

Value must be omitted for boolean attributes
>
</CheckoutCardDetail>
)
expect(await screen.getByText('2. checkout-block.billingAddress')).toHaveTextContent('2. checkout-block.billingAddress');

Check failure on line 41 in blocks/subscriptions-block/components/CheckoutCardDetail/index.test.jsx

View workflow job for this annotation

GitHub Actions / ensure_minimum_test_coverage_linting

`getByText` query is sync so it does not need to be awaited
});
it("renders payment card", async () => {
render(
<CheckoutCardDetail
type='Payment'
isOpen={true}
>
</CheckoutCardDetail>
)
expect(await screen.getByText('3. checkout-block.payment')).toHaveTextContent('3. checkout-block.payment');
});
it("renders review card", async () => {
render(
<CheckoutCardDetail
type='Review'
isOpen={true}
>
</CheckoutCardDetail>
)
expect(await screen.getByText('4. checkout-block.review')).toHaveTextContent('4. checkout-block.review');
});
})
88 changes: 88 additions & 0 deletions blocks/subscriptions-block/features/checkout/default.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import React from "react";

import { render, screen, act } from "@testing-library/react";
import "@testing-library/jest-dom";

import { useIdentity, usePhrases } from "@wpmedia/arc-themes-components";
import useSales from "../../components/useSales";
import Checkout from "./default";

jest.mock("@wpmedia/arc-themes-components");
jest.mock("../../components/useSales");

const assignMock = jest.fn(() => "checkoutURL");
delete window.location;
window.location = { assign: assignMock, href: "checkoutURL" };

describe("Checkout Feature", () => {
afterEach(() => {
assignMock.mockClear();
})
it("show billing address card when user is logged in", async () => {
useIdentity.mockImplementation(() => ({
getSignedInIdentity: jest.fn(
(user) =>
user?.identities?.reduce((prev, current) =>
prev.lastLoginDate > current.lastLoginDate ? prev : current,
) || null,
),
Identity: {
isLoggedIn: jest.fn(async () => true),
getUserProfile: jest.fn(async () => {})
},
}));

usePhrases.mockImplementation(() => ({
t: jest.fn((phrase => phrase))
}))

useSales.mockReturnValue({
Sales: {},
});

await act(async () => {

Check warning on line 43 in blocks/subscriptions-block/features/checkout/default.test.jsx

View workflow job for this annotation

GitHub Actions / ensure_minimum_test_coverage_linting

Avoid wrapping Testing Library util calls in `act`
render(
<Checkout
customFields={{
offerURL: "/offer-url/"
}}
/>,
);
});
expect(screen.getByText("Billing Address Placeholder")).not.toBeNull();
});
it("redirects user to login url when user is not logged in", async () => {
useIdentity.mockImplementation(() => ({
getSignedInIdentity: jest.fn(
(user) =>
user?.identities?.reduce((prev, current) =>
prev.lastLoginDate > current.lastLoginDate ? prev : current,
) || null,
),
Identity: {
isLoggedIn: jest.fn(async () => false),
getUserProfile: jest.fn(async () => {})
},
}));

usePhrases.mockImplementation(() => ({
t: jest.fn((phrase => phrase))
}))

useSales.mockReturnValue({
Sales: {},
});

await act(async () => {

Check warning on line 76 in blocks/subscriptions-block/features/checkout/default.test.jsx

View workflow job for this annotation

GitHub Actions / ensure_minimum_test_coverage_linting

Avoid wrapping Testing Library util calls in `act`
render(
<Checkout
customFields={{
loginURL: "/login-url/",
}}
/>,
);
});
expect(screen.getByText("Account Placeholder")).not.toBeNull();
expect(window.location.href).toBe("/login-url/?redirect=checkoutURL");
});
});

0 comments on commit 36d2d5d

Please sign in to comment.