This repository was archived by the owner on Jan 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathmockers-without-borders.js
62 lines (53 loc) · 1.67 KB
/
mockers-without-borders.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Subject under test
var App = {}
App.payMerchants = function (startDate, endDate) {
var transactions = App.fetch(startDate, endDate)
var purchaseOrders = App.createPurchaseOrders(App.groupByMerchant(transactions))
App.submit(purchaseOrders)
}
// Test
var td = require('testdouble')
module.exports = {
beforeEach: function () {
td.replace(App, 'fetch')
td.replace(App, 'submit')
},
paysMerchantsWithTotals: function () {
var transactions = [
{merchant: 'Nike', desc: 'Shoes', amount: 119.20},
{merchant: 'Nike', desc: 'Waterproof spray', amount: 10.10},
{merchant: 'Apple', desc: 'iPad', amount: 799.99},
{merchant: 'Apple', desc: 'iPad Cover', amount: 59.99}
]
var startDate = new Date(2015, 0, 1)
var endDate = new Date(2015, 11, 31)
td.when(App.fetch(startDate, endDate)).thenReturn(transactions)
App.payMerchants(startDate, endDate)
td.verify(App.submit([
{merchant: 'Nike', total: 129.30},
{merchant: 'Apple', total: 859.98}
]))
},
afterEach: function () {
td.reset()
}
}
// Fake production implementations to simplify example test of subject
var _ = require('lodash')
App.fetch = function (startDate, endDate) {
// Imagine something that hits a data store here
}
App.groupByMerchant = function (transactions) {
return _.groupBy(transactions, 'merchant')
}
App.createPurchaseOrders = function (transactionsByMerchant) {
return _.map(transactionsByMerchant, function (transactions, merchant) {
return {
merchant: merchant,
total: _.sumBy(transactions, 'amount')
}
})
}
App.submit = function (purchaseOrders) {
// Imagine something that hits a payment processor here
}