From 9126d1bc830bbd2d63db2d8bdc27b373db3ab634 Mon Sep 17 00:00:00 2001 From: James Baxley Date: Tue, 20 Feb 2018 20:49:10 -0500 Subject: [PATCH] added test for retry link --- package.json | 6 ++-- src/__tests__/react.tsx | 2 -- src/__tests__/retry.ts | 73 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 src/__tests__/retry.ts diff --git a/package.json b/package.json index f431713..bafe72f 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,7 @@ "watch": "tsc -w -p ." }, "dependencies": { - "apollo-link": "^1.0.3", + "apollo-link": "^1.1.0", "hash.js": "^1.1.3" }, "peerDependencies": { @@ -54,6 +54,7 @@ "apollo-cache-inmemory": "^1.1.4", "apollo-client": "^2.0.4", "apollo-link-http": "1.2.0", + "apollo-link-retry": "^2.1.2", "browserify": "14.5.0", "bundlesize": "0.15.3", "codecov": "3.0.0", @@ -75,7 +76,8 @@ "rollup": "0.45.2", "ts-jest": "21.1.4", "typescript": "2.5.1", - "uglify-js": "3.1.5" + "uglify-js": "3.1.5", + "wait-for-observables": "^1.0.3" }, "jest": { "transform": { diff --git a/src/__tests__/react.tsx b/src/__tests__/react.tsx index 6820223..0c1ea12 100644 --- a/src/__tests__/react.tsx +++ b/src/__tests__/react.tsx @@ -8,8 +8,6 @@ import { createHttpLink } from 'apollo-link-http'; import { print, parse } from 'graphql'; import { sha256 } from 'js-sha256'; -import { data, response, shortHash as hash } from './'; - export const query = gql` query Test($filter: FilterObject) { foo(filter: $filter) { diff --git a/src/__tests__/retry.ts b/src/__tests__/retry.ts new file mode 100644 index 0000000..3255c6a --- /dev/null +++ b/src/__tests__/retry.ts @@ -0,0 +1,73 @@ +import gql from 'graphql-tag'; +import { execute, ApolloLink, Observable, FetchResult } from 'apollo-link'; +import waitFor from 'wait-for-observables'; +import { RetryLink } from 'apollo-link-retry'; +import { createHttpLink } from 'apollo-link-http'; + +import { createPersistedQueryLink } from '../'; + +export const query = gql` + query Test($id: ID!) { + foo(id: $id) { + bar + } + } +`; + +export const variables = { id: 1 }; +const standardError = new Error('I never work'); + +export const data = { + foo: { bar: true }, +}; +export const response = JSON.stringify({ data }); +const errors = [{ message: 'PersistedQueryNotFound' }]; +const giveUpErrors = [{ message: 'PersistedQueryNotSupported' }]; +const multipleErrors = [...errors, { message: 'not logged in' }]; +const errorResponse = JSON.stringify({ errors }); +const giveUpResponse = JSON.stringify({ errors: giveUpErrors }); +const multiResponse = JSON.stringify({ errors: multipleErrors }); + +const fromError = (errorValue: any) => { + return new Observable(observer => { + observer.error(errorValue); + }); +}; + +describe('RetryLink', () => { + beforeEach(fetch.mockReset); + it('correctly allows retry link integration', done => { + fetch.mockResponseOnce(errorResponse); + fetch.mockResponseOnce(response); + + let count = 0; + const fetcher = (...args) => { + count++; + const [_, payload] = args; + const body = JSON.parse(payload.body); + // on the first fetch, we return not found + if (count === 1) expect(body.query).toBeUndefined(); + // on the second, a critical error (retried from persisted query with query in payload) + if (count === 2) { + expect(body.query).toBeDefined(); + return Promise.reject(new Error('server error')); + } + // on the third (retried with query in the payload), we return good data + if (count === 3) expect(body.query).toBeDefined(); + if (count > 3) done.fail('fetch called too many times'); + return fetch(...args); + }; + + const retry = new RetryLink(); + const persist = createPersistedQueryLink(); + const http = createHttpLink({ fetch: fetcher }); + + const link = ApolloLink.from([persist, retry, http]); + + execute(link, { query, variables }).subscribe(result => { + expect(result.data).toEqual(data); + expect(count).toEqual(3); + done(); + }, done.fail); + }); +});