Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
describe('Integration | Gateways | Http basket repository', () => {
const BASE_API_URL = 'https://www.fakeapi.com'
let httpClient: HTTPClient
let basketRepository: BasketRepository
let basket: Basket
beforeEach(() => {
httpClient = {
get : sinon.stub(),
post: sinon.stub()
}
basketRepository = new HTTPBasketRepository(httpClient)
basket = new Basket()
})
context('Récupérer le panier', () => {
it('Tout s’est bien passé', async () => {
basket.add(new Book('book1'))
httpClient.get.resolves({ basket_items: ['book1'] })
const result = await basketRepository.getBasket()
expect(httpClient.get).to.have.been.calledOnceWithExactly(`${ BASE_API_URL }/baskets`)
expect(result).to.eql(basket)
})
it('Quelque chose s’est mal passé', async () => {
const error = new Error('error from api')
httpClient.get.rejects(error)
try {
await basketRepository.getBasket()
fail()
} catch (e) {
expect(e).to.eql(error)
}
})
})
context('Ajout d’un livre au panier', () => {
it('Tout s’est bien passé', async () => {
basket.add(new Book('book1'))
await basketRepository.save(basket)
expect(httpClient.post).to.have.been.calledOnceWithExactly(
`${ BASE_API_URL }/baskets`,
{ basket_items: ['book1'] }
)
})
it('Quelque chose s’est mal passé', async () => {
basket.add(new Book('book1'))
const error = new Error('error from api')
httpClient.post.rejects(error)
try {
await basketRepository.save(basket)
fail()
} catch (e) {
expect(e).to.eql(error)
}
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.