Skip to content

Commit

Permalink
add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
helfer authored and Sashko Stubailo committed Oct 15, 2016
1 parent 6be9b13 commit 9f0a06f
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions test/graphqlSubscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import {
assert,
} from 'chai';

import clonedeep = require('lodash.clonedeep');

import { isSubscriptionResultAction } from '../src/actions';

import ApolloClient from '../src';

import gql from 'graphql-tag';
Expand Down Expand Up @@ -242,6 +246,69 @@ describe('GraphQL Subscriptions', () => {
}
});

it('should fire redux action and call result reducers', (done) => {
const query = gql`
query miniQuery {
number
}
`;

const res = {
data: {
number: 0,
},
};

const req1 = {
request: { query },
result: res,
};

const network = mockSubscriptionNetworkInterface([sub1], req1);
let numResults = 0;
let counter = 0;
const queryManager = new QueryManager({
networkInterface: network,
reduxRootSelector: (state: any) => state.apollo,
store: createApolloStore(),
});

const observableQuery = queryManager.watchQuery({
query,
reducer: (previousResult, action) => {
counter++;
if (isSubscriptionResultAction(action)) {
const newResult = clonedeep(previousResult) as any;
newResult.number++;
return newResult;
}
return previousResult;
},
}).subscribe({
next: () => null,
});

const sub = queryManager.startGraphQLSubscription(realOptions).subscribe({
next(result) {
assert.deepEqual(result, results[numResults].result);
numResults++;
if (numResults === 4) {
// once for itself, four times for the subscription results.
observableQuery.unsubscribe();
assert.equal(counter, 5);
assert.equal(queryManager.store.getState()['apollo']['data']['ROOT_QUERY']['number'], 4);
done();
}
},
}) as any;

const id = sub._networkSubscriptionId;

for (let i = 0; i < 4; i++) {
network.fireResult(id);
}
});

// it('should work with an observable query', (done) => {
// const network = mockSubscriptionNetworkInterface([sub2], {
// request: {
Expand Down

0 comments on commit 9f0a06f

Please sign in to comment.