Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Added custom cache option to MockedProvider #2142

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change log

## 2.1.8 (June 28, 2018)

- Allow a custom `Cache` to be passed into `MockedProvider` via props.
[@2142](https://github.com/JakeDawkins) in [#2142](https://github.com/apollographql/react-apollo/pull/2142)

## 2.1.7 (June 27, 2018)

- The `ApolloProvider` `children` prop type has been changed from `element`
Expand Down
2 changes: 1 addition & 1 deletion src/test-utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class MockedProvider extends React.Component<any, any> {
const link = new MockLink(this.props.mocks, this.props.addTypename);
this.client = new ApolloClient({
link,
cache: new Cache({ addTypename: this.props.addTypename }),
cache: this.props.cache || new Cache({ addTypename: this.props.addTypename }),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JakeDawkins should we warn if you have a mismatch from addTypename as a prop vs in the cache?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about that, but how do you think we could do that if they can pass in arbitrary caches?

defaultOptions: this.props.defaultOptions,
});
}
Expand Down
29 changes: 29 additions & 0 deletions test/test-utils.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,32 @@ it('errors if the query in the mock and component do not match', done => {
</MockedProvider>,
);
});

it('allows a custom cache to be used for the MockedProvider', done => {
const customCache = new InMemoryCache();
customCache.writeQuery({ query, variables, data: { user: { ...user, id: 'custom' } } });

class Container extends React.Component<ChildProps<Variables, Data, Variables>> {
componentWillReceiveProps(nextProps: ChildProps<Variables, Data, Variables>) {
expect(nextProps.data!.user!.id).toEqual('custom');
done();
}

render() {
return null;
}
}

const ContainerWithData = graphql<Variables, Data, Variables>(query, {
options: props => ({
variables: props,
fetchPolicy: 'cache-only',
}),
})(Container);

renderer.create(
<MockedProvider mocks={mocks} cache={customCache} addTypename={false}>
<ContainerWithData {...variables} />
</MockedProvider>,
);
});