1 | import gql from 'graphql-tag';
|
2 | import { ApolloClient } from 'apollo-client';
|
3 | import { InMemoryCache } from 'apollo-cache-inmemory';
|
4 | import { Observable, ApolloLink, execute } from 'apollo-link';
|
5 |
|
6 | import { withClientState } from '../';
|
7 |
|
8 | describe('initialization', () => {
|
9 | const resolvers = { Query: { foo: () => ({ bar: true }) } };
|
10 | const defaults = { foo: { bar: false, __typename: 'Bar' } };
|
11 | const query = gql`
|
12 | {
|
13 | foo @client {
|
14 | bar
|
15 | }
|
16 | }
|
17 | `;
|
18 |
|
19 | const remoteQuery = gql`
|
20 | {
|
21 | foo {
|
22 | bar
|
23 | }
|
24 | }
|
25 | `;
|
26 |
|
27 | const typeDefs = `
|
28 | type Todo {
|
29 | id: String
|
30 | message: String!
|
31 | }
|
32 |
|
33 | type Query {
|
34 | todo(id: String!): Todo
|
35 | }
|
36 | `;
|
37 |
|
38 | it('writes defaults to the cache upon initialization', () => {
|
39 | const cache = new InMemoryCache();
|
40 |
|
41 | withClientState({ cache, resolvers, defaults });
|
42 | expect(cache.extract()).toMatchSnapshot();
|
43 | });
|
44 |
|
45 | it(`doesn't call the resolver if the data is already in the cache`, () => {
|
46 | const fooResolver = jest.fn();
|
47 | const resolvers = { Query: { foo: fooResolver } };
|
48 |
|
49 | const cache = new InMemoryCache();
|
50 | const client = new ApolloClient({
|
51 | cache,
|
52 | link: withClientState({ cache, resolvers, defaults }),
|
53 | });
|
54 |
|
55 | client
|
56 | .query({ query })
|
57 | .then(({ data }) => {
|
58 | expect(fooResolver).not.toHaveBeenCalled();
|
59 | expect(data.foo.bar).toEqual(false);
|
60 | })
|
61 | .catch(e => console.error(e));
|
62 | });
|
63 |
|
64 | it('adds a schema string in SDL format to the context as definition if typeDefs are passed in', done => {
|
65 | const nextLink = new ApolloLink(operation => {
|
66 | const { schemas } = operation.getContext();
|
67 | expect(schemas).toMatchSnapshot();
|
68 | return Observable.of({
|
69 | data: { foo: { bar: true, __typename: 'Bar' } },
|
70 | });
|
71 | });
|
72 |
|
73 | const client = withClientState({ resolvers, defaults, typeDefs });
|
74 |
|
75 | execute(client.concat(nextLink), {
|
76 | query: remoteQuery,
|
77 | }).subscribe(() => done(), done.fail);
|
78 | });
|
79 |
|
80 | it('concatenates schema strings if typeDefs are passed in as an array', done => {
|
81 | const anotherSchema = `
|
82 | type Foo {
|
83 | foo: String!
|
84 | bar: String
|
85 | }
|
86 | `;
|
87 |
|
88 | const nextLink = new ApolloLink(operation => {
|
89 | const { schemas } = operation.getContext();
|
90 | expect(schemas).toMatchSnapshot();
|
91 | return Observable.of({
|
92 | data: { foo: { bar: true, __typename: 'Bar' } },
|
93 | });
|
94 | });
|
95 |
|
96 | const client = withClientState({
|
97 | resolvers,
|
98 | defaults,
|
99 | typeDefs: [typeDefs, anotherSchema],
|
100 | });
|
101 |
|
102 | execute(client.concat(nextLink), {
|
103 | query: remoteQuery,
|
104 | }).subscribe(() => done(), done.fail);
|
105 | });
|
106 | });
|