lundi 22 mai 2017

Jest test for custom redux middleware is returning a promise as undefined

Im attempting my first go at writing a api calling Redux middleware. Somehow my tests are all passing but im getting a undefined return value on the promise that should be getting returned and then resolved on the final test. Ive attached my test output and code below. Any help or thoughts are greatly appreciated!

TEST OUTPUT

console.warn __tests__/youtubeMiddleware.test.js:56
Promise { <pending> }

(node:13535) UnhandledPromiseRejectionWarning: Unhandled promise 
rejection (rejection id: 1): Error: expect(received).toBe(expected)

Expected value to be (using ===):
{"status": "ok"}
Received:
undefined

Difference:

Comparing two different types of values. Expected object but received 
undefined.
(node:13535) DeprecationWarning: Unhandled promise rejections are 
deprecated. In the future, promise rejections that are not handled will 
terminate the Node.js process with a non-zero exit code.
PASS  __tests__/youtubeMiddleware.test.js
YOUTUBE_MIDDLEWARE:Api
when action is called without CALL_API
  ✓ passes the action to next middleware (16ms)
when action has been called with CALL_YOUTUBE
  ✓ sends request to path with query and body (36ms)
  ✓ resolves returned promise with response when success (41ms)
  ✓ dispatch successType with response when success (35ms)

Test Suites: 1 passed, 1 total
Tests:       4 passed, 4 total
Snapshots:   0 total
Time:        3.758s
Ran all test suites.
/////////////////////////////

MIDDLEWARE

import superAgent from 'superagent';
// import Promise from 'bluebird';
export const CALL_YOUTUBE = Symbol('CALL_YOUTUBE');
import { fetchingChannelPlaylists, recievedPlaylists } from 
'../actions/YOUTUBE_DATA_API_ACTIONS';

export default store => next => action => {
  if ( !action[CALL_YOUTUBE] ) {
    return next(action);
  }
  let request = action[CALL_YOUTUBE];
  let { getState } = store;
  let { method, url, successType } = request;
  return new Promise((resolve,reject) => {
     superAgent[method](url)
     .then((res)=> {
       resolve(next({
         type: successType,
         response: res.body
       }));
     })
     .catch((err) => {
       reject(err);
     })

  })
};

TEST

import * as types from '../GPX_REDUX_LIB/types/youtube_types.js';
import * as action from 
'../GPX_REDUX_LIB/actions/YOUTUBE_DATA_API_ACTIONS';
import configureStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import nock from 'nock';
import youtubeMiddleware, { CALL_YOUTUBE } from 
'../GPX_REDUX_LIB/middleware/youtubeMiddleWare';

const middlewares = [thunk, youtubeMiddleware];
const mockStore = configureStore(middlewares)
export const YTKEY = 'some super cool string!;

describe('YOUTUBE_MIDDLEWARE:Api', function() {
  let store, next;
  let action;
  let successType = 'PLAYLISTS_SUCCESS';
  let url = 'http://the-url/path'

  beforeEach(function(){
     store = {};
     next = jest.fn();
     action = {
       [CALL_YOUTUBE]: {
          method: 'get',
          url,
          successType
       }
     };
   });
describe('when action is called without CALL_API', function() {
    it('passes the action to next middleware', function() {
       action = {type: 'not-CALL_API'};
       youtubeMiddleware(store)(next)(action);
       expect(next).toHaveBeenCalledWith(action);
    });
});

describe('when action has been called with CALL_YOUTUBE', function() {
    let nockScope;
    beforeEach(function() {
      nockScope = nock('http://the-url')
                    .get('/path')
    });
    afterEach(function() {
      nock.cleanAll();
    });
    it('sends request to path with query and body', function(){
      nockScope = nockScope.reply(200, { status: 'ok' });

      youtubeMiddleware(store)(next)(action);

      nockScope.done();
    });
  it('resolves returned promise with response when success', function()
{
    nockScope = nockScope.reply(200, { status: 'ok' });
    let promise = youtubeMiddleware(store)(next)(action);
    console.warn(promise);
      // return promise.then((res) => {
        expect(promise).resolves.toBe({ status: 'ok'})
      // });
  });
  it('dispatch successType with response when success', function(done){
    nockScope = nockScope.reply(200, { status: 'ok' });
    let promise = youtubeMiddleware(store)(next)(action);
    promise.then(()=> {
      expect(next).toHaveBeenCalledWith({
        type: successType,
        response: {
          status: 'ok'

        }
      });
      done();
    });
  });
})

Aucun commentaire:

Enregistrer un commentaire