Skip to content

Commit

Permalink
Subscripitons unit tests and subscribe method calls correctly to handler
Browse files Browse the repository at this point in the history
  • Loading branch information
evans committed Aug 9, 2017
1 parent a3aa89c commit 700cb68
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
1 change: 1 addition & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
{
"name": "Test",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
"stopOnEntry": false,
"args": [
Expand Down
4 changes: 3 additions & 1 deletion src/ApolloClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,9 @@ export default class ApolloClient implements DataProxy {
networkInterface as ApolloLink,
request,
) as any).subscribe({
next: (data: FetchResult) => handler(data.errors, data.data),
next: (data: FetchResult) => handler(undefined, data),
error: (error: Error) => handler([error]),
complete: handler,
});

const id = count.toString();
Expand Down
81 changes: 81 additions & 0 deletions test/graphqlSubscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ import { QueryManager } from '../src/core/QueryManager';

import { createApolloStore } from '../src/store';

import { SubscriptionOptions } from '../src/core/watchQueryOptions';

import { ApolloLink, Observable, FetchResult } from 'apollo-link-core';

describe('GraphQL Subscriptions', () => {
const results = [
'Dahivat Pandya',
Expand Down Expand Up @@ -205,6 +209,83 @@ describe('GraphQL Subscriptions', () => {
}
});

it('should receive multiple results for a subscription with Apollo Link', done => {
let numResults = 0;
const expectedData = [
'Dahivat Pandya',
'Vyacheslav Kim',
'Changping Chen',
'Amanda Liu',
].map(name => ({ user: { name: name } }));
const queryInfo = {
request: defaultSub1.request || ({} as any),
results: [...expectedData],
};

const link = ApolloLink.from([
operation =>
new Observable(observer => {
if (queryInfo.results) {
queryInfo.results.map(result =>
observer.next(result as FetchResult),
);
}
}),
]);
const client = new ApolloClient({
networkInterface: link,
addTypename: false,
});

const obs = client.subscribe(queryInfo.request as SubscriptionOptions);
const sub = obs.subscribe({
next: data => {
const expected = expectedData.shift();
if (expected) {
assert.equal(data, expected);
} else {
assert(false);
}
if (expectedData.length === 0) {
done();
}
},
error: console.log,
complete: () => assert(false),
});
});

it('should unsubscribe properly with Apollo Link', () => {
let numResults = 0;
const readyToUnsub: boolean[] = [];

const link = ApolloLink.from([
operation =>
new Observable(observer => {
const index = readyToUnsub.length;
readyToUnsub.push(false);
return () => {
assert(readyToUnsub[index]);
};
}),
]);
const client = new ApolloClient({
networkInterface: link,
addTypename: false,
});

const obs0 = client.subscribe(defaultSub1.request as SubscriptionOptions);
const obs1 = client.subscribe(defaultSub1.request as SubscriptionOptions);
const subscription0 = obs0.subscribe({});
const subscription1 = obs1.subscribe({});

readyToUnsub[1] = true;
subscription1.unsubscribe();
readyToUnsub[0] = true;
subscription0.unsubscribe();
readyToUnsub.map(bool => assert(bool));
});

it('should fire redux action and call result reducers', done => {
const query = gql`
query miniQuery {
Expand Down

0 comments on commit 700cb68

Please sign in to comment.