|
1 | | -import { InMemoryCache } from 'apollo-cache-inmemory'; |
2 | | -import { ApolloClient } from 'apollo-client'; |
| 1 | +import {ApolloLink, execute, FetchResult, GraphQLRequest, Observable} from 'apollo-link'; |
3 | 2 | import {expect} from 'chai'; |
4 | 3 | import { parse } from 'graphql'; |
| 4 | +import gql from 'graphql-tag'; |
5 | 5 | import sinon from 'sinon'; |
6 | | -import Client from 'slicknode-client'; |
7 | 6 | import SlicknodeLink from '../index'; |
8 | 7 |
|
9 | 8 | describe('SlicknodeLink', () => { |
10 | | - it('fails for missing configuration options', () => { |
11 | | - expect(() => { |
12 | | - const link = new SlicknodeLink({ |
13 | | - client: null, |
14 | | - }); |
15 | | - }).to.throw('No instance of a client provided in configuration options for SlicknodeLink'); |
16 | | - }); |
17 | | - |
18 | | - it('creates link successfully', () => { |
19 | | - const client = new Client({ |
20 | | - endpoint: 'https://localhost', |
21 | | - }); |
22 | | - const link = new SlicknodeLink({ |
23 | | - client, |
24 | | - }); |
25 | | - expect(link).to.not.equal(null); |
26 | | - }); |
27 | | - |
28 | | - it('sends and receives requests with apollo client', async () => { |
29 | | - const client = new Client({ |
30 | | - endpoint: 'https://localhost', |
31 | | - }); |
32 | | - const link = new SlicknodeLink({ |
33 | | - client, |
34 | | - }); |
35 | | - |
36 | | - const payload = { |
37 | | - data: { |
38 | | - value: 'test', |
39 | | - }, |
| 9 | + it('forwards request to next link', (done) => { |
| 10 | + const data = { |
| 11 | + test: true, |
40 | 12 | }; |
41 | | - sinon.stub(client, 'fetch').resolves(payload); |
42 | | - const apolloClient = new ApolloClient({ |
43 | | - link, |
44 | | - cache: new InMemoryCache(), |
45 | | - }); |
46 | | - |
47 | | - const result = await apolloClient.query({ |
48 | | - query: parse('{value}'), |
| 13 | + const slicknodeLink = ApolloLink.from([ |
| 14 | + new SlicknodeLink(), |
| 15 | + new ApolloLink((e) => { |
| 16 | + return new Observable<FetchResult>((observer) => { |
| 17 | + observer.next({data}); |
| 18 | + }); |
| 19 | + }), |
| 20 | + ]); |
| 21 | + const query = gql`{test}`; |
| 22 | + const request: GraphQLRequest = { |
| 23 | + query, |
| 24 | + variables: {}, |
| 25 | + }; |
| 26 | + const observable = execute(slicknodeLink, request); |
| 27 | + observable.subscribe({ |
| 28 | + next(result: FetchResult) { |
| 29 | + expect(result.data).to.equal(data); |
| 30 | + done(); |
| 31 | + }, |
| 32 | + error: done, |
49 | 33 | }); |
50 | | - |
51 | | - expect(result.data).to.deep.equal(payload.data); |
52 | 34 | }); |
53 | 35 |
|
54 | | - it('handles error with apollo client', async () => { |
55 | | - const client = new Client({ |
56 | | - endpoint: 'https://localhost', |
57 | | - }); |
58 | | - const link = new SlicknodeLink({ |
59 | | - client, |
| 36 | + it('forwards accessToken from options', (done) => { |
| 37 | + const data = { |
| 38 | + test: true, |
| 39 | + }; |
| 40 | + const accessToken = 'abc123'; |
| 41 | + const slicknodeLink = new SlicknodeLink({ |
| 42 | + accessToken, |
60 | 43 | }); |
61 | | - |
62 | | - const errorMessage = 'Error fetching data'; |
63 | | - sinon.stub(client, 'fetch').throws(errorMessage); |
64 | | - const apolloClient = new ApolloClient({ |
65 | | - link, |
66 | | - cache: new InMemoryCache(), |
| 44 | + const link = ApolloLink.from([ |
| 45 | + slicknodeLink, |
| 46 | + new ApolloLink((operation) => { |
| 47 | + expect(operation.getContext()).to.deep.equal({ |
| 48 | + headers: { |
| 49 | + Authorization: `Bearer ${accessToken}`, |
| 50 | + }, |
| 51 | + }); |
| 52 | + done(); |
| 53 | + return null; |
| 54 | + }), |
| 55 | + ]); |
| 56 | + const query = gql`{test}`; |
| 57 | + const request: GraphQLRequest = { |
| 58 | + query, |
| 59 | + variables: {}, |
| 60 | + }; |
| 61 | + const observable = execute(link, request); |
| 62 | + observable.subscribe({ |
| 63 | + next(result: FetchResult) { |
| 64 | + expect(result.data).to.equal(data); |
| 65 | + }, |
| 66 | + error: done, |
67 | 67 | }); |
68 | | - |
69 | | - try { |
70 | | - const result = await apolloClient.query({ |
71 | | - query: parse('{value}'), |
72 | | - }); |
73 | | - } catch (e) { |
74 | | - expect(e.networkError.name).to.deep.equal(errorMessage); |
75 | | - } |
76 | 68 | }); |
77 | 69 | }); |
0 commit comments