Skip to content

Instantly share code, notes, and snippets.

@ericelliott
Last active January 9, 2016 10:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericelliott/b37419f1d885edea39d3 to your computer and use it in GitHub Desktop.
Save ericelliott/b37419f1d885edea39d3 to your computer and use it in GitHub Desktop.
impure addToCart() mutates existing cart. https://jsbin.com/dobefu/edit?html,js,output
// impure addToCart mutates existing cart
const addToCart = (cart, item, quantity) => {
cart.items.push({
item,
quantity
});
return cart;
};
test('addToCart()', assert => {
const msg = 'addToCart() should add a new item to the cart.';
const originalCart = {
items: []
};
const cart = addToCart(
originalCart,
{
name: "Digital SLR Camera",
price: '1495'
},
1
);
const expected = 1; // num items in cart
const actual = cart.items.length;
assert.equal(actual, expected, msg);
assert.deepEqual(originalCart, cart, 'mutates original cart.');
assert.end();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment