nordnet-next-api
Advanced tools
Comparing version 2.2.0 to 2.2.1
'use strict'; | ||
require('test-helper'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
var _chai = require('chai'); | ||
var _testHelper = require('test-helper'); | ||
var _index = require('./../index'); | ||
var _expectations = require('./expectations'); | ||
describe('next-api', function () { | ||
var ntag = 'qwerty-12345-6789'; | ||
var _expectations2 = _interopRequireDefault(_expectations); | ||
var sandbox = undefined; | ||
var promise = undefined; | ||
var response = undefined; | ||
var fetchSpy = undefined; | ||
var _index = require('../index'); | ||
beforeEach(function () { | ||
initSandbox(); | ||
initResponses(); | ||
initSpies(); | ||
}); | ||
var _index2 = _interopRequireDefault(_index); | ||
afterEach(function () { | ||
return sandbox.restore(); | ||
}); | ||
var _chai = require('chai'); | ||
function initSandbox() { | ||
sandbox = sinon.sandbox.create(); | ||
sandbox.useFakeTimers(); | ||
sandbox.useFakeServer(); | ||
sandbox.server.respondImmediately = true; | ||
} | ||
function init(done, _ref) { | ||
var _this = this; | ||
function initResponses() { | ||
sandbox.server.respondWith('/next/2/accounts', [200, { 'Content-Type': 'application/json; charset=UTF-8', ntag: ntag }, JSON.stringify([{ accno: 123 }])]); | ||
sandbox.server.respondWith('/next/2/accounts/123', [200, { 'Content-Type': 'application/json; charset=UTF-8', ntag: ntag }, JSON.stringify({ accno: 123 })]); | ||
sandbox.server.respondWith('/next/2/accounts/456', [401, {}, '']); | ||
sandbox.server.respondWith('/next/2/instruments/123?positions=456,789&accno=987', JSON.stringify({ instrument_id: 123 })); | ||
sandbox.server.respondWith('GET', '/next/2/user/settings/foo', [204, { 'Content-Type': 'application/json; charset=UTF-8', ntag: ntag }, '']); | ||
sandbox.server.respondWith('POST', '/next/2/user/settings/bar', [200, { 'Content-Type': 'application/json; charset=UTF-8', ntag: ntag }, JSON.stringify([{ key: 'bar', value: { bar: 'bar' } }])]); | ||
sandbox.server.respondWith('PUT', '/next/2/user/settings/bar', [200, { 'Content-Type': 'application/json; charset=UTF-8', ntag: ntag }, JSON.stringify([{ key: 'bar', value: { bar: 'bar' } }])]); | ||
sandbox.server.respondWith('DELETE', '/next/2/user/settings/bar', [200, { 'Content-Type': 'application/json; charset=UTF-8', ntag: ntag }, '']); | ||
sandbox.server.respondWith('/next/2/news?days=0', [200, { 'Content-Type': 'application/json; charset=UTF-8', ntag: ntag }, JSON.stringify([{ news_id: 1 }])]); | ||
} | ||
var request = _ref.request; | ||
var response = _ref.response; | ||
function initSpies() { | ||
fetchSpy = sandbox.spy(window, 'fetch'); | ||
} | ||
function settle(method, url, params, headers) { | ||
return function settlePromise() { | ||
promise = method(url, params, headers).then(function (res) { | ||
return response = res; | ||
})['catch'](function (res) { | ||
return response = res; | ||
}); | ||
sandbox.clock.tick(1000); | ||
}; | ||
} | ||
function expectUrl(url) { | ||
(0, _chai.expect)(fetchSpy.args[0][0]).to.equal(url); | ||
} | ||
function expectHeaders(headers) { | ||
(0, _chai.expect)(fetchSpy.args[0][1].headers).to.deep.equal(headers); | ||
} | ||
describe('URL', function () { | ||
describe('when url is invalid', function () { | ||
return it('should throw an error', function () { | ||
return (0, _chai.expect)(function () { | ||
return (0, _index.get)(''); | ||
}).to['throw'](Error); | ||
}); | ||
}); | ||
describe('when path parameters are missing', function () { | ||
return it('should throw an error', function () { | ||
return (0, _chai.expect)(function () { | ||
return (0, _index.get)('/next/2/accounts/{accno}'); | ||
}).to['throw'](Error); | ||
}); | ||
}); | ||
describe('when path parameters are required', function () { | ||
beforeEach(settle(_index.get, '/next/2/accounts/{accno}', { accno: 123 })); | ||
it('should fetch with expected url', function (done) { | ||
expectUrl('/next/2/accounts/123'); | ||
done(); | ||
}); | ||
}); | ||
describe('when path parameters are not required', function () { | ||
beforeEach(settle(_index.get, '/next/2/accounts')); | ||
it('should fetch with expected url', function (done) { | ||
expectUrl('/next/2/accounts'); | ||
done(); | ||
}); | ||
}); | ||
describe('when path and query parameters are provided', function () { | ||
beforeEach(settle(_index.get, '/next/2/instruments/{instrument_id}', { instrument_id: 123, positions: [456, 789], accno: 987 })); | ||
it('should fetch with expected url', function (done) { | ||
expectUrl('/next/2/instruments/123?positions=' + encodeURIComponent('456,789') + '&accno=987'); | ||
done(); | ||
}); | ||
}); | ||
describe('when path contains query parameters', function () { | ||
beforeEach(settle(_index.get, '/next/2/instruments/{instrument_id}?positions={positions}', { instrument_id: 123, positions: [456, 789], accno: 987 })); | ||
it('should fetch with expected url', function (done) { | ||
expectUrl('/next/2/instruments/123?positions=' + encodeURIComponent('456,789') + '&accno=987'); | ||
done(); | ||
}); | ||
}); | ||
_testHelper.initSandBox.apply(this); | ||
_testHelper.respondWith.call(this, response); | ||
_testHelper.execute.apply(null, request).then(function (res) { | ||
return _this.response = res; | ||
}, function (res) { | ||
return _this.response = res; | ||
}).then(function () { | ||
return done(); | ||
})['catch'](function () { | ||
return done(); | ||
}); | ||
} | ||
describe('promise', function () { | ||
describe('when HTTP request succeeded', function () { | ||
beforeEach(settle(_index.get, '/next/2/accounts')); | ||
it('should resolve promise', function (done) { | ||
/*eslint-disable */ | ||
(0, _chai.expect)(promise).should.be.fulfilled; | ||
/*eslint-enable */ | ||
done(); | ||
}); | ||
function verifyExpectations(expected) { | ||
Object.keys(expected).forEach(function (key) { | ||
return it('should fetch with expected ' + key, function () { | ||
_testHelper.expectations[key].call(this, expected[key]); | ||
}); | ||
describe('when HTTP request fails', function () { | ||
beforeEach(settle(_index.get, '/next/2/accounts/{accno}', { accno: 456 })); | ||
it('should reject promise', function (done) { | ||
/*eslint-disable */ | ||
(0, _chai.expect)(promise).should.be.rejected; | ||
/*eslint-enable */ | ||
done(); | ||
}); | ||
it('should set response status', function (done) { | ||
(0, _chai.expect)(response.status).to.equal(401); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
} | ||
describe('get', function () { | ||
describe('accounts', function () { | ||
beforeEach(settle(_index.get, '/next/2/accounts/{accno}', { accno: 123 }, { 'Accept-Language': 'sv' })); | ||
function test(_ref2) { | ||
var conditions = _ref2.conditions; | ||
var expected = _ref2.expected; | ||
it('should set expected headers', function () { | ||
return expectHeaders({ Accept: 'application/json', 'Accept-Language': 'sv' }); | ||
}); | ||
it('should set method \'get\'', function () { | ||
return (0, _chai.expect)(fetchSpy.args[0][1].method).to.equal('get'); | ||
}); | ||
it('should include credentials', function () { | ||
return (0, _chai.expect)(fetchSpy.args[0][1].credentials).to.equal('include'); | ||
}); | ||
it('should not set body', function () { | ||
return (0, _chai.expect)(fetchSpy.args[0][1].body).to.be.undefined; | ||
}); | ||
it('should return expected status', function () { | ||
return (0, _chai.expect)(response.status).to.equal(200); | ||
}); | ||
it('should return expected response data', function () { | ||
return (0, _chai.expect)(response.data).to.deep.equal({ accno: 123 }); | ||
}); | ||
return function () { | ||
beforeEach(function (done) { | ||
init.call(this, done, conditions); | ||
}); | ||
describe('news', function () { | ||
beforeEach(settle(_index.get, '/next/2/news?days={days}', { days: 0 })); | ||
it('should return expected status', function () { | ||
return (0, _chai.expect)(response.status).to.equal(200); | ||
}); | ||
it('should return expected response data', function () { | ||
return (0, _chai.expect)(response.data).to.deep.equal([{ news_id: 1 }]); | ||
}); | ||
afterEach(function () { | ||
this.sandbox.restore(); | ||
}); | ||
describe('settings', function () { | ||
beforeEach(settle(_index.get, '/next/2/user/settings/{foo}', { foo: 'foo' })); | ||
verifyExpectations.call(this, expected); | ||
}; | ||
} | ||
it('should return expected status', function () { | ||
return (0, _chai.expect)(response.status).to.equal(204); | ||
}); | ||
it('should return expected response data', function () { | ||
return (0, _chai.expect)(response.data).to.be.undefined; | ||
}); | ||
function testThrows(conditions) { | ||
return function () { | ||
conditions.forEach(function (condition) { | ||
return Object.keys(_index2['default']).forEach(testMethodThrows(condition)); | ||
}); | ||
}); | ||
}; | ||
} | ||
describe('post', function () { | ||
beforeEach(settle(_index.get, '/next/2/user/settings/{key}', { key: 'foo' }, { 'Accept-Language': 'sv' })); | ||
describe('when parameters are missing', function () { | ||
beforeEach(settle(_index.post, '/next/2/user/settings/{key}', { key: 'bar' }, { 'Accept-Language': 'sv' })); | ||
it('should set empty POST body', function () { | ||
return (0, _chai.expect)(fetchSpy.args[1][1].body).to.equal(''); | ||
}); | ||
function testMethodThrows(condition) { | ||
return function (method) { | ||
return it('should throw an error with ' + method + ' and url \'' + condition + '\'', function () { | ||
return (0, _chai.expect)(function () { | ||
return _index2['default'][method](condition); | ||
}).to['throw'](Error); | ||
}); | ||
}; | ||
} | ||
describe('when parameters are provided', function () { | ||
beforeEach(settle(_index.post, '/next/2/user/settings/{key}', { key: 'bar', value: { bar: 'bar' } }, { 'Accept-Language': 'sv' })); | ||
it('should set POST body', function () { | ||
return (0, _chai.expect)(fetchSpy.args[1][1].body).to.deep.equal('value=' + encodeURIComponent(JSON.stringify({ bar: 'bar' }))); | ||
}); | ||
}); | ||
describe('when making POST request', function () { | ||
beforeEach(settle(_index.post, '/next/2/user/settings/{key}', { key: 'bar', value: { bar: 'bar' } }, { 'Accept-Language': 'sv' })); | ||
it('should set expected headers', function () { | ||
return (0, _chai.expect)(fetchSpy.args[1][1].headers).to.deep.equal({ 'Content-type': 'application/x-www-form-urlencoded', Accept: 'application/json', 'Accept-Language': 'sv', ntag: ntag }); | ||
}); | ||
it('should set method \'post\'', function () { | ||
return (0, _chai.expect)(fetchSpy.args[1][1].method).to.equal('post'); | ||
}); | ||
it('should include credentials', function () { | ||
return (0, _chai.expect)(fetchSpy.args[1][1].credentials).to.equal('include'); | ||
}); | ||
}); | ||
describe('when making POST request with custom Content-type header', function () { | ||
beforeEach(settle(_index.post, '/next/2/user/lists/{list_id}/{instrument_id}', { list_id: 1, instrument_id: 101 }, { 'Accept-Language': 'sv', 'Content-type': 'application/json' })); | ||
it('should set expected headers', function () { | ||
return (0, _chai.expect)(fetchSpy.args[1][1].headers).to.deep.equal({ 'Content-type': 'application/json', Accept: 'application/json', 'Accept-Language': 'sv', ntag: ntag }); | ||
}); | ||
it('should set method \'post\'', function () { | ||
return (0, _chai.expect)(fetchSpy.args[1][1].method).to.equal('post'); | ||
}); | ||
it('should include credentials', function () { | ||
return (0, _chai.expect)(fetchSpy.args[1][1].credentials).to.equal('include'); | ||
}); | ||
}); | ||
}); | ||
describe('put', function () { | ||
beforeEach(function () { | ||
settle(_index.get, '/next/2/user/settings/{key}', { key: 'foo' }, { 'Accept-Language': 'sv' })(); | ||
settle(_index.put, '/next/2/user/settings/{key}', { key: 'bar', value: { bar: 'bar' } }, { 'Accept-Language': 'sv' })(); | ||
}); | ||
it('should set ntag header', function () { | ||
return (0, _chai.expect)(fetchSpy.args[1][1].headers).to.deep.equal({ 'Content-type': 'application/x-www-form-urlencoded', Accept: 'application/json', 'Accept-Language': 'sv', ntag: ntag }); | ||
}); | ||
it('should set PUT body', function () { | ||
return (0, _chai.expect)(fetchSpy.args[1][1].body).to.deep.equal('value=' + encodeURIComponent(JSON.stringify({ bar: 'bar' }))); | ||
}); | ||
it('should set method \'put\'', function () { | ||
return (0, _chai.expect)(fetchSpy.args[1][1].method).to.equal('put'); | ||
}); | ||
it('should include credentials', function () { | ||
return (0, _chai.expect)(fetchSpy.args[1][1].credentials).to.equal('include'); | ||
}); | ||
}); | ||
describe('delete', function () { | ||
beforeEach(function () { | ||
settle(_index.get, '/next/2/user/settings/{key}', { key: 'foo' }, { 'Accept-Language': 'sv' })(); | ||
settle(_index.del, '/next/2/user/settings/{key}', { key: 'bar' }, { 'Accept-Language': 'sv' })(); | ||
}); | ||
it('should set ntag header', function () { | ||
return (0, _chai.expect)(fetchSpy.args[1][1].headers).to.deep.equal({ Accept: 'application/json', 'Accept-Language': 'sv', ntag: ntag }); | ||
}); | ||
it('should not set body', function () { | ||
return (0, _chai.expect)(fetchSpy.args[1][1].body).to.be.undefined; | ||
}); | ||
it('should set method \'delete\'', function () { | ||
return (0, _chai.expect)(fetchSpy.args[1][1].method).to.equal('delete'); | ||
}); | ||
it('should include credentials', function () { | ||
return (0, _chai.expect)(fetchSpy.args[1][1].credentials).to.equal('include'); | ||
}); | ||
}); | ||
describe('api', function () { | ||
describe('when url is invalid', testThrows([undefined, '', '/api/2/accounts/{accno}'])); | ||
describe('when request succeeded', test(_expectations2['default'].getInstrument)); | ||
describe('when request failed', test(_expectations2['default'].getAccounts)); | ||
describe('when response is not JSON', test(_expectations2['default'].ping)); | ||
describe('when making POST request', test(_expectations2['default'].postUserLists)); | ||
describe('when making POST request with JSON payload', test(_expectations2['default'].postUserSettings)); | ||
}); |
@@ -126,5 +126,5 @@ 'use strict'; | ||
var path = buildPath(options.url, options.params); | ||
var params = buildParams((0, _lodashObjectOmit2['default'])(options.params, getPathParams(options.url))); | ||
var params = (0, _lodashObjectOmit2['default'])(options.params, getPathParams(options.url)); | ||
var query = hasQuery(options.method) ? params : undefined; | ||
var query = hasQuery(options.method) ? buildParams(params) : undefined; | ||
var headers = buildHeaders(options.method, options.headers); | ||
@@ -251,3 +251,3 @@ var body = buildBody(options.method, params, headers); | ||
return isJsonContentType(headers) ? JSON.stringify(params) : params.join('&'); | ||
return isJsonContentType(headers) ? JSON.stringify(params) : buildParams(params).join('&'); | ||
} | ||
@@ -254,0 +254,0 @@ |
{ | ||
"name": "nordnet-next-api", | ||
"version": "2.2.0", | ||
"version": "2.2.1", | ||
"description": "Nordnet nExt API Javascript client", | ||
@@ -11,4 +11,3 @@ "main": "lib/index.js", | ||
"eslint": "eslint --ext=.jsx --ext=.js -c .eslintrc src || echo \"Linting failed...\"", | ||
"jscs": "jscs -c .jscsrc src", | ||
"lint": "npm run jscs && npm run eslint", | ||
"lint": "npm run eslint", | ||
"pretest": "rimraf reports/test-results.xml reports/coverage", | ||
@@ -38,3 +37,2 @@ "test": "karma start ./test/karma.conf.js --reporters mocha,junit,coverage --source-map", | ||
"babel-eslint": "^3.1.23", | ||
"babel-jscs": "^2.0.3", | ||
"babel-loader": "^5.1.4", | ||
@@ -49,4 +47,2 @@ "chai": "^3.0.0", | ||
"isparta-loader": "^0.2.0", | ||
"jscs": "jscs-dev/node-jscs#c5adeba", | ||
"jscs-jsdoc": "^1.1.0", | ||
"karma": "0.12.31", | ||
@@ -77,6 +73,6 @@ "karma-chai": "0.1.0", | ||
"dependencies": { | ||
"es6-promise": "^2.3.0", | ||
"isomorphic-fetch": "^2.1.1", | ||
"lodash": "^3.10.0" | ||
"es6-promise": "2.3.0", | ||
"isomorphic-fetch": "2.1.1", | ||
"lodash": "3.10.0" | ||
} | ||
} |
@@ -1,227 +0,54 @@ | ||
import 'test-helper'; | ||
import { initSandBox, respondWith, execute, expectations } from 'test-helper'; | ||
import tests from './expectations'; | ||
import api from '../index'; | ||
import { expect } from 'chai'; | ||
import { get, post, put, del } from './../index'; | ||
describe('next-api', () => { | ||
const ntag = 'qwerty-12345-6789'; | ||
function init(done, { request, response }) { | ||
initSandBox.apply(this); | ||
respondWith.call(this, response); | ||
execute.apply(null, request) | ||
.then(res => this.response = res, res => this.response = res) | ||
.then(() => done()) | ||
.catch(() => done()); | ||
} | ||
let sandbox; | ||
let promise; | ||
let response; | ||
let fetchSpy; | ||
function verifyExpectations(expected) { | ||
Object.keys(expected) | ||
.forEach(key => it(`should fetch with expected ${key}`, function() { | ||
expectations[key].call(this, expected[key]); | ||
})); | ||
} | ||
beforeEach(() => { | ||
initSandbox(); | ||
initResponses(); | ||
initSpies(); | ||
}); | ||
afterEach(() => sandbox.restore()); | ||
function initSandbox() { | ||
sandbox = sinon.sandbox.create(); | ||
sandbox.useFakeTimers(); | ||
sandbox.useFakeServer(); | ||
sandbox.server.respondImmediately = true; | ||
} | ||
function initResponses() { | ||
sandbox.server.respondWith('/next/2/accounts', [200, { 'Content-Type': 'application/json; charset=UTF-8', ntag: ntag }, JSON.stringify([{ accno: 123 }])]); | ||
sandbox.server.respondWith('/next/2/accounts/123', [200, { 'Content-Type': 'application/json; charset=UTF-8', ntag: ntag }, JSON.stringify({ accno: 123 })]); | ||
sandbox.server.respondWith('/next/2/accounts/456', [401, {}, '']); | ||
sandbox.server.respondWith('/next/2/instruments/123?positions=456,789&accno=987', JSON.stringify({ instrument_id: 123 })); | ||
sandbox.server.respondWith('GET', '/next/2/user/settings/foo', | ||
[204, { 'Content-Type': 'application/json; charset=UTF-8', ntag: ntag }, '']); | ||
sandbox.server.respondWith('POST', '/next/2/user/settings/bar', | ||
[200, { 'Content-Type': 'application/json; charset=UTF-8', ntag: ntag }, JSON.stringify([{ key: 'bar', value: { bar: 'bar' }}])]); | ||
sandbox.server.respondWith('PUT', '/next/2/user/settings/bar', | ||
[200, { 'Content-Type': 'application/json; charset=UTF-8', ntag: ntag }, JSON.stringify([{ key: 'bar', value: { bar: 'bar' }}])]); | ||
sandbox.server.respondWith('DELETE', '/next/2/user/settings/bar', | ||
[200, { 'Content-Type': 'application/json; charset=UTF-8', ntag: ntag }, '']); | ||
sandbox.server.respondWith('/next/2/news?days=0', [200, { 'Content-Type': 'application/json; charset=UTF-8', ntag: ntag }, JSON.stringify([{ news_id: 1 }])]); | ||
} | ||
function initSpies() { | ||
fetchSpy = sandbox.spy(window, 'fetch'); | ||
} | ||
function settle(method, url, params, headers) { | ||
return function settlePromise() { | ||
promise = method(url, params, headers) | ||
.then(res => response = res) | ||
.catch(res => response = res); | ||
sandbox.clock.tick(1000); | ||
}; | ||
} | ||
function expectUrl(url) { | ||
expect(fetchSpy.args[0][0]).to.equal(url); | ||
} | ||
function expectHeaders(headers) { | ||
expect(fetchSpy.args[0][1].headers).to.deep.equal(headers); | ||
} | ||
describe('URL', () => { | ||
describe('when url is invalid', () => | ||
it('should throw an error', () => expect(() => get('')).to.throw(Error))); | ||
describe('when path parameters are missing', () => | ||
it('should throw an error', () => expect(() => get('/next/2/accounts/{accno}')).to.throw(Error))); | ||
describe('when path parameters are required', () => { | ||
beforeEach(settle(get, '/next/2/accounts/{accno}', { accno: 123 })); | ||
it('should fetch with expected url', (done) => { | ||
expectUrl('/next/2/accounts/123'); | ||
done(); | ||
}); | ||
function test({ conditions, expected }) { | ||
return function() { | ||
beforeEach(function(done) { | ||
init.call(this, done, conditions); | ||
}); | ||
describe('when path parameters are not required', () => { | ||
beforeEach(settle(get, '/next/2/accounts')); | ||
it('should fetch with expected url', (done) => { | ||
expectUrl('/next/2/accounts'); | ||
done(); | ||
}); | ||
afterEach(function() { | ||
this.sandbox.restore(); | ||
}); | ||
describe('when path and query parameters are provided', () => { | ||
beforeEach(settle(get, '/next/2/instruments/{instrument_id}', { instrument_id: 123, positions: [456, 789], accno: 987 })); | ||
verifyExpectations.call(this, expected); | ||
}; | ||
} | ||
it('should fetch with expected url', (done) => { | ||
expectUrl('/next/2/instruments/123?positions=' + encodeURIComponent('456,789') + '&accno=987'); | ||
done(); | ||
}); | ||
}); | ||
function testThrows(conditions) { | ||
return function() { | ||
conditions.forEach(condition => Object.keys(api).forEach(testMethodThrows(condition))); | ||
}; | ||
} | ||
describe('when path contains query parameters', () => { | ||
beforeEach(settle(get, '/next/2/instruments/{instrument_id}?positions={positions}', { instrument_id: 123, positions: [456, 789], accno: 987 })); | ||
function testMethodThrows(condition) { | ||
return method => it(`should throw an error with ${method} and url '${condition}'`, | ||
() => expect(() => api[method](condition)).to.throw(Error)); | ||
} | ||
it('should fetch with expected url', (done) => { | ||
expectUrl('/next/2/instruments/123?positions=' + encodeURIComponent('456,789') + '&accno=987'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('promise', () => { | ||
describe('when HTTP request succeeded', () => { | ||
beforeEach(settle(get, '/next/2/accounts')); | ||
it('should resolve promise', (done) => { | ||
/*eslint-disable */ | ||
expect(promise).should.be.fulfilled; | ||
/*eslint-enable */ | ||
done(); | ||
}); | ||
}); | ||
describe('when HTTP request fails', () => { | ||
beforeEach(settle(get, '/next/2/accounts/{accno}', { accno: 456 })); | ||
it('should reject promise', (done) => { | ||
/*eslint-disable */ | ||
expect(promise).should.be.rejected; | ||
/*eslint-enable */ | ||
done(); | ||
}); | ||
it('should set response status', (done) => { | ||
expect(response.status).to.equal(401); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('get', () => { | ||
describe('accounts', () => { | ||
beforeEach(settle(get, '/next/2/accounts/{accno}', { accno: 123 }, { 'Accept-Language': 'sv' })); | ||
it('should set expected headers', () => expectHeaders({ Accept: 'application/json', 'Accept-Language': 'sv' })); | ||
it('should set method \'get\'', () => expect(fetchSpy.args[0][1].method).to.equal('get')); | ||
it('should include credentials', () => expect(fetchSpy.args[0][1].credentials).to.equal('include')); | ||
it('should not set body', () => expect(fetchSpy.args[0][1].body).to.be.undefined); | ||
it('should return expected status', () => expect(response.status).to.equal(200)); | ||
it('should return expected response data', () => expect(response.data).to.deep.equal({ accno: 123 })); | ||
}); | ||
describe('news', () => { | ||
beforeEach(settle(get, '/next/2/news?days={days}', { days: 0 })); | ||
it('should return expected status', () => expect(response.status).to.equal(200)); | ||
it('should return expected response data', () => expect(response.data).to.deep.equal([{ news_id: 1 }])); | ||
}); | ||
describe('settings', () => { | ||
beforeEach(settle(get, '/next/2/user/settings/{foo}', { foo: 'foo' })); | ||
it('should return expected status', () => expect(response.status).to.equal(204)); | ||
it('should return expected response data', () => expect(response.data).to.be.undefined); | ||
}); | ||
}); | ||
describe('post', () => { | ||
beforeEach(settle(get, '/next/2/user/settings/{key}', { key: 'foo' }, { 'Accept-Language': 'sv' })); | ||
describe('when parameters are missing', () => { | ||
beforeEach(settle(post, '/next/2/user/settings/{key}', { key: 'bar' }, { 'Accept-Language': 'sv' })); | ||
it('should set empty POST body', () => expect(fetchSpy.args[1][1].body).to.equal('')); | ||
}); | ||
describe('when parameters are provided', () => { | ||
beforeEach(settle(post, '/next/2/user/settings/{key}', { key: 'bar', value: { bar: 'bar' }}, { 'Accept-Language': 'sv' })); | ||
it('should set POST body', () => expect(fetchSpy.args[1][1].body).to.deep.equal('value=' + encodeURIComponent(JSON.stringify({ bar: 'bar' })))); | ||
}); | ||
describe('when making POST request', () => { | ||
beforeEach(settle(post, '/next/2/user/settings/{key}', { key: 'bar', value: { bar: 'bar' }}, { 'Accept-Language': 'sv' })); | ||
it('should set expected headers', () => | ||
expect(fetchSpy.args[1][1].headers).to.deep.equal( | ||
{ 'Content-type': 'application/x-www-form-urlencoded', Accept: 'application/json', 'Accept-Language': 'sv', ntag: ntag })); | ||
it('should set method \'post\'', () => expect(fetchSpy.args[1][1].method).to.equal('post')); | ||
it('should include credentials', () => expect(fetchSpy.args[1][1].credentials).to.equal('include')); | ||
}); | ||
describe('when making POST request with custom Content-type header', () => { | ||
beforeEach(settle(post, '/next/2/user/lists/{list_id}/{instrument_id}', | ||
{ list_id: 1, instrument_id: 101}, { 'Accept-Language': 'sv', 'Content-type': 'application/json' })); | ||
it('should set expected headers', () => | ||
expect(fetchSpy.args[1][1].headers).to.deep.equal( | ||
{ 'Content-type': 'application/json', Accept: 'application/json', 'Accept-Language': 'sv', ntag: ntag })); | ||
it('should set method \'post\'', () => expect(fetchSpy.args[1][1].method).to.equal('post')); | ||
it('should include credentials', () => expect(fetchSpy.args[1][1].credentials).to.equal('include')); | ||
}); | ||
}); | ||
describe('put', () => { | ||
beforeEach(() => { | ||
settle(get, '/next/2/user/settings/{key}', { key: 'foo' }, { 'Accept-Language': 'sv' })(); | ||
settle(put, '/next/2/user/settings/{key}', { key: 'bar', value: { bar: 'bar' }}, { 'Accept-Language': 'sv' })(); | ||
}); | ||
it('should set ntag header', () => | ||
expect(fetchSpy.args[1][1].headers).to.deep.equal( | ||
{ 'Content-type': 'application/x-www-form-urlencoded', Accept: 'application/json', 'Accept-Language': 'sv', ntag: ntag })); | ||
it('should set PUT body', () => expect(fetchSpy.args[1][1].body).to.deep.equal('value=' + encodeURIComponent(JSON.stringify({ bar: 'bar' })))); | ||
it('should set method \'put\'', () => expect(fetchSpy.args[1][1].method).to.equal('put')); | ||
it('should include credentials', () => expect(fetchSpy.args[1][1].credentials).to.equal('include')); | ||
}); | ||
describe('delete', () => { | ||
beforeEach(() => { | ||
settle(get, '/next/2/user/settings/{key}', { key: 'foo' }, { 'Accept-Language': 'sv' })(); | ||
settle(del, '/next/2/user/settings/{key}', { key: 'bar' }, { 'Accept-Language': 'sv' })(); | ||
}); | ||
it('should set ntag header', () => | ||
expect(fetchSpy.args[1][1].headers).to.deep.equal( | ||
{ Accept: 'application/json', 'Accept-Language': 'sv', ntag: ntag })); | ||
it('should not set body', () => expect(fetchSpy.args[1][1].body).to.be.undefined); | ||
it('should set method \'delete\'', () => expect(fetchSpy.args[1][1].method).to.equal('delete')); | ||
it('should include credentials', () => expect(fetchSpy.args[1][1].credentials).to.equal('include')); | ||
}); | ||
describe('api', function() { | ||
describe('when url is invalid', testThrows([undefined, '', '/api/2/accounts/{accno}'])); | ||
describe('when request succeeded', test(tests.getInstrument)); | ||
describe('when request failed', test(tests.getAccounts)); | ||
describe('when response is not JSON', test(tests.ping)); | ||
describe('when making POST request', test(tests.postUserLists)); | ||
describe('when making POST request with JSON payload', test(tests.postUserSettings)); | ||
}); |
@@ -86,5 +86,5 @@ import es6Promise from 'es6-promise'; | ||
const path = buildPath(options.url, options.params); | ||
const params = buildParams(omit(options.params, getPathParams(options.url))); | ||
const params = omit(options.params, getPathParams(options.url)); | ||
const query = hasQuery(options.method) ? params : undefined; | ||
const query = hasQuery(options.method) ? buildParams(params) : undefined; | ||
const headers = buildHeaders(options.method, options.headers); | ||
@@ -202,3 +202,3 @@ const body = buildBody(options.method, params, headers); | ||
return isJsonContentType(headers) ? JSON.stringify(params) : params.join('&'); | ||
return isJsonContentType(headers) ? JSON.stringify(params) : buildParams(params).join('&'); | ||
} | ||
@@ -205,0 +205,0 @@ |
@@ -1,1 +0,62 @@ | ||
require('es5-shim'); // make phantomjs happy | ||
import { expect } from 'chai'; | ||
export function initSandBox() { | ||
this.sandbox = sinon.sandbox.create(); | ||
this.sandbox.useFakeServer(); | ||
this.sandbox.server.autoRespond = true; | ||
this.fetchSpy = this.sandbox.spy(window, 'fetch'); | ||
} | ||
export function respondWith(response) { | ||
this.sandbox.server.respondWith.apply(this.sandbox.server, response); | ||
} | ||
export function execute(method, request) { | ||
return method.apply(null, request); | ||
} | ||
export const expectations = { | ||
url: expectUrl, | ||
headers: expectHeaders, | ||
method: expectMethod, | ||
credentials: expectCredentials, | ||
body: expectBody, | ||
status: expectStatus, | ||
data: expectData, | ||
}; | ||
function expectUrl(url) { | ||
expect(this.fetchSpy).to.have.been.calledWith(url); | ||
} | ||
function expectHeaders(headers) { | ||
expectDeepEqual(headers, this.fetchSpy.args[0][1].headers); | ||
} | ||
function expectMethod(method) { | ||
expectEqual(method, this.fetchSpy.args[0][1].method); | ||
} | ||
function expectCredentials() { | ||
expectEqual('include', this.fetchSpy.args[0][1].credentials); | ||
} | ||
function expectBody(body) { | ||
expectDeepEqual(body, this.fetchSpy.args[0][1].body); | ||
} | ||
function expectStatus(status) { | ||
expectEqual(status, this.response.status); | ||
} | ||
function expectData(data) { | ||
expectDeepEqual(data, this.response.data); | ||
} | ||
function expectEqual(expected, actual) { | ||
expect(actual).to.equal(expected); | ||
} | ||
function expectDeepEqual(expected, actual) { | ||
expect(actual).to.deep.equal(expected); | ||
} |
@@ -0,3 +1,10 @@ | ||
// make phantomjs happy, polyfill .bind etc... | ||
require('es5-shim'); | ||
var chai = require('chai'); | ||
var sinonChai = require('sinon-chai'); | ||
chai.use(sinonChai); | ||
// --- Load all common js tests via webpack | ||
var context = require.context('./../src', true, /\.test\.js$/); | ||
context.keys().forEach(context); | ||
context.keys().forEach(context); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
65038
35
38
1564
4
+ Addedisomorphic-fetch@2.1.1(transitive)
+ Addedlodash@3.10.0(transitive)
+ Addedwhatwg-fetch@0.8.2(transitive)
- Removedisomorphic-fetch@2.2.1(transitive)
- Removedlodash@3.10.1(transitive)
- Removedwhatwg-fetch@3.6.20(transitive)
Updatedes6-promise@2.3.0
Updatedisomorphic-fetch@2.1.1
Updatedlodash@3.10.0