Code from Unit testing in JavaScript Part 3 - Test runners
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function orderTotal(order) { | |
return order.items.reduce((prev, cur) => cur.price * (cur.quantity || 1) + prev, 0) | |
} | |
module.exports = orderTotal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const orderTotal = require('./order-total') | |
it('Quantity', () => | |
expect(orderTotal({ | |
items: [ | |
{ 'name': 'Dragon candy', price: 2, quantity: 3 } | |
] | |
})).toBe(6)) | |
it('No quantity specified', () => | |
expect(orderTotal({ | |
items: [ | |
{ 'name': 'Dragon candy', price: 3 } | |
] | |
})).toBe(3) | |
) | |
it('Happy path (Example 1)', () => | |
expect(orderTotal({ | |
items: [ | |
{ name: 'Dragon food', price: 8, quantity: 1 }, | |
{ name: 'Dragon cage (small)', price: 800, quantity: 1 } | |
] | |
})).toBe(808) | |
) | |
it('Happy path (Example 2)', () => | |
expect(orderTotal({ | |
items: [ | |
{ name: 'Dragon collar', price: 20, quantity: 1 }, | |
{ name: 'Dragon chew toy', price: 40, quantity: 1 } | |
] | |
})).toBe(60)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "test-runners", | |
"version": "1.0.0", | |
"description": "", | |
"main": "code-and-tests.js", | |
"scripts": { | |
"test": "jest", | |
"watch": "jest --watch *.js" | |
}, | |
"author": "", | |
"license": "ISC", | |
"devDependencies": { | |
"jest": "^21.2.1" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment