Skip to content

Commit 2b4401a

Browse files
AndyAndy
authored andcommitted
ending
1 parent 56bf0a6 commit 2b4401a

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

tests/unit/MessageDisplay.spec.js

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,35 @@
11
import MessageDisplay from '@/components/MessageDisplay'
22
import { mount } from '@vue/test-utils'
3+
import { getMessage } from '@/services/axios'
4+
import flushPromises from 'flush-promises'
5+
6+
jest.mock('@/services/axios')
37

48
describe('MessageDisplay', () => {
5-
it('Calls getMessage and displays message', async () => {
6-
// mock the API call
9+
beforeEach(() => {
10+
jest.clearAllMocks()
11+
})
12+
13+
it('Calls getMessage once and displays message', async () => {
14+
const mockMessage = 'Hello from the db'
15+
getMessage.mockResolvedValueOnce({ text: mockMessage })
716
const wrapper = mount(MessageDisplay)
8-
// wait for promise to resolve
9-
// check that call happened once
10-
// check that component displays message
17+
18+
await flushPromises()
19+
expect(getMessage).toHaveBeenCalledTimes(1)
20+
21+
const message = wrapper.find('[data-testid="message"]').text()
22+
expect(message).toEqual(mockMessage)
1123
})
1224

1325
it('Displays an error when getMessage call fails', async () => {
14-
// mock the failed API call
26+
const mockError = 'Oops! Something went wrong.'
27+
getMessage.mockRejectedValueOnce(mockError)
1528
const wrapper = mount(MessageDisplay)
16-
// wait for promise to resolve
17-
// check that call happened once
18-
// check that component displays error
29+
30+
await flushPromises()
31+
expect(getMessage).toHaveBeenCalledTimes(1)
32+
const displayedError = wrapper.find('[data-testid="message-error"]').text()
33+
expect(displayedError).toEqual(mockError)
1934
})
2035
})

0 commit comments

Comments
 (0)