|
| 1 | +const { ApolloClient } = require('apollo-client'); |
| 2 | +const { SchemaLink } = require('apollo-link-schema'); |
| 3 | +const { makeExecutableSchema } = require('graphql-tools'); |
| 4 | +const gql = require('graphql-tag'); |
| 5 | + |
| 6 | +const { ApolloCacheLite } = require('../src/ApolloCacheLite'); |
| 7 | +const resolvers = require('./test-utils/resolvers'); |
| 8 | +const typeDefs = require('./test-utils/type-defs'); |
| 9 | + |
| 10 | +function createApolloClient() { |
| 11 | + const schema = makeExecutableSchema({ typeDefs, resolvers }); |
| 12 | + |
| 13 | + return new ApolloClient({ |
| 14 | + cache: new ApolloCacheLite(), |
| 15 | + link: new SchemaLink({ schema }) |
| 16 | + }); |
| 17 | +} |
| 18 | + |
| 19 | +describe('ApolloCacheLite', () => { |
| 20 | + it('should export a class', () => { |
| 21 | + expect(ApolloCacheLite).toBeDefined(); |
| 22 | + expect(new ApolloCacheLite()).toBeInstanceOf(ApolloCacheLite); |
| 23 | + }); |
| 24 | + |
| 25 | + it('client.query() / cache.write() :: should store query and cacheable nodes', async () => { |
| 26 | + const client = createApolloClient(); |
| 27 | + |
| 28 | + const { data } = await client.query({ |
| 29 | + query: gql(`{ |
| 30 | + getBook(id: 1) { |
| 31 | + id |
| 32 | + title |
| 33 | + author { |
| 34 | + id |
| 35 | + name |
| 36 | + } |
| 37 | + } |
| 38 | + }`) |
| 39 | + }); |
| 40 | + |
| 41 | + const { queries, store } = client.cache.extract(); |
| 42 | + |
| 43 | + expect(data).toEqual({ |
| 44 | + getBook: { |
| 45 | + __typename: 'Book', |
| 46 | + id: '1', |
| 47 | + title: 'Test book', |
| 48 | + author: { |
| 49 | + __typename: 'Author', |
| 50 | + id: '1', |
| 51 | + name: 'D' |
| 52 | + } |
| 53 | + } |
| 54 | + }); |
| 55 | + |
| 56 | + expect(queries).toEqual({ '-702561878___3938': data }); |
| 57 | + |
| 58 | + expect(store).toEqual({ |
| 59 | + Author: { |
| 60 | + '1': { |
| 61 | + __typename: 'Author', |
| 62 | + id: '1', |
| 63 | + name: 'D' |
| 64 | + } |
| 65 | + }, |
| 66 | + Book: { |
| 67 | + '1': { |
| 68 | + __typename: 'Book', |
| 69 | + id: '1', |
| 70 | + title: 'Test book', |
| 71 | + author: { |
| 72 | + __typename: 'Author', |
| 73 | + id: '1', |
| 74 | + name: 'D' |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + it('client.readQuery() / cache.read() :: should return cached result for a query', async () => { |
| 82 | + const client = createApolloClient(); |
| 83 | + const options = { |
| 84 | + query: gql(`{ |
| 85 | + getBook(id: 1) { |
| 86 | + id |
| 87 | + title |
| 88 | + author { |
| 89 | + id |
| 90 | + name |
| 91 | + } |
| 92 | + } |
| 93 | + }`) |
| 94 | + }; |
| 95 | + |
| 96 | + // calling client.readQuery() with cold cache should return undefined |
| 97 | + expect(await client.readQuery(options)).not.toBeDefined(); |
| 98 | + |
| 99 | + // cache warmup |
| 100 | + await client.query(options); |
| 101 | + |
| 102 | + // test client.readQuery() with warm cache |
| 103 | + const data = await client.readQuery(options); |
| 104 | + const { queries, store } = client.cache.extract(); |
| 105 | + |
| 106 | + expect(data).toEqual({ |
| 107 | + getBook: { |
| 108 | + __typename: 'Book', |
| 109 | + id: '1', |
| 110 | + title: 'Test book', |
| 111 | + author: { |
| 112 | + __typename: 'Author', |
| 113 | + id: '1', |
| 114 | + name: 'D' |
| 115 | + } |
| 116 | + } |
| 117 | + }); |
| 118 | + |
| 119 | + expect(queries).toEqual({ '-702561878___3938': data }); |
| 120 | + |
| 121 | + expect(store).toEqual({ |
| 122 | + Author: { |
| 123 | + '1': { |
| 124 | + __typename: 'Author', |
| 125 | + id: '1', |
| 126 | + name: 'D' |
| 127 | + } |
| 128 | + }, |
| 129 | + Book: { |
| 130 | + '1': { |
| 131 | + __typename: 'Book', |
| 132 | + id: '1', |
| 133 | + title: 'Test book', |
| 134 | + author: { |
| 135 | + __typename: 'Author', |
| 136 | + id: '1', |
| 137 | + name: 'D' |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + }); |
| 142 | + }); |
| 143 | + |
| 144 | + it('client.readFragment() / cache.readFragment() :: should return cached node', async () => { |
| 145 | + const client = createApolloClient(); |
| 146 | + |
| 147 | + // cache warmup |
| 148 | + await client.query({ |
| 149 | + query: gql(`{ |
| 150 | + getBook(id: 1) { |
| 151 | + id |
| 152 | + title |
| 153 | + author { |
| 154 | + id |
| 155 | + name |
| 156 | + } |
| 157 | + } |
| 158 | + }`) |
| 159 | + }); |
| 160 | + |
| 161 | + // test readFragment() with warm cache |
| 162 | + const data = client.readFragment({ |
| 163 | + id: 'Book:1', |
| 164 | + fragment: ` |
| 165 | + fragment fullBookDetails on Book { |
| 166 | + id |
| 167 | + title |
| 168 | + author { |
| 169 | + id |
| 170 | + name |
| 171 | + } |
| 172 | + } |
| 173 | + ` |
| 174 | + }); |
| 175 | + |
| 176 | + expect(data).toEqual({ |
| 177 | + __typename: 'Book', |
| 178 | + id: '1', |
| 179 | + title: 'Test book', |
| 180 | + author: { |
| 181 | + __typename: 'Author', |
| 182 | + id: '1', |
| 183 | + name: 'D' |
| 184 | + } |
| 185 | + }); |
| 186 | + }); |
| 187 | +}); |
0 commit comments