Skip to content

Instantly share code, notes, and snippets.

@asoglovo
Created January 20, 2020 13:41
Show Gist options
  • Save asoglovo/72c8ca381e5f2377c697428723a26a4c to your computer and use it in GitHub Desktop.
Save asoglovo/72c8ca381e5f2377c697428723a26a4c to your computer and use it in GitHub Desktop.
Test for the PaymentWithCard component
import testWrapperBuilder from './PaymentWithCard.spec.factory'
import api from '@/api/paymentAPI'
jest.mock('@/api/paymentAPI', function() {
return { pay: jest.fn() }
})
describe('Payment with card', () => {
beforeEach(() => {
jest.clearAllMocks()
})
it('should display a success message when the payment succeeds', () => {
api.pay.mockReturnValue({ ok: true })
const successMessageSpy = jest.fn()
let wrapper = testWrapperBuilder()
.with.cardInfo('12345', 'Hari Seldon', '11/23')
.spy.successMsg(successMessageSpy)
.build()
wrapper.find('[data-qa="pay-btn"]').trigger('click')
expect(successMessageSpy).toHaveBeenCalledTimes(1)
})
it('should display an error message when the payment fails', () => {
api.pay.mockReturnValue({ ok: false })
const errorMessageSpy = jest.fn()
let wrapper = testWrapperBuilder()
.with.cardInfo('12345', 'Hari Seldon', '11/23')
.spy.errorMsg(errorMessageSpy)
.build()
wrapper.find('[data-qa="pay-btn"]').trigger('click')
expect(errorMessageSpy).toHaveBeenCalledTimes(1)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment