-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathvue-apollo.js
94 lines (79 loc) · 2.24 KB
/
vue-apollo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import '@testing-library/jest-dom'
import fetch from 'isomorphic-unfetch'
import {DefaultApolloClient} from '@vue/apollo-composable'
import ApolloClient from 'apollo-boost'
import {setupServer} from 'msw/node'
import {graphql} from 'msw'
import {render, fireEvent, screen} from '..'
import Component from './components/VueApollo.vue'
// Since vue-apollo doesn't provide a MockProvider for Vue,
// you need to use some kind of mocks for the queries.
// We are using Mock Service Worker (aka MSW) library to declaratively mock API communication
// in your tests instead of stubbing window.fetch, or relying on third-party adapters.
const server = setupServer(
...[
graphql.query('getUser', (req, res, ctx) => {
const {variables} = req
if (variables.id !== '1') {
return res(
ctx.errors([
{
message: 'User not found',
},
]),
)
}
return res(
ctx.data({
user: {
id: 1,
email: '[email protected]',
__typename: 'User',
},
}),
)
}),
graphql.mutation('updateUser', (req, res, ctx) => {
const {variables} = req
return res(
ctx.data({
updateUser: {
id: variables.input.id,
email: variables.input.email,
__typename: 'User',
},
}),
)
}),
],
)
beforeAll(() => server.listen())
afterEach(() => server.resetHandlers())
afterAll(() => server.close())
test('mocking queries and mutations', async () => {
const apolloClient = new ApolloClient({
uri: 'http://localhost:3000',
fetch,
})
render(Component, {
props: {id: '1'},
global: {
provide: {
[DefaultApolloClient]: apolloClient,
},
},
})
//Initial rendering will be in the loading state,
expect(screen.getByText('Loading')).toBeInTheDocument()
expect(
await screen.findByText('Email: [email protected]'),
).toBeInTheDocument()
await fireEvent.update(
screen.getByLabelText('Email'),
)
await fireEvent.click(screen.getByRole('button', {name: 'Change email'}))
expect(
await screen.findByText('Email: [email protected]'),
).toBeInTheDocument()
})