1
1
import '@testing-library/jest-dom'
2
2
import fetch from 'isomorphic-unfetch'
3
3
import { render , fireEvent , screen } from '..'
4
- import { DefaultApolloClient } from '@vue/apollo-composable'
4
+ import { DefaultApolloClient } from '@vue/apollo-composable'
5
5
import ApolloClient from 'apollo-boost'
6
6
import { setupServer } from 'msw/node'
7
7
import { graphql } from 'msw'
8
- import { provide , h } from 'vue'
9
8
import Component from './components/VueApollo.vue'
10
9
11
10
// Since vue-apollo doesn't provide a MockProvider for Vue,
@@ -31,12 +30,11 @@ const server = setupServer(
31
30
32
31
return res (
33
32
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
+ } ,
40
38
} ) ,
41
39
)
42
40
} ) ,
@@ -47,48 +45,40 @@ const server = setupServer(
47
45
return res (
48
46
ctx . data ( {
49
47
updateUser : {
50
- id : variables . input . id ,
48
+ id : variables . input . id ,
51
49
email : variables . input . email ,
52
- __typename : 'User'
50
+ __typename : 'User' ,
53
51
} ,
54
52
} ) ,
55
- )
56
- } ) ,
57
-
53
+ )
54
+ } ) ,
58
55
] ,
59
56
)
60
57
61
58
beforeAll ( ( ) => server . listen ( ) )
62
59
afterEach ( ( ) => server . resetHandlers ( ) )
63
60
afterAll ( ( ) => server . close ( ) )
64
61
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
-
81
62
test ( 'mocking queries and mutations' , async ( ) => {
63
+ const apolloClient = new ApolloClient ( {
64
+ uri : 'http://localhost:3000' ,
65
+ fetch,
66
+ } )
82
67
83
- render ( ComponentWithInjectedApollo , {
84
- props : { id : '1' }
68
+ render ( Component , {
69
+ props : { id : '1' } ,
70
+ global : {
71
+ provide : {
72
+ [ DefaultApolloClient ] : apolloClient ,
73
+ } ,
74
+ } ,
85
75
} )
86
76
87
77
//Initial rendering will be in the loading state,
88
78
expect ( screen . getByText ( 'Loading' ) ) . toBeInTheDocument ( )
89
79
90
80
expect (
91
- await screen . findByText ( 'Email: alice@example.com' )
81
+ await screen . findByText ( 'Email: alice@example.com' ) ,
92
82
) . toBeInTheDocument ( )
93
83
94
84
await fireEvent . update (
@@ -99,6 +89,6 @@ test('mocking queries and mutations', async () => {
99
89
await fireEvent . click ( screen . getByRole ( 'button' , { name : 'Change email' } ) )
100
90
101
91
expect (
102
- await screen . findByText ( 'Email: alice+new@example.com' ) ,
92
+ await screen . findByText ( 'Email: alice+new@example.com' ) ,
103
93
) . toBeInTheDocument ( )
104
94
} )
0 commit comments