From f7cb860175052eadf00d3454474eb02a2facd1ab Mon Sep 17 00:00:00 2001 From: Alberto Delgado Date: Sun, 10 Dec 2017 16:59:56 +0100 Subject: [PATCH] ch18 Homework 18.5, improvement of checkout component test --- .../nile/__tests__/checkout.test.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/ch18/devcorpio_homework18_5/nile/__tests__/checkout.test.js b/ch18/devcorpio_homework18_5/nile/__tests__/checkout.test.js index 75666f9..eb19051 100644 --- a/ch18/devcorpio_homework18_5/nile/__tests__/checkout.test.js +++ b/ch18/devcorpio_homework18_5/nile/__tests__/checkout.test.js @@ -9,12 +9,15 @@ Enzyme.configure({ adapter: new Adapter() }); describe('checkout', () => { const cartItems = { [Products[0].id]: 1, - [Products[1].id]: 3, + [Products[3].id]: 2, [Products[2].id]: 4 } + const cartItemsKeys = Object.keys(cartItems) + const totalCartItems = cartItemsKeys.length + const totalQuantityToShow = cartItemsKeys.reduce((acc, curr) => { + return acc + cartItems[curr] + }, 0) - const totalQuantityToShow = 8 - const totalCartItems = Object.keys(cartItems).length const wrapper = Enzyme.mount() it(`has ${totalCartItems} products`, () => { @@ -22,14 +25,14 @@ describe('checkout', () => { }) it('has the correct product title', () => { - Object.keys(cartItems).forEach((value) => { + cartItemsKeys.forEach((value) => { let productTitle = Products[value].title expect(wrapper.find(`td[children="${productTitle}"]`).length).toBe(1) }) }) it('each product has the correct quantity', () => { - Object.keys(cartItems).forEach((value) => { + cartItemsKeys.forEach((value) => { let productId = Products[value].id let productQuantity = cartItems[productId] @@ -37,7 +40,7 @@ describe('checkout', () => { }) }) - it(`show the total product quantity correctly`, () => { + it(`show the total product quantity correctly: ${totalQuantityToShow}`, () => { expect(wrapper.find('p').text()).toBe("Total: " + totalQuantityToShow) })