Skip to content

Commit

Permalink
Add tests for fetchData
Browse files Browse the repository at this point in the history
  • Loading branch information
jameshopkins committed Jan 28, 2017
1 parent 8307da7 commit cdd32b8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
"sanctuary": "^0.11.1"
},
"devDependencies": {
"babel-core": "^6.21.0",
"babel-cli": "^6.18.0",
"babel-core": "^6.21.0",
"babel-plugin-transform-es2015-modules-commonjs": "^6.18.0",
"babel-register": "^6.18.0",
"coveralls": "^2.11.15",
Expand Down
4 changes: 2 additions & 2 deletions src/polling/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const handleResponse = chain(res => {

const poll = compose(handleResponse, makeRequest());

export const pollRes = (rej, res, fetchData, url, data, timeout = setTimeout) => {
export const resolvePollRes = (rej, res, fetchData, url, data, timeout = setTimeout) => {
if (propEq('Status', 'UpdatesComplete', data)) {
console.log('Query result complete');
res(data);
Expand All @@ -23,7 +23,7 @@ export const pollRes = (rej, res, fetchData, url, data, timeout = setTimeout) =>
}
}

export const fetchData = (url, pollEndpoint = poll) =>
export const fetchData = (url, pollEndpoint = poll, pollRes = resolvePollRes) =>
(rej, res) => {
pollEndpoint(url).fork(
rej,
Expand Down
36 changes: 31 additions & 5 deletions src/polling/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,36 @@
import { of, reject } from 'fluture';
import assert from 'power-assert';
import { match, spy } from 'sinon';

import { fetchData, pollRes } from './';
import { fetchData, resolvePollRes } from './';

describe('API: Polling', () => {
describe('pollRes', () => {
describe('fetchData', () => {
it('uses the reject branch of the parent future when necessary', () => {
const rej = spy();
const res = null;
const poll = () => reject('This is an error!');
fetchData('http://moo', poll)(rej, res);
assert(rej.calledWith('This is an error!'));
});

it('calls resolvePollRes to process poll result', () => {
const rej = () => {};
const res = () => {};
const poll = () => of({ some: 'data' });
const pollRes = spy();
fetchData('http://moo', poll, pollRes)(rej, res);
assert(pollRes.calledWith(
rej,
res,
fetchData,
'http://moo',
{ some: 'data' }
))
});
});

describe('resolvePollRes', () => {
it('returns data once the data is ready', () => {
const rej = spy();
const res = spy();
Expand All @@ -13,7 +39,7 @@ describe('API: Polling', () => {
Status: 'UpdatesComplete'
};

const result = pollRes(rej, res, fetchData, 'url', data);
const result = resolvePollRes(rej, res, fetchData, 'url', data);
assert(res.calledWith(data));
});

Expand All @@ -27,7 +53,7 @@ describe('API: Polling', () => {
Status: 'UpdatesPending'
};

const result = pollRes(
const result = resolvePollRes(
rej,
res,
fetchData,
Expand All @@ -49,7 +75,7 @@ describe('API: Polling', () => {
Status: 'UpdatesPending'
};

const result = pollRes(
const result = resolvePollRes(
rej,
res,
fetchData,
Expand Down
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@ glob-parent@^2.0.0:
dependencies:
is-glob "^2.0.0"

glob@7.0.5, glob@^7.0.5:
glob@7.0.5:
version "7.0.5"
resolved "https://registry.yarnpkg.com/glob/-/glob-7.0.5.tgz#b4202a69099bbb4d292a7c1b95b6682b67ebdc95"
dependencies:
Expand All @@ -853,7 +853,7 @@ glob@^5.0.5:
once "^1.3.0"
path-is-absolute "^1.0.0"

glob@^7.0.6:
glob@^7.0.5, glob@^7.0.6:
version "7.1.1"
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.1.tgz#805211df04faaf1c63a3600306cdf5ade50b2ec8"
dependencies:
Expand Down

0 comments on commit cdd32b8

Please sign in to comment.