Last active
October 15, 2021 21:12
-
-
Save ErrorPro/756c0f3b1d5e5233422950bd90c6c9fa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // create a graphlhttp middleware | |
| const middleware = graphqlHTTP({ | |
| schema, | |
| rootValue: { | |
| id: 'viewer', | |
| loaders: { | |
| countries: () => countriesMock, | |
| }, | |
| }, | |
| }); | |
| // create a mocked request | |
| const request = { | |
| method: 'POST', | |
| headers: {}, | |
| body: { query: '{ viewer { countries { name } } }' }, | |
| }; | |
| // create a mocked response, graphql middleware calls json function to set response data, so we stub it. | |
| const response = { | |
| setHeader: jest.fn(), | |
| end: jest.fn(), | |
| json: jest.fn(), | |
| }; | |
| // call middleware function with mocked response and request | |
| await middleware(request, response); | |
| // get json's stub function arguments, this is actually a data returned by graphql middleware | |
| const responseData = response.json.mock.calls[0][0]; | |
| //now we can do two things: check that the data returned is equal what we passed as a mock | |
| expect(responseData).toEqual(countriesMock); | |
| // or just use jest.snapshot to snapshot | |
| expect(responseData).toMatchSnapshot(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment