mock-apollo-client
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -0,1 +1,7 @@ | ||
# [1.2.0](https://github.com/Mike-Gibson/mock-apollo-client/releases/tag/v1.2.0) (2021-08-10) | ||
### Features | ||
* Handle fragments [#24](https://github.com/Mike-Gibson/mock-apollo-client/issues/31) | ||
# [1.1.0](https://github.com/Mike-Gibson/mock-apollo-client/releases/tag/v1.1.0) (2021-04-10) | ||
@@ -22,2 +28,8 @@ | ||
# [0.7.0](https://github.com/Mike-Gibson/mock-apollo-client/releases/tag/v0.7.0) (2021-04-27) | ||
### Features | ||
* Support subscriptions [#28](https://github.com/Mike-Gibson/mock-apollo-client/issues/28) | ||
# [0.6.0](https://github.com/Mike-Gibson/mock-apollo-client/releases/tag/v0.6.0) (2021-04-01) | ||
@@ -24,0 +36,0 @@ |
@@ -76,4 +76,23 @@ "use strict"; | ||
exports.MockLink = MockLink; | ||
var normalise = function (requestQuery) { | ||
var stripped = utilities_1.removeConnectionDirectiveFromDocument(requestQuery); | ||
stripped = stripped !== null | ||
? stripTypenames(stripped) | ||
: null; | ||
return stripped === null | ||
? requestQuery | ||
: stripped; | ||
}; | ||
var stripTypenames = function (document) { | ||
return graphql_1.visit(document, { | ||
Field: { | ||
enter: function (node) { return node.name.value === '__typename' | ||
? null | ||
: undefined; }, | ||
}, | ||
}); | ||
}; | ||
var requestToKey = function (query) { | ||
var queryString = query && graphql_1.print(query); | ||
var normalised = normalise(query); | ||
var queryString = query && graphql_1.print(normalised); | ||
var requestKey = { query: queryString }; | ||
@@ -80,0 +99,0 @@ return JSON.stringify(requestKey); |
{ | ||
"name": "mock-apollo-client", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Library to help unit testing when using apollo-client", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -280,1 +280,48 @@ # Mock Apollo Client | ||
Note: it is not possible to specify the `link` to use as this is how `mock-apollo-client` injects its behaviour. | ||
### Fragments | ||
If your queries or mutations use fragments against union or interface types, you must inject a cache object when creating the mock client which has been provided with `possibleTypes`, and also include the correct `__typename` field when mocking the response. | ||
For example: | ||
```typescript | ||
import { InMemoryCache } from '@apollo/client'; | ||
import { createMockClient } from 'mock-apollo-client'; | ||
const cache = new InMemoryCache({ | ||
possibleTypes: { | ||
Hardware: ['Memory', 'Cpu'], | ||
}, | ||
}); | ||
const mockClient = createMockClient({ cache }); | ||
``` | ||
You must then ensure that the query result includes the `__typename` field as it would when calling your actual GraphQL API. This is to ensure that the fragment matching works as expected: | ||
```typescript | ||
const query = gql` | ||
query Hardware { | ||
hardware { | ||
id | ||
... on Memory { | ||
size | ||
} | ||
... on Cpu { | ||
speed | ||
} | ||
} | ||
} | ||
`; | ||
const mockData = { | ||
hardware: { | ||
__typename: 'Memory', | ||
id: 2, | ||
size: '16gb', | ||
}, | ||
}; | ||
const requestHandler = jest.fn().mockResolvedValue({ data: mockData }); | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25808
256
327