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

test-utils: allow passing a custom cache object to MockedProvider #2254

Merged
merged 4 commits into from
Aug 22, 2018
Merged
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
3 changes: 3 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
to see if any query errors have occurred. A better (and supported) way to
check for errors is to use the result `errors` property. <br/>
[@TLadd](https://github.com/TLadd) in [#1983](https://github.com/apollographql/react-apollo/pull/1983)
- Allow a custom `cache` object to be passed into the test-utils
`MockedProvider`. <br/>
[@palmfjord](https://github.com/palmfjord) in [#2254](https://github.com/apollographql/react-apollo/pull/2254)

## 2.1.11 (August 9, 2018)

Expand Down
8 changes: 5 additions & 3 deletions src/test-utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import { InMemoryCache as Cache } from 'apollo-cache-inmemory';

import { ApolloProvider } from './index';
import { MockedResponse, MockLink } from './test-links';
import { ApolloCache } from 'apollo-cache';
export * from './test-links';

export interface MockedProviderProps {
export interface MockedProviderProps<TSerializedCache = {}> {
mocks?: MockedResponse[];
addTypename?: boolean;
defaultOptions?: DefaultOptions;
cache?: ApolloCache<TSerializedCache>;
}

export interface MockedProviderState {
Expand All @@ -25,9 +27,9 @@ export class MockedProvider extends React.Component<MockedProviderProps, MockedP
constructor(props: MockedProviderProps) {
super(props);

const { mocks, addTypename, defaultOptions } = this.props;
const { mocks, addTypename, defaultOptions, cache } = this.props;
const client = new ApolloClient({
cache: new Cache({ addTypename }),
cache: cache || new Cache({ addTypename }),
defaultOptions,
link: new MockLink(mocks || [], addTypename),
});
Expand Down
23 changes: 23 additions & 0 deletions test/test-utils.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,28 @@ it('allows for querying with the typename', done => {
);
});

it('allows for using a custom cache', done => {
const cache = new InMemoryCache();
cache.writeQuery({
query,
variables,
data: { user },
});

const Container: React.SFC<ChildProps<Variables, Data, Variables>> = props => {
expect(props.data).toMatchObject({ user });
done();

return null;
};
const ContainerWithData = withUser(Container);
renderer.create(
<MockedProvider mocks={[]} cache={cache}>
<ContainerWithData {...variables} />
</MockedProvider>,
);
});

it('errors if the variables in the mock and component do not match', done => {
class Container extends React.Component<ChildProps<Variables, Data, Variables>> {
componentWillReceiveProps(nextProps: ChildProps<Variables, Data, Variables>) {
Expand Down Expand Up @@ -317,6 +339,7 @@ it('doesnt crash on unmount if there is no query manager', () => {
return null;
}
}

renderer
.create(
<MockedProvider>
Expand Down