Skip to content

Commit

Permalink
Add tests for coupon code
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Rugh <rugh@adobe.com>
  • Loading branch information
sirugh committed Jan 22, 2020
1 parent e7e0285 commit 16d6a96
Show file tree
Hide file tree
Showing 6 changed files with 370 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`disables remove button on click 1`] = `
<div>
<span>
COUPON
</span>
<button
className="removeButton"
disabled={true}
onClick={[Function]}
>
Remove
</button>
</div>
`;

exports[`disables submit button on coupon entry 1`] = `
<form
className="entryForm"
onReset={[Function]}
onSubmit={[Function]}
>
<div>
<label
htmlFor="couponCode"
>
Coupon Code
</label>
<span
style={
Object {
"--iconsAfter": 0,
"--iconsBefore": 0,
}
}
>
<span>
<input
id="couponCode"
name="couponCode"
onBlur={[Function]}
onChange={[Function]}
placeholder="Enter code"
value=""
/>
</span>
<span />
<span />
</span>
<p>
</p>
</div>
<div>
<label />
<button
className="applybutton"
disabled={true}
type="submit"
>
<span>
Apply
</span>
</button>
</div>
</form>
`;

exports[`renders CouponCode input and submit button 1`] = `
<form
className="entryForm"
onReset={[Function]}
onSubmit={[Function]}
>
<div>
<label
htmlFor="couponCode"
>
Coupon Code
</label>
<span
style={
Object {
"--iconsAfter": 0,
"--iconsBefore": 0,
}
}
>
<span>
<input
id="couponCode"
name="couponCode"
onBlur={[Function]}
onChange={[Function]}
placeholder="Enter code"
value=""
/>
</span>
<span />
<span />
</span>
<p>
</p>
</div>
<div>
<label />
<button
className="applybutton"
disabled={false}
type="submit"
>
<span>
Apply
</span>
</button>
</div>
</form>
`;

exports[`renders an error message if an error occurs on code entry 1`] = `
<form
className="entryForm"
onReset={[Function]}
onSubmit={[Function]}
>
<div>
<label
htmlFor="couponCode"
>
Coupon Code
</label>
<span
style={
Object {
"--iconsAfter": 0,
"--iconsBefore": 0,
}
}
>
<span>
<input
id="couponCode"
name="couponCode"
onBlur={[Function]}
onChange={[Function]}
placeholder="Enter code"
value=""
/>
</span>
<span />
<span />
</span>
<p>
An error occurred. Try again.
</p>
</div>
<div>
<label />
<button
className="applybutton"
type="submit"
>
<span>
Apply
</span>
</button>
</div>
</form>
`;

exports[`renders an error state if unable to fetch applied coupons 1`] = `"Something went wrong. Refresh and try again."`;

exports[`renders nothing if no data is returned 1`] = `null`;

exports[`renders the coupon code view if applied coupons has data 1`] = `
<div>
<span>
COUPON
</span>
<button
className="removeButton"
disabled={false}
onClick={[Function]}
>
Remove
</button>
</div>
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
import React from 'react';
import { createTestInstance } from '@magento/peregrine';

import CouponCode from '../couponCode';
import { useLazyQuery, useMutation } from '@apollo/react-hooks';

jest.mock('@apollo/react-hooks', () => {
const runQuery = jest.fn();
const runMutation = jest.fn();
const queryResult = {
data: {
cart: {
id: 'cartId',
applied_coupons: null
}
},
error: null,
loading: false
};

const mutationResult = {
error: null,
loading: false
};

const useLazyQuery = jest.fn(() => [runQuery, queryResult]);
const useMutation = jest.fn(() => [runMutation, mutationResult]);

return { useLazyQuery, useMutation };
});

jest.mock('@magento/peregrine/lib/context/cart', () => {
const state = { cartId: 'cart123' };
const api = {};
const useCartContext = jest.fn(() => [state, api]);

return { useCartContext };
});

const defaultProps = {
classes: {
applyButton: 'applybutton',
appliedCoupon: 'appliedCoupon',
entryForm: 'entryForm',
removeButton: 'removeButton'
}
};

test('renders nothing if no data is returned', () => {
useLazyQuery.mockReturnValueOnce([
jest.fn(),
{
data: null
}
]);

const tree = createTestInstance(<CouponCode {...defaultProps} />);

expect(tree.toJSON()).toMatchSnapshot();
});

test('renders an error state if unable to fetch applied coupons', () => {
useLazyQuery.mockReturnValueOnce([
jest.fn(),
{
data: {},
error: true
}
]);

const tree = createTestInstance(<CouponCode {...defaultProps} />);

expect(tree.toJSON()).toMatchSnapshot();
});

test('renders CouponCode input and submit button', () => {
const instance = createTestInstance(<CouponCode {...defaultProps} />);

expect(instance.toJSON()).toMatchSnapshot();
});

test('disables submit button on coupon entry', () => {
useMutation.mockReturnValueOnce([
jest.fn(),
{
loading: true
}
]);

const instance = createTestInstance(<CouponCode {...defaultProps} />);
expect(instance.toJSON()).toMatchSnapshot();
});

test('renders an error message if an error occurs on code entry', () => {
useMutation.mockReturnValueOnce([
jest.fn(),
{
error: 'AN ERROR'
}
]);
const instance = createTestInstance(<CouponCode {...defaultProps} />);

expect(instance.toJSON()).toMatchSnapshot();
});

test('renders the coupon code view if applied coupons has data', () => {
useLazyQuery.mockReturnValueOnce([
jest.fn(),
{
data: {
cart: {
applied_coupons: [
{
code: 'COUPON'
}
]
}
}
}
]);
const instance = createTestInstance(<CouponCode {...defaultProps} />);

expect(instance.toJSON()).toMatchSnapshot();
});

test('disables remove button on click', () => {
useLazyQuery.mockReturnValueOnce([
jest.fn(),
{
data: {
cart: {
applied_coupons: [
{
code: 'COUPON'
}
]
}
}
}
]);

// Mock the click of the remove button
useMutation
.mockReturnValueOnce([
jest.fn(),
{
loading: false
}
])
.mockReturnValueOnce([
jest.fn(),
{
loading: true
}
]);

const instance = createTestInstance(<CouponCode {...defaultProps} />);
expect(instance.toJSON()).toMatchSnapshot();
});
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* Styles for "add" view. */
.entryForm {
display: grid;
grid-template-columns: 1fr 1fr;
Expand Down Expand Up @@ -28,7 +29,6 @@
}

/* Styles for "removal" view. */

.removeButton {
margin-left: 1rem;
font-weight: bold;
Expand Down
Loading

0 comments on commit 16d6a96

Please # to comment.