-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinjectable.spec.ts
33 lines (31 loc) · 1.96 KB
/
injectable.spec.ts
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
import { injectable, Order, Shipping } from '../index'
import { order } from './order'
import { deepFreeze } from './deepFreeze'
describe('Basic order functionality', () => {
const numberToPrice = (price: number) => Number.parseFloat(price.toFixed(3))
const minusPrice = injectable.minusPrice(numberToPrice)
const addPrices = injectable.addPrices(numberToPrice)
const documentsShipping = injectable.documents.shipping(addPrices)
const invariantsShipping = injectable.invariants.shipping({ ds: documentsShipping, mp: minusPrice })
const shipping = injectable.order.shipping(invariantsShipping)
const _ = <U extends Shipping, T extends Order<U>>(order: T) => ({
invoice: (invoice: U) => shipping(deepFreeze(order)).invoice(deepFreeze(invoice)),
cancel: (cancelation: U) => shipping(deepFreeze(order)).cancel(deepFreeze(cancelation)),
refund: (refund: U) => shipping(deepFreeze(order)).refund(deepFreeze(refund)) })
const updateDocuments = (documents: {shipping: number}[]) => documents
.map(({ shipping, ...rest }) => ({ ...rest, shipping: numberToPrice(shipping / 10) }))
const sutOrder = { ...order,
shipping: numberToPrice(order.shipping / 10),
invoiced: updateDocuments(order.invoiced),
canceled: updateDocuments(order.canceled),
refunded: updateDocuments(order.refunded) }
it('Check order shipping calculations (with 3 digits', () => {
const shipping = _(sutOrder)
expect(shipping.invoice({ shipping: .001 })).toEqual({ qty: -1, total: -.001 })
expect(shipping.invoice({ shipping: 0 })).toEqual({ qty: 0, total: 0 })
expect(shipping.refund({ shipping: .058 })).toEqual({ qty: -1, total: .001 })
expect(shipping.refund({ shipping: .06 })).toEqual({ qty: -1, total: -.001 })
expect(shipping.cancel({ shipping: .001 })).toEqual({ qty: -1, total: -.001 })
expect(shipping.cancel({ shipping: 0 })).toEqual({ qty: 0, total: 0 })
})
})