This repository has been archived by the owner on Oct 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] Modernize graphql testing (#623)
- Loading branch information
Showing
17 changed files
with
256 additions
and
341 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
export {default} from './createGraphQLFactory'; | ||
|
||
export * from './createGraphQLFactory'; | ||
export * from './types'; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export {InflightLink} from './inflight'; | ||
export {MockLink} from './mocks'; |
This file contains 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
4 changes: 2 additions & 2 deletions
4
...ges/graphql-testing/src/MockApolloLink.ts → packages/graphql-testing/src/links/mocks.ts
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import gql from 'graphql-tag'; | ||
|
||
import {MockGraphQLResponse} from '../../types'; | ||
import {MockLink} from '../mocks'; | ||
|
||
import {executeOnce} from './utilities'; | ||
|
||
const petQuery = gql` | ||
query Pet { | ||
pets { | ||
...CatInfo | ||
} | ||
} | ||
fragment CatInfo on Cat { | ||
name | ||
} | ||
`; | ||
|
||
describe('MockLink', () => { | ||
it('returns a result when there is an object matching an operation', async () => { | ||
const data = {pets: [{name: 'Spike'}]}; | ||
const link = new MockLink({Pet: data}); | ||
const {result} = await executeOnce(link, petQuery); | ||
expect(result).toEqual({data}); | ||
}); | ||
|
||
it('returns a result when there is an function matching an operation', async () => { | ||
const data = {pets: [{name: 'Spike'}]}; | ||
const spy = jest.fn(() => data); | ||
const link = new MockLink({Pet: spy}); | ||
const {result} = await executeOnce(link, petQuery); | ||
|
||
expect(result).toEqual({data}); | ||
expect(spy).toHaveBeenCalledWith( | ||
expect.objectContaining({query: petQuery}), | ||
); | ||
}); | ||
|
||
it('returns an error message when the mock looks like a fixture', async () => { | ||
const link = new MockLink({}); | ||
const {result} = await executeOnce(link, petQuery); | ||
|
||
expect(result).toMatchObject( | ||
new Error( | ||
"Can’t perform GraphQL operation 'Pet' because no valid mocks were found (it looks like you tried to provide data directly to the mock GraphQL client. You need to provide your fixture on the key that matches its operation name. To fix this, simply change your code to read 'mockGraphQLClient({Pet: yourFixture}).'", | ||
), | ||
); | ||
}); | ||
|
||
it('returns an error message when there are no matching mocks', async () => { | ||
const link = new MockLink({LostPets: {}, PetsForSale: {}}); | ||
const {result} = await executeOnce(link, petQuery); | ||
|
||
expect(result).toMatchObject( | ||
new Error( | ||
"Can’t perform GraphQL operation 'Pet' because no valid mocks were found (you provided an object that had mocks only for the following operations: LostPets, PetsForSale).", | ||
), | ||
); | ||
}); | ||
|
||
it('returns an error message when no fixture is returned from a mock', async () => { | ||
const link = new MockLink(() => (null as unknown) as MockGraphQLResponse); | ||
|
||
const {result} = await executeOnce(link, petQuery); | ||
|
||
expect(result).toMatchObject( | ||
new Error( | ||
"Can’t perform GraphQL operation 'Pet' because no valid mocks were found (you provided a function that did not return a valid mock result)", | ||
), | ||
); | ||
}); | ||
}); |
File renamed without changes.
36 changes: 0 additions & 36 deletions
36
packages/graphql-testing/src/test/MemoryApolloClient.test.ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.