|
| 1 | +import axiosMock from 'axios' |
| 2 | +import { render, fireEvent } from '@testing-library/vue' |
| 3 | +import Component from './components/Fetch.vue' |
| 4 | +import 'jest-dom/extend-expect' |
| 5 | + |
| 6 | +test('makes an API call and displays the greeting when load-greeting is clicked', async () => { |
| 7 | + axiosMock.get.mockImplementationOnce(() => |
| 8 | + Promise.resolve({ |
| 9 | + data: { greeting: 'hello there' } |
| 10 | + }) |
| 11 | + ) |
| 12 | + |
| 13 | + const { html, getByText } = render(Component, { props: { url: '/greeting' } }) |
| 14 | + |
| 15 | + // Act |
| 16 | + await fireEvent.click(getByText('Fetch')) |
| 17 | + |
| 18 | + expect(axiosMock.get).toHaveBeenCalledTimes(1) |
| 19 | + expect(axiosMock.get).toHaveBeenCalledWith('/greeting') |
| 20 | + getByText('hello there') |
| 21 | + |
| 22 | + // You can render component snapshots by using html(). However, bear in mind |
| 23 | + // that Snapshot Testing should not be treated as a replacement for regular |
| 24 | + // tests. |
| 25 | + // More about the topic: https://twitter.com/searls/status/919594505938112512 |
| 26 | + expect(html()).toMatchSnapshot() |
| 27 | +}) |
0 commit comments