import {
  ButtonController,
  RESET_DELAY,
  OPTIMISTIC_COLOR,
  OPTIMISTIC_ICON
} from './button.controller';

describe('ButtonController', () => {

  let ctrl, $window;

  beforeEach(() => {
    $window = {
      location: {
        href: null
      }
    }
    ctrl = new ButtonController($window, setTimeout.bind(window));
  });

  it('Should open a link when link is set', () => {
    let link = 'http://google.de'
    ctrl.link = link;
    ctrl.clickHandler();
    expect($window.location.href).toBe(link);
  });

  it('If mode is optimistic and no link is given, it should be disabled and change color and icon', () => {
    ctrl.optimistic = 'true';
    ctrl.clickHandler();
    expect(ctrl.disabled).toBe(true);
    expect(ctrl.color).toBe(OPTIMISTIC_COLOR);
    expect(ctrl.icon).toBe(OPTIMISTIC_ICON);
  });

  it(`Should reset the state to inital-one after ${RESET_DELAY}ms`, (done) => {
    ctrl.optimistic = 'true';
    let iconBefore = ctrl.icon;
    let colorBefore = ctrl.color;
    let disabledBefore = ctrl.disabled;
    ctrl.clickHandler();
    setTimeout(() => {
      expect(ctrl.icon).toBe(iconBefore);
      expect(ctrl.color).toBe(colorBefore);
      expect(ctrl.disabled).toBe(disabledBefore);
      done();
    }, ctrl.resetDelay + 500);
  });
});
