mock-apollo-client
Advanced tools
Comparing version 0.6.0 to 0.7.0
@@ -0,1 +1,7 @@ | ||
# [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) | ||
@@ -2,0 +8,0 @@ |
export * from './mockClient'; | ||
export { createMockSubscription, IMockSubscription } from './mockSubscription'; |
@@ -7,1 +7,3 @@ "use strict"; | ||
__export(require("./mockClient")); | ||
var mockSubscription_1 = require("./mockSubscription"); | ||
exports.createMockSubscription = mockSubscription_1.createMockSubscription; |
import ApolloClient, { ApolloClientOptions } from 'apollo-client'; | ||
import { NormalizedCacheObject } from 'apollo-cache-inmemory'; | ||
import { DocumentNode } from 'apollo-link'; | ||
export declare type RequestHandler<TData = any, TVariables = any> = (variables: TVariables) => Promise<RequestHandlerResponse<TData>>; | ||
import { IMockSubscription } from './mockSubscription'; | ||
export declare type RequestHandler<TData = any, TVariables = any> = (variables: TVariables) => Promise<RequestHandlerResponse<TData>> | IMockSubscription<TData>; | ||
export declare type RequestHandlerResponse<T> = { | ||
data: T; | ||
errors?: any[]; | ||
} | { | ||
errors: any[]; | ||
}; | ||
@@ -9,0 +11,0 @@ export declare type MockApolloClient = ApolloClient<NormalizedCacheObject> & { |
@@ -20,2 +20,3 @@ "use strict"; | ||
var visitor_1 = require("graphql/language/visitor"); | ||
var mockSubscription_1 = require("./mockSubscription"); | ||
var MockLink = (function (_super) { | ||
@@ -41,21 +42,26 @@ __extends(MockLink, _super); | ||
} | ||
var resultPromise = undefined; | ||
try { | ||
resultPromise = handler(operation.variables); | ||
} | ||
catch (error) { | ||
throw new Error("Unexpected error whilst calling request handler: " + error.message); | ||
} | ||
if (!isPromise(resultPromise)) { | ||
throw new Error("Request handler must return a promise. Received '" + typeof resultPromise + "'."); | ||
} | ||
return new apollo_link_1.Observable(function (observer) { | ||
resultPromise | ||
.then(function (result) { | ||
observer.next(result); | ||
observer.complete(); | ||
}) | ||
.catch(function (error) { | ||
observer.error(error); | ||
}); | ||
var result = undefined; | ||
try { | ||
result = handler(operation.variables); | ||
} | ||
catch (error) { | ||
throw new Error("Unexpected error whilst calling request handler: " + error.message); | ||
} | ||
if (isPromise(result)) { | ||
result | ||
.then(function (result) { | ||
observer.next(result); | ||
observer.complete(); | ||
}) | ||
.catch(function (error) { | ||
observer.error(error); | ||
}); | ||
} | ||
else if (isSubscription(result)) { | ||
result.subscribe(observer); | ||
} | ||
else { | ||
throw new Error("Request handler must return a promise or subscription. Received '" + typeof result + "'."); | ||
} | ||
return function () { }; | ||
@@ -102,1 +108,4 @@ }); | ||
}; | ||
var isSubscription = function (maybeSubscription) { | ||
return maybeSubscription && maybeSubscription instanceof mockSubscription_1.MockSubscription; | ||
}; |
{ | ||
"name": "mock-apollo-client", | ||
"version": "0.6.0", | ||
"version": "0.7.0", | ||
"description": "Library to help unit testing when using apollo-client", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
123
README.md
@@ -5,7 +5,13 @@ # Mock Apollo Client | ||
# Versions | ||
## Versions | ||
Version 0.x of this library is compatible with Apollo client 2 | ||
```bash | ||
npm install --save-dev mock-apollo-client@apollo2 | ||
``` | ||
Version 1.x of this library is compatible with Apollo client 3 | ||
```bash | ||
npm install --save-dev mock-apollo-client | ||
``` | ||
@@ -28,3 +34,3 @@ ## Motivation | ||
```bash | ||
npm install --save-dev mock-apollo-client | ||
npm install --save-dev mock-apollo-client@apollo2 | ||
``` | ||
@@ -64,4 +70,4 @@ | ||
{({ loading, error, data }) => { | ||
if (loading) return 'Loading...'; | ||
if (error) return 'Error!'; | ||
if (loading) return <p>Loading...</p>; | ||
if (error) return <p>Error!</p>; | ||
@@ -160,2 +166,111 @@ return ( | ||
### Subscriptions | ||
Subscriptions can be tested, but require a different setup as they receive a stream of data. | ||
Consider the file below, which contains a single subscription and a component which is responsible for rendering the updated data: | ||
```tsx | ||
// dogSubscription.tsx | ||
import * as React from 'react'; | ||
import gql from 'graphql-tag'; | ||
import { Subscription } from 'react-apollo'; | ||
export const SUBSCRIBE_DOG_DOCUMENT = gql` | ||
subscription subscribeDog($name: String) { | ||
dog(name: $name) { | ||
id | ||
name | ||
numberOfBarks | ||
} | ||
} | ||
`; | ||
export const DogSubscription = ({ name }) => ( | ||
<Subscription subscription={SUBSCRIBE_DOG_DOCUMENT} variables={{ name }}> | ||
{({ loading, error, data }) => { | ||
if (loading) return <p>Loading...</p>; | ||
if (error) return <p>Error!</p>; | ||
return ( | ||
<p> | ||
{data.dog.name} has barked {data.dog.numberOfBarks} time(s) | ||
</p> | ||
); | ||
}} | ||
</Subscription> | ||
); | ||
``` | ||
To unit test this component using `mock-apollo-client`, the test file could look like the following: | ||
```tsx | ||
// dogSubscription.test.tsx | ||
import { mount, ReactWrapper } from 'enzyme'; | ||
import * as React from 'react'; | ||
import { ApolloProvider } from 'react-apollo'; | ||
import { createMockClient, createMockSubscription, IMockSubscription } from 'mock-apollo-client'; | ||
import { SUBSCRIBE_DOG_DOCUMENT, DogSubscription } from './dogSubscription'; | ||
let wrapper: ReactWrapper; | ||
let mockSubscription: IMockSubscription; | ||
beforeEach(() => { | ||
const mockClient = createMockClient(); | ||
mockSubscription = createMockSubscription(); | ||
mockClient.setRequestHandler( | ||
SUBSCRIBE_DOG_DOCUMENT, | ||
() => mockSubscription); | ||
wrapper = mount( | ||
<ApolloProvider client={mockClient}> | ||
<DogSubscription name="Rufus" /> | ||
</ApolloProvider> | ||
); | ||
}); | ||
it('renders the dog details', () => { | ||
mockSubscription.next({ data: { dog: { id: 1, name: 'Rufus', numberOfBarks: 0 } } }); | ||
wrapper.update(); | ||
expect(wrapper.text()).toContain('Rufus has barked 0 time(s)'); | ||
mockSubscription.next({ data: { dog: { id: 1, name: 'Rufus', numberOfBarks: 1 } } }); | ||
wrapper.update(); | ||
expect(wrapper.text()).toContain('Rufus has barked 1 time(s)'); | ||
}); | ||
``` | ||
The subscription can be closed by calling `.complete` if necessary for the test. | ||
#### Errors | ||
You can also test error states by calling `.error` on the `mockSubscription` and passing errors as described in [Error States](#error-states): | ||
```typescript | ||
mockSubscription.error(new Error('GraphQL Network Error')) | ||
``` | ||
#### Multiple subscriptions | ||
A mock subscription will only be associated with a single invocation of a query. If a component is subscribing to the same query multiple times, then a separate mock subscription should be used for each one. | ||
```typescript | ||
const subscriptions: IMockSubscription[] = []; | ||
mockClient.setRequestHandler( | ||
SUBSCRIBE_DOG_DOCUMENT, | ||
() => { | ||
const subscription = createMockSubscription(); | ||
subscriptions.push(subscription); | ||
return subscription; | ||
}); | ||
... | ||
subscriptions.forEach((s) => s.next({ data: { dog: { id: 1, name: 'Rufus', numberOfBarks: 1 } } })); | ||
``` | ||
### Specifying Apollo client options | ||
@@ -162,0 +277,0 @@ |
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
24896
12
263
335