import commonModule from '../';

describe('WsMultipleInputComponent', () => {
  let isolatedScope;
  let outerScope;
  let element;

  beforeEach(angular.mock.module(commonModule));

  function injectElement(elementHtml) {
    beforeEach(angular.mock.inject(($rootScope, $compile) => {
      outerScope = $rootScope.$new();
      element = angular.element(elementHtml);
      $compile(element)(outerScope);
      isolatedScope = element.isolateScope();
      isolatedScope.$digest();
    }));
  }

  describe('initial state', () => {
    injectElement('<ws-multiple-input on-completed="onCompleted" number-of-inputs="3" is-readonly="false" />');

    it('should have created the right number of inputs', () => {
      const inputs = element.find('.ws-multiple-input');
      expect(inputs.length).toBe(3);
    });
  });

  describe('populating an input', () => {
    injectElement('<ws-multiple-input on-completed="onCompleted" number-of-inputs="3" is-readonly="false" />');

    it('should focus on next element', () => {
      const input0 = element.find('.ws-multiple-input')[0];
      const input1 = element.find('.ws-multiple-input')[1];
      spyOn(input1, 'focus');

      angular.element(input0).val('3');
      input0.dispatchEvent(new Event('keyup'));
      outerScope.$digest();

      expect(input1.focus).toHaveBeenCalled();
    });
  });
});
