UNPKG

redux-observable-sans

Version:
272 lines (254 loc) 9.67 kB
'use strict'; var _reduxObservable = require('redux-observable'); var _rxjs = require('rxjs'); var _index = require('../index'); var _client = require('fetch-mock/es5/client'); var _client2 = _interopRequireDefault(_client); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } describe('httpEpic', function () { var dispatch = function dispatch(i) { return i; }; var url = 'localhost:3000'; var me = { token: 'key', name: 'AB Normal', userId: '3f43f' }; var state$ = { value: { me: me } }; var subject = void 0; var action$ = void 0; var testOutput = function testOutput(done, testFetch, expectedActions) { var count = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0; return function (action) { expect(action).toEqual(expectedActions[count]); count++; if (count === expectedActions.length) { testFetch(); done(); } }; }; beforeEach(function () { subject = new _rxjs.Subject(); action$ = new _reduxObservable.ActionsObservable(subject); }); afterEach(function () { _client2.default.restore(); }); it('basic GET', function (done) { // values var type = 'GET_WHAT'; var path = 'whats'; var method = 'GET'; var response = { what: 'evs' // setup };var httpAct = (0, _index.httpAction)(dispatch)(type, path, method)(); var expectedActions = [{ type: type + '_REQUEST', payload: { body: undefined, method: method, url: url + '/' + path } }, { type: type + '_SUCCESS', payload: response }]; _client2.default.mock({ matcher: url + '/' + path, method: method, response: response }); var testFetch = function testFetch() { return expect(_client2.default.calls(url + '/' + path, method)).toEqual([[url + '/' + path, { 'headers': { Accept: 'application/json', 'Content-Type': 'application/json' }, method: method }]]); }; // run var result$ = (0, _index.httpEpic)({ url: url })(action$, state$); result$.subscribe(testOutput(done, testFetch, expectedActions), console.error, console.error); subject.next(httpAct); }); it('POST with auth headers and id', function (done) { // values var id = 'GH'; // test addition of id var setHeaders = function setHeaders(action$, deps) { return { Authorization: 'Bearer ' + action$.value.me.token }; }; var type = 'SET_WHAT'; var path = 'whats'; var method = 'POST'; var body = { what: 'evs' }; var response = { yay: 'success' // setup };var httpAct = (0, _index.httpAction)(dispatch, id)(type, path, method)(body); var expectedActions = [{ type: type + '_REQUEST', payload: { body: body, method: method, url: url + '/' + path } }, { type: type + '_SUCCESS', payload: response }]; _client2.default.mock({ matcher: url + '/' + path, method: method, response: response }); var testFetch = function testFetch() { return expect(_client2.default.calls(url + '/' + path, method)).toEqual([[url + '/' + path, { 'headers': { Accept: 'application/json', 'Content-Type': 'application/json', Authorization: 'Bearer key' }, method: method, body: JSON.stringify(body) }]]); }; // run var options = { id: id, url: url, setHeaders: setHeaders }; var result$ = (0, _index.httpEpic)(options)(action$, state$); result$.subscribe(testOutput(done, testFetch, expectedActions), console.error, console.error); subject.next(httpAct); }); it('401 with extra action', function (done) { // values var id = 'LH'; var type = 'SET_WHAT'; var path = 'unauth'; var method = 'POST'; var body = { what: 'evs' }; var response = { status: 401, body: { error: 'no entry' } // setup };var httpAct = (0, _index.httpAction)(dispatch, id)(type, path, method)(body); var expectedActions = [{ type: type + '_REQUEST', payload: { body: body, method: method, url: url + '/' + path } }, { type: type + '_FAILURE', payload: response.body }, { type: 'HTTP_LH_UNAUTHORIZED', payload: { url: url + '/' + path, code: response.status, body: response.body } }]; _client2.default.mock({ matcher: url + '/' + path, method: method, response: response }); var testFetch = function testFetch() { return expect(_client2.default.calls(url + '/' + path, method)).toEqual([[url + '/' + path, { 'headers': { Accept: 'application/json', 'Content-Type': 'application/json' }, method: method, body: JSON.stringify(body) }]]); }; // run var result$ = (0, _index.httpEpic)({ url: url, id: id })(action$, state$); result$.subscribe(testOutput(done, testFetch, expectedActions), console.error, console.error); subject.next(httpAct); }); it('http error', function (done) { // values var id = 'LH'; var type = 'SET_WHAT'; var path = 'unauth'; var method = 'POST'; var body = { what: 'evs' }; var response = { status: 500, body: { error: 'no entry' } // setup };var httpAct = (0, _index.httpAction)(dispatch, id)(type, path, method)(body); var expectedActions = [{ type: type + '_REQUEST', payload: { body: body, method: method, url: url + '/' + path } }, { type: type + '_FAILURE', payload: response.body }, { type: 'HTTP_LH_ERROR', payload: { url: url + '/' + path, code: response.status, body: response.body } }]; _client2.default.mock({ matcher: url + '/' + path, method: method, response: response }); var testFetch = function testFetch() { return expect(_client2.default.calls(url + '/' + path, method)).toEqual([[url + '/' + path, { 'headers': { Accept: 'application/json', 'Content-Type': 'application/json' }, method: method, body: JSON.stringify(body) }]]); }; // run var result$ = (0, _index.httpEpic)({ url: url, id: id })(action$, state$); result$.subscribe(testOutput(done, testFetch, expectedActions), console.error, console.error); subject.next(httpAct); }); it('throws', function (done) { // values var type = 'SET_WHAT'; var path = 'throws'; var method = 'POST'; var body = { what: 'evs' }; var msg = 'none of that'; var response = function response() { throw new Error(msg); }; // setup var httpAct = (0, _index.httpAction)(dispatch)(type, path, method)(body); var expectedActions = [{ type: type + '_REQUEST', payload: { body: body, method: method, url: url + '/' + path } }, { type: type + '_FAILURE' }, { type: 'HTTP_ERROR', payload: { url: url + '/' + path } }]; _client2.default.mock({ matcher: url + '/' + path, method: method, response: response }); var testFetch = function testFetch() { return expect(_client2.default.calls(url + '/' + path, method)).toEqual([[url + '/' + path, { 'headers': { Accept: 'application/json', 'Content-Type': 'application/json' }, method: method, body: JSON.stringify(body) }]]); }; // run var result$ = (0, _index.httpEpic)({ url: url })(action$, state$); result$.subscribe(testOutput(done, testFetch, expectedActions), console.error, console.error); subject.next(httpAct); }); it('EPIC_END causes _CANCELLED', function (done) { // values var type = 'GET_WHAT'; var path = 'whats'; var method = 'GET'; var response = { what: 'evs' // setup };var httpAct = (0, _index.httpAction)(dispatch)(type, path, method)(); var expectedActions = [{ type: type + '_REQUEST', payload: { body: undefined, method: method, url: url + '/' + path } }, { type: type + '_CANCELLED' }]; _client2.default.mock({ matcher: url + '/' + path, method: method, response: response }); var testFetch = function testFetch() { return expect(_client2.default.calls(url + '/' + path, method)).toEqual([[url + '/' + path, { 'headers': { Accept: 'application/json', 'Content-Type': 'application/json' }, method: method }]]); }; // run var result$ = (0, _index.httpEpic)({ url: url })(action$, state$); result$.subscribe(testOutput(done, testFetch, expectedActions), console.error, console.error); subject.next(httpAct); subject.next({ type: _reduxObservable.EPIC_END }); }); it('_CANCELLED', function (done) { // values var type = 'GET_WHAT'; var path = 'whats'; var method = 'GET'; var response = { what: 'evs' // setup };var httpAct = (0, _index.httpAction)(dispatch)(type, path, method)(); var expectedActions = [{ type: type + '_REQUEST', payload: { body: undefined, method: method, url: url + '/' + path } }]; _client2.default.mock({ matcher: url + '/' + path, method: method, response: response }); var testFetch = function testFetch() { return expect(_client2.default.calls(url + '/' + path, method)).toEqual([[url + '/' + path, { 'headers': { Accept: 'application/json', 'Content-Type': 'application/json' }, method: method }]]); }; // run var result$ = (0, _index.httpEpic)({ url: url })(action$, state$); result$.subscribe(testOutput(done, testFetch, expectedActions), console.error, console.error); subject.next(httpAct); subject.next({ type: type + '_CANLCELLED' }); }); });