Skip to content

Instantly share code, notes, and snippets.

@mickaelw
Last active April 20, 2020 06:58
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/148c9a967e5f6d3b5feaf462a3a698f0 to your computer and use it in GitHub Desktop.
Save mickaelw/148c9a967e5f6d3b5feaf462a3a698f0 to your computer and use it in GitHub Desktop.
describe('Unit | Basket price calculation', () => {
let basket: Basket
beforeEach(() => basket = new Basket())
it('For an empty basket', () => {
expect(basket.price).to.equal(0)
})
describe('Borrowing n different books', () => {
it('Apply no discount', () => {
basket.add(new Book('1'))
basket.add(new Book('2'))
basket.add(new Book('3'))
basket.add(new Book('5'))
expect(basket.price).to.equal(32)
})
})
describe('Borrowing n times the same book', () => {
it('Apply no discount for only one book', () => {
addNTimesTheSameBook(1)
expect(basket.price).to.equal(8)
})
it('Apply 5% discount for two same books', () => {
addNTimesTheSameBook(2)
expect(basket.price).to.equal(15.2)
})
it('Apply 25% discount for three same books', () => {
addNTimesTheSameBook(3)
expect(basket.price).to.equal(18)
})
it('Apply 25% discount for more than three times', () => {
addNTimesTheSameBook(4)
expect(basket.price).to.equal(24)
})
function addNTimesTheSameBook(nTimes: number) {
const book = new Book('1')
for (let i = 1; i <= nTimes; i++)
basket.add(book)
}
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment