Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Commit

Permalink
Add http-fetcher tests for coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Evans Hauser committed Jun 15, 2017
1 parent 8ce09a0 commit 56fe476
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/httpFetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { print } from 'graphql';
import { DocumentNode, DefinitionNode, OperationDefinitionNode } from 'graphql/language/ast';
import 'isomorphic-fetch';

class HttpObservable implements Observable {
export class HttpObservable implements Observable {
private _request: () => Promise<Response>;
private subscribers: Array<Subscriber<FetchResult>>;
private state: State;
Expand Down
21 changes: 20 additions & 1 deletion tests/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { assert } from 'chai';
import * as sinon from 'sinon';
import HttpFetcher from '../src/httpFetcher';
import { HttpObservable } from '../src/httpFetcher';
import { State } from '../src/types';
import gql from 'graphql-tag';
import * as fetchMock from 'fetch-mock';

Expand All @@ -27,7 +29,11 @@ describe('HttpFetcher', () => {
operationName: 'SampleQuery',
});
//observableToPromise return a promise
observable.subscribe(next, (error) => assert(false), () => { assert(next.calledOnce); done(); });
observable.subscribe({
next,
error: (error) => assert(false),
complete: () => { assert(next.calledOnce); done(); },
});

fetchMock.restore();
});
Expand All @@ -53,6 +59,19 @@ describe('HttpFetcher', () => {
fetchMock.restore();
});

it('changes status to STOPPED after stop call', () => {
const mockError = {throws: new TypeError('mock me')};
fetchMock.get('*', mockError);
const fetcher = new HttpFetcher('', fetch);
const observable = <HttpObservable>fetcher.request({
query: sampleQuery,
operationName: 'SampleQuery',
});
observable.stop();
assert.equal(observable.status().state, State.STOPPED);
assert.equal(observable.status().numberSubscribers, 0);
});

//future tests:
//complain with unknown args to request
//http-fetcher calls fetch with proper arguments
Expand Down

0 comments on commit 56fe476

Please sign in to comment.