Skip to content

Commit 2c28e77

Browse files
committed
Use global provider to inject apollo client
1 parent 22e7f84 commit 2c28e77

File tree

1 file changed

+23
-33
lines changed

1 file changed

+23
-33
lines changed

src/__tests__/vue-apollo.js

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import '@testing-library/jest-dom'
22
import fetch from 'isomorphic-unfetch'
33
import {render, fireEvent, screen} from '..'
4-
import { DefaultApolloClient } from '@vue/apollo-composable'
4+
import {DefaultApolloClient} from '@vue/apollo-composable'
55
import ApolloClient from 'apollo-boost'
66
import {setupServer} from 'msw/node'
77
import {graphql} from 'msw'
8-
import { provide, h } from 'vue'
98
import Component from './components/VueApollo.vue'
109

1110
// Since vue-apollo doesn't provide a MockProvider for Vue,
@@ -31,12 +30,11 @@ const server = setupServer(
3130

3231
return res(
3332
ctx.data({
34-
user:
35-
{
36-
id: 1,
37-
email: 'alice@example.com',
38-
__typename: 'User'
39-
},
33+
user: {
34+
id: 1,
35+
email: 'alice@example.com',
36+
__typename: 'User',
37+
},
4038
}),
4139
)
4240
}),
@@ -47,48 +45,40 @@ const server = setupServer(
4745
return res(
4846
ctx.data({
4947
updateUser: {
50-
id: variables.input.id,
48+
id: variables.input.id,
5149
email: variables.input.email,
52-
__typename: 'User'
50+
__typename: 'User',
5351
},
5452
}),
55-
)
56-
}),
57-
53+
)
54+
}),
5855
],
5956
)
6057

6158
beforeAll(() => server.listen())
6259
afterEach(() => server.resetHandlers())
6360
afterAll(() => server.close())
6461

65-
const apolloClient = new ApolloClient({
66-
uri: "http://localhost:3000",
67-
fetch,
68-
})
69-
70-
const ComponentWithInjectedApollo = {
71-
// It would be preferable to use global.provide when we pass options to VTU options
72-
// to testing library render function but that option is not yet supported by VTU
73-
setup () {
74-
provide(DefaultApolloClient, apolloClient)
75-
},
76-
render() {
77-
return h(Component)
78-
}
79-
}
80-
8162
test('mocking queries and mutations', async () => {
63+
const apolloClient = new ApolloClient({
64+
uri: 'http://localhost:3000',
65+
fetch,
66+
})
8267

83-
render(ComponentWithInjectedApollo, {
84-
props: {id: '1'}
68+
render(Component, {
69+
props: {id: '1'},
70+
global: {
71+
provide: {
72+
[DefaultApolloClient]: apolloClient,
73+
},
74+
},
8575
})
8676

8777
//Initial rendering will be in the loading state,
8878
expect(screen.getByText('Loading')).toBeInTheDocument()
8979

9080
expect(
91-
await screen.findByText('Email: alice@example.com')
81+
await screen.findByText('Email: alice@example.com'),
9282
).toBeInTheDocument()
9383

9484
await fireEvent.update(
@@ -99,6 +89,6 @@ test('mocking queries and mutations', async () => {
9989
await fireEvent.click(screen.getByRole('button', {name: 'Change email'}))
10090

10191
expect(
102-
await screen.findByText('Email: alice+new@example.com'),
92+
await screen.findByText('Email: alice+new@example.com'),
10393
).toBeInTheDocument()
10494
})

0 commit comments

Comments
 (0)