mardi 19 mai 2020

How to mock a POST request correctly with no data in the response with chai/mocha?

I'm trying to cover the lines of code within my claimNotes.js file from my claimNotes.test.js test file. I want to cover the lines of code of the else statement from the following:

Code snippet of claimNotes.js file

if (response.status != null && response.status == '200') {
      if ('data' in response.data) {
        if (response.data.data.length > 0) {
          updatedState.claimNotes = response.data.data[0];
          return updatedState;
        }
        else {
          updatedState.claimNotes = 'Error';
          return updatedState;
        }
      }
    }
    else {
      updatedState.claimNotes = "Error";

      await event.reply('#builtin_text', {
        text: `Notes not Available for Provided Claim ID`,
        typing: true
      });
    }

  }

I can't seem to cover the else statement from the following chai test written here:

claimNotes.test.js

const claimNotes = require('../../src/api/claimNotes');
const knowledgePortalAPI = require ('../../src/api/knowledgePortalAPI'); 
const rewire = require('rewire');
const { expect } = require('chai');
const MockAdapter =  require('axios-mock-adapter');
const Store = require('../store');

//import the updatedState and Event Variables from the store.
const newStore = new Store();
let updatedState = newStore.getUpdatedState();
let event = newStore.getEvent();
let initialState = newStore.getInitialState();

const proxyquire = require('proxyquire').noCallThru();

const _ = require('lodash');

describe('testing claimNotes function', () => {
  it('GET request with data', async () => {
    updatedState = _.cloneDeep(initialState);
    const router = rewire('../../src/api/claimNotes');
    const claimNotes = router.__get__('claimNotes');

    const mockEndpoint = new RegExp(/.*/, 'g');
    let mock = new MockAdapter(knowledgePortalAPI);
    mock.onGet(mockEndpoint).reply(404);

    let claimObject = await claimNotes(updatedState, event);
  });
});

Thee store file just contains the appropriate store variables needed to go through the file. Any help much be much appreciated.

Aucun commentaire:

Enregistrer un commentaire