Last active
June 19, 2020 05:10
-
-
Save mickaelw/59ccf3414d718636dae12f609971dbc8 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('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