Skip to content
This repository has been archived by the owner on Jan 27, 2022. It is now read-only.

added test for retry link #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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",
Expand All @@ -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": {
Expand Down
2 changes: 0 additions & 2 deletions src/__tests__/react.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
73 changes: 73 additions & 0 deletions src/__tests__/retry.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});