Skip to content

Instantly share code, notes, and snippets.

@mickaelw
Last active June 19, 2020 05:10
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/59ccf3414d718636dae12f609971dbc8 to your computer and use it in GitHub Desktop.
Save mickaelw/59ccf3414d718636dae12f609971dbc8 to your computer and use it in GitHub Desktop.
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('Fetch all books', () => {
it('Everything is good', 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('Something is wrong', 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('Add a book', () => {
it('Everything is good', 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('Something is wrong', 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