Created
December 4, 2017 07:57
-
-
Save mpj/2906d711a5d9fac5443ffa25e7e77e35 to your computer and use it in GitHub Desktop.
This file contains hidden or 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(fetch, order) { | |
fetch('https://vatapi.com/v1/country-code-check?code=' + order.country) | |
return Promise.resolve(order.items.reduce((prev, cur) => | |
cur.price * (cur.quantity || 1) + prev, 0)) | |
} | |
module.exports = orderTotal |
This file contains hidden or 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') | |
const emptyFunction = () => {} | |
it('calls vatapi.com correctly', () => { | |
let isFakeFetchCalled = false | |
const fakeFetch = (url) => { | |
expect(url).toBe('https://vatapi.com/v1/country-code-check?code=DE') | |
isFakeFetchCalled = true | |
} | |
orderTotal(fakeFetch, { | |
country: 'DE', | |
items: [ | |
{ 'name': 'Dragon waffles', price: 20, quantity: 2 } | |
] | |
}).then(result => { | |
expect(isFakeFetchCalled).toBe(true) | |
}) | |
}) | |
it('if country code specified') | |
it('Quantity', () => | |
orderTotal(emptyFunction, { | |
items: [ | |
{ 'name': 'Dragon candy', price: 2, quantity: 3 } | |
] | |
}).then(result => expect(result).toBe(6))) | |
it('No quantity specified', () => | |
orderTotal(emptyFunction, { | |
items: [ | |
{ 'name': 'Dragon candy', price: 3 } | |
] | |
}).then(result => expect(result).toBe(3))) | |
it('Happy path (Example 1)', () => | |
orderTotal(emptyFunction,{ | |
items: [ | |
{ name: 'Dragon food', price: 8, quantity: 1 }, | |
{ name: 'Dragon cage (small)', price: 800, quantity: 1 } | |
] | |
}).then(result => expect(result).toBe(808))) | |
it('Happy path (Example 2)', () => | |
orderTotal(emptyFunction, { | |
items: [ | |
{ name: 'Dragon collar', price: 20, quantity: 1 }, | |
{ name: 'Dragon chew toy', price: 40, quantity: 1 } | |
] | |
}).then(result => expect(result).toBe(60))) |
This file contains hidden or 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" | |
} | |
} |
This file contains hidden or 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 fetch = require('node-fetch') | |
const result = | |
fetch('https://vatapi.com/v1/country-code-check?code=DE', { | |
headers: { | |
'apikey': process.env.VAT_API_KEY | |
} | |
}) | |
.then(response => response.json()) | |
.then(data => data.rates.standard.value) | |
result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment