Skip to content

Commit

Permalink
ch(feedback): refactor cart test case to test connected component
Browse files Browse the repository at this point in the history
  • Loading branch information
akhilome committed Feb 7, 2019
1 parent a4c692b commit d48579d
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 145 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ cache:
directories:
- node_modules
script:
- npm test
- npm run coveralls
2 changes: 1 addition & 1 deletion src/components/Cart.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import PropTypes from 'prop-types';
import CheckOutCard from './CheckOutCard';
import { removeFromCart, checkout } from '../actions';

export class Cart extends Component {
class Cart extends Component {
onCheckoutClick = async () => {
const { cart, checkout: placeOrder, history } = this.props;
const foodItems = Object.keys(cart).map(Number);
Expand Down
71 changes: 40 additions & 31 deletions src/tests/components/Cart.test.jsx
Original file line number Diff line number Diff line change
@@ -1,61 +1,70 @@
import React from 'react';
import { mount } from 'enzyme';
import MockAdapter from 'axios-mock-adapter';

import { Cart } from '../../components/Cart';
import Cart from '../../components/Cart';
import { reduxWrap } from '../helpers';
import axios from '../../services/axios';

const axiosMock = new MockAdapter(axios);

describe('<Cart />', () => {
const props = {
const initialState = {
user: { isLoggedIn: true },
fetching: { fetching: false, errorMessage: '' },
menu: [
{
id: 1,
food_name: 'Tasty Prawns',
food_image: 'https://i.imgur.com/mTHYwlc.jpg',
price: 1250,
},
{
id: 2,
food_name: 'Turkey Wings',
food_image: 'https://i.imgur.com/Bfn1CxC.jpg',
price: 950,
},
],
cart: {
1: {
foodName: 'Sweet food',
foodPrice: 829,
foodName: 'Tasty Prawns',
foodPrice: 1250,
},
2: {
foodName: 'Ugly plantain',
foodPrice: 129,
foodName: 'Turkey Wings',
foodPrice: 950,
},
},
removeFromCart: jest.fn(),
checkout: jest.fn(),
history: { push: jest.fn() },
fetching: { fetching: false, errorMessage: '' },
};

afterEach(axiosMock.restore);
afterAll(axiosMock.reset);

const connectedComponent = reduxWrap(<Cart />, { initialState });

it('should render correctly', () => {
const wrapper = mount(<Cart {...props} />);
const wrapper = mount(connectedComponent);
expect(wrapper).toMatchSnapshot();
wrapper.unmount();
});

it('should remove item from cart successfully', () => {
const wrapper = mount(<Cart {...props} />);
const wrapper = mount(connectedComponent);
const firstRemoveButton = wrapper.find('.food-card-checkout > button').first();
firstRemoveButton.simulate('click');
expect(props.removeFromCart).toHaveBeenCalledWith(1);
expect(wrapper.find('.food-card-checkout').length).toEqual(1);
wrapper.unmount();
});

it('should place order for food successfully', async () => {
const wrapper = mount(<Cart {...props} />);
const checkoutButton = wrapper.find('#checkout');
await checkoutButton.simulate('click');
expect(props.checkout).toHaveBeenCalledWith([1, 2]);
expect(props.history.push).toHaveBeenCalledWith('/order-history');
wrapper.unmount();
});

it('should not redirect user if error occurs', async () => {
const wrapper = mount(
<Cart
{...{
...props,
fetching: { ...props.fetching, errorMessage: 'something went bonkers' },
}}
/>,
);
axiosMock.onPost().replyOnce(201, {
status: 'success',
message: 'new order placed successfully',
});
const wrapper = mount(connectedComponent);
const checkoutButton = wrapper.find('#checkout');
await checkoutButton.simulate('click');
expect(props.history.push).toHaveBeenCalledTimes(1);
wrapper.unmount();
});
});
227 changes: 118 additions & 109 deletions src/tests/components/__snapshots__/Cart.test.jsx.snap
Original file line number Diff line number Diff line change
@@ -1,126 +1,135 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`<Cart /> should render correctly 1`] = `
<Cart
cart={
<Provider
store={
Object {
"1": Object {
"foodName": "Sweet food",
"foodPrice": 829,
},
"2": Object {
"foodName": "Ugly plantain",
"foodPrice": 129,
},
"dispatch": [Function],
"getState": [Function],
"replaceReducer": [Function],
"subscribe": [Function],
Symbol(observable): [Function],
}
}
checkout={[MockFunction]}
fetching={
Object {
"errorMessage": "",
"fetching": false,
}
}
history={
Object {
"push": [MockFunction],
}
}
removeFromCart={[MockFunction]}
>
<div
className="wrapper"
>
<h2>
Items To Be Purchased
</h2>
<section
className="container checkout"
<Connect(Cart)>
<Cart
cart={
Object {
"1": Object {
"foodName": "Tasty Prawns",
"foodPrice": 1250,
},
"2": Object {
"foodName": "Turkey Wings",
"foodPrice": 950,
},
}
}
checkout={[Function]}
fetching={
Object {
"errorMessage": "",
"fetching": false,
}
}
removeFromCart={[Function]}
>
<CheckOutCard
buttonCallback={[Function]}
foodName="Sweet food"
foodPrice={829}
id={1}
key="1"
<div
className="wrapper"
>
<div
className="food-card-checkout"
data-foodid={1}
<h2>
Items To Be Purchased
</h2>
<section
className="container checkout"
>
<div
className="food-card-checkout__details"
<CheckOutCard
buttonCallback={[Function]}
foodName="Tasty Prawns"
foodPrice={1250}
id={1}
key="1"
>
<h4>
Sweet food
</h4>
<p>
829
</p>
</div>
<button
onClick={[Function]}
type="button"
<div
className="food-card-checkout"
data-foodid={1}
>
<div
className="food-card-checkout__details"
>
<h4>
Tasty Prawns
</h4>
<p>
1250
</p>
</div>
<button
onClick={[Function]}
type="button"
>
Remove
</button>
</div>
</CheckOutCard>
<CheckOutCard
buttonCallback={[Function]}
foodName="Turkey Wings"
foodPrice={950}
id={2}
key="2"
>
Remove
</button>
</div>
</CheckOutCard>
<CheckOutCard
buttonCallback={[Function]}
foodName="Ugly plantain"
foodPrice={129}
id={2}
key="2"
>
<div
className="food-card-checkout"
data-foodid={2}
>
<div
className="food-card-checkout__details"
>
<h4>
Turkey Wings
</h4>
<p>
950
</p>
</div>
<button
onClick={[Function]}
type="button"
>
Remove
</button>
</div>
</CheckOutCard>
</section>
<div
className="food-card-checkout"
data-foodid={2}
className="order-price"
>
<div
className="food-card-checkout__details"
>
<h4>
Ugly plantain
</h4>
<p>
129
</p>
</div>
<button
onClick={[Function]}
type="button"
>
Remove
</button>
<p>
<strong>
total price
</strong>
: ₦
<span
id="order-price"
>
2,200
</span>
</p>
</div>
</CheckOutCard>
</section>
<div
className="order-price"
>
<p>
<strong>
total price
</strong>
: ₦
<span
id="order-price"
<button
className="btn-primary"
id="checkout"
onClick={[Function]}
type="button"
>
958
</span>
</p>
</div>
<button
className="btn-primary"
id="checkout"
onClick={[Function]}
type="button"
>
Checkout
</button>
</div>
</Cart>
Checkout
</button>
</div>
</Cart>
</Connect(Cart)>
</Provider>
`;
7 changes: 4 additions & 3 deletions src/tests/helpers/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import { Provider } from 'react-redux';

import reducers from '../../reducers';

const store = createStore(reducers, applyMiddleware(thunk));

export const reduxWrap = component => <Provider store={store}>{component}</Provider>;
export const reduxWrap = (
component,
{ initialState, store = createStore(reducers, initialState, applyMiddleware(thunk)) } = {},
) => <Provider store={store}>{component}</Provider>;
export const routerWrap = component => <MemoryRouter>{component}</MemoryRouter>;

/*
Expand Down

0 comments on commit d48579d

Please sign in to comment.