From 9f0a06f8b988a9d725e9ab0f48b4f267b4f2e477 Mon Sep 17 00:00:00 2001 From: Jonas Helfer Date: Fri, 14 Oct 2016 17:59:10 -0700 Subject: [PATCH] add a test --- test/graphqlSubscriptions.ts | 67 ++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/test/graphqlSubscriptions.ts b/test/graphqlSubscriptions.ts index c70ffae16f9..04663f9c79f 100644 --- a/test/graphqlSubscriptions.ts +++ b/test/graphqlSubscriptions.ts @@ -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'; @@ -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: {