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