jest.dontMock('../../stores/MesosSummaryStore');

const ApplicationUtil = require('../ApplicationUtil');
const Config = require('../../config/Config');
const EventTypes = require('../../constants/EventTypes');
const MesosSummaryStore = require('../../stores/MesosSummaryStore');

describe('ApplicationUtil', function () {

  describe('#invokeAfterPageLoad', function () {

    it('should call callback right away', function () {
      let spy = jest.fn();
      let now = Date.now();

      global.getPageLoadedTime = function () {
        return now - Config.applicationRenderDelay;
      };

      ApplicationUtil.invokeAfterPageLoad(spy);

      expect(setTimeout.mock.calls[0][0]).toEqual(spy);
      expect(setTimeout.mock.calls[0][1]).toEqual(undefined);
    });

    it('should call after time has elapsed', function () {
      let spy = jest.fn();
      let now = Date.now();

      global.getPageLoadedTime = function () {
        return now;
      };

      ApplicationUtil.invokeAfterPageLoad(spy);

      expect(setTimeout.mock.calls[0][0]).toEqual(spy);
      expect(setTimeout.mock.calls[0][1]).toBeCloseTo(1000, 100);
    });

  });

  describe('#beginTemporaryPolling', function () {

    it('calls callback once event is emitted', function () {
      let spy = jest.fn();
      ApplicationUtil.beginTemporaryPolling(spy);
      expect(spy).not.toBeCalled();
      MesosSummaryStore.emit(EventTypes.MESOS_SUMMARY_CHANGE);
      expect(spy).toBeCalled();
    });

  });

});

jest.dontMock('../../stores/MesosSummaryStore');

const ApplicationUtil = require('../ApplicationUtil');
const Config = require('../../config/Config');
const EventTypes = require('../../constants/EventTypes');
const MesosSummaryStore = require('../../stores/MesosSummaryStore');

describe('ApplicationUtil', function () {

  describe('#invokeAfterPageLoad', function () {

    beforeEach(function () {
      // Clean up application timers.
      jasmine.clock().uninstall();
      // Install our custom jasmine timers.
      jasmine.clock().install();
      jasmine.clock().mockDate(new Date(2016, 3, 19));
    });

    afterEach(function () {
      // Clean up application timers.
      jasmine.clock().uninstall();
    });

    it('should call callback right away', function () {
      let handler = jasmine.createSpy('handler');
      let now = Date.now();

      global.getPageLoadedTime = function () {
        return now - Config.applicationRenderDelay;
      };

      ApplicationUtil.invokeAfterPageLoad(handler);

      jasmine.clock().tick();
      expect(handler).toHaveBeenCalled();
    });

    it('should call after time has elapsed', function () {
      let handler = jasmine.createSpy('handler');
      let now = Date.now();

      global.getPageLoadedTime = function () {
        return now;
      };

      ApplicationUtil.invokeAfterPageLoad(handler);

      // Ensure that it's not called before the time has elapsed, tick the
      // timers and then check if it was called
      expect(handler).not.toHaveBeenCalled();
      jasmine.clock().tick(Config.applicationRenderDelay);
      expect(handler).toHaveBeenCalled();
    });

  });

  describe('#beginTemporaryPolling', function () {

    it('calls callback once event is emitted', function () {
      let handler = jasmine.createSpy('handler');
      ApplicationUtil.beginTemporaryPolling(handler);
      expect(handler).not.toBeCalled();
      MesosSummaryStore.emit(EventTypes.MESOS_SUMMARY_CHANGE);
      expect(handler).toBeCalled();
    });

  });

});

Fix ApplicationUtil test reliability issues