Last active
November 23, 2020 08:22
-
-
Save mickaelw/07fa70b57b8df2a7d232be7bd7bd9056 to your computer and use it in GitHub Desktop.
This file contains 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
describe('Unit | Calcule du prix du panier', () => { | |
let getBasketInformation: GetBasketInformation | |
let addToBasket: AddToBasket | |
beforeEach(() => { | |
const basketRepository = new InMemoryBasketRepository() | |
getBasketInformation = new GetBasketInformation(basketRepository) | |
addToBasket = new AddToBasket(basketRepository) | |
}) | |
it('Pour un panier vide', async () => { | |
const basket = await getBasketInformation.handle() | |
expect(basket.price).to.equal(0) | |
expect(basket.items).to.deep.equal([]) | |
}) | |
describe('Emprunter n livres différents', () => { | |
it('Appliquer aucune remise', () => { | |
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('Emprunter n fois le même livre', () => { | |
const book = new Book('1') | |
it('Appliquer aucune remise pour uniquement un livre', async () => { | |
await addNTimesTheSameBook(1) | |
const basket = await getBasketInformation.handle() | |
expect(basket.price).to.equal(8) | |
expect(basket.items).to.deep.equal([book]) | |
}) | |
it('Appliquer 5% de remise pour 2 livres identiques', 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('Appliquer 25% de remise pour 3 livres identiques', 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('Appliquer 25% de remise pour plus de 3 livres identiques', 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