Friday 28 April 2017

Assert variable in non public function?

I want to assert weatherData.length is 4 in computeHours(response)...for 4 hour weather reports. Is this possible? I wasn't sure since weatherData array is not public nor is the function computeHours().

Note: using proxyquire for injection

var HOURS_AHEAD_TO_CHECK = 4;

function init() {
  fetch(darkWeatherUrl)
    .then(toJSON)
    .then(computeHours)
    .then(sendAlerts)
    .catch((e) => {
      console.log('init error ' + e);
    });
}

function computeHours(response) {
  var weatherData = [];
  response.hourly.data.every((hourlyReport, index) => {
    weatherData.push(hourlyReport);

    if (index === HOURS_AHEAD_TO_CHECK-1) {
      return false;
    } else {
      return true;
    }
  });
  console.log(weatherData.length);
  return weatherData;
}

function checkBadConditions(hourData) {
  if (hourData.icon === 'hail' || hourData.icon === 'thunderstorm'
      || hourData.icon === 'tornado') {
        return true;
      } else {
        return false;
      }
}

module.exports = init;


describe('lifx alert test', ()=> {
  var fetchStub;

  beforeEach(() => {
    fetchStub = sinon.stub();
  });

  it('should check the next 4 hour report', ()=> {

    var body = {
      "hourly": {
         data:
           [ { time: 1493413200,
              icon: 'clear-day',
              precipIntensity: 0,
              precipProbability: 0,
              ozone: 297.17 },
            { time: 1493416800,
              icon: 'clear-day',
              precipIntensity: 0,
              precipProbability: 0,
              ozone: 296.89 },
            { time: 1493420400,
              icon: 'clear-day',
              precipIntensity: 0,
              precipProbability: 0,
              ozone: 296.73 },
            { time: 1493424000,
              icon: 'clear-day',
              precipIntensity: 0,
              precipProbability: 0,
              ozone: 296.31 } ]
        }
    };

    var response = { json: () => { return body } };
    fetchStub.returns(Promise.resolve(response));
    var init =  proxy('../index.js', {'node-fetch': fetchStub});
    init();
    fetchStub.should.have.been.called;
  });

});



via dman

No comments:

Post a Comment