Skip to content

Instantly share code, notes, and snippets.

@mickaelw
Last active April 25, 2020 11:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mickaelw/5d83ae145cb5435fd547eff9389a3964 to your computer and use it in GitHub Desktop.
Save mickaelw/5d83ae145cb5435fd547eff9389a3964 to your computer and use it in GitHub Desktop.
describe('Unit | Basket price calculation', () => {
let getBasketInformation: GetBasketInformation
let addToBasket: AddToBasket
beforeEach(() => {
const basketRepository = new InMemoryBasketRepository()
getBasketInformation = new GetBasketInformation(basketRepository)
addToBasket = new AddToBasket(basketRepository)
})
it('For an empty basket', async () => {
const basket = await getBasketInformation.handle()
expect(basket.price).to.equal(0)
expect(basket.items).to.deep.equal([])
})
describe('Borrowing n different books', () => {
it('Apply no discount', () => {
await addToBasket.execute(new Book('1'))
await addToBasket.execute(new Book('2'))
await addToBasket.execute(new Book('3'))
await addToBasket.execute(new Book('5'))
const basket = await getBasketInformation.handle()
expect(basket.price).to.equal(32)
expect(basket.items).to.deep.equal([
new Book('1'),
new Book('2'),
new Book('3'),
new Book('5')
])
})
})
describe('Borrowing n times the same book', () => {
const book = new Book('1')
it('Apply no discount for only one book', async () => {
await addNTimesTheSameBook(1)
const basket = await getBasketInformation.handle()
expect(basket.price).to.equal(8)
expect(basket.items).to.deep.equal([book])
})
it('Apply 5% discount for two same books', async () => {
await addNTimesTheSameBook(2)
const basket = await getBasketInformation.handle()
expect(basket.price).to.equal(15.2)
expect(basket.items).to.deep.equal([book, book])
})
it('Apply 25% discount for three same books', async () => {
await addNTimesTheSameBook(3)
const basket = await getBasketInformation.handle()
expect(basket.price).to.equal(18)
expect(basket.items).to.deep.equal([book, book, book])
})
it('Apply 25% discount for more than three times', async () => {
await addNTimesTheSameBook(4)
const basket = await getBasketInformation.handle()
expect(basket.price).to.equal(24)
expect(basket.items).to.deep.equal([book, book, book, book])
})
async function addNTimesTheSameBook(nTimes: number) {
for (let i = 1; i <= nTimes; i++)
await addToBasket.execute(book)
}
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment