import commonModule from '../..';

describe('autogrowInput directive', () => {
  let $compile;
  let outerScope;
  let element;

  beforeEach(angular.mock.module(commonModule));
  beforeEach(angular.mock.inject((_$compile_, $rootScope) => {
    $compile = _$compile_;
    outerScope = $rootScope.$new();
  }));

  function injectElement(elementHtml, scopeValues = {}) {
    element = angular.element(elementHtml);
    $compile(element)(Object.assign(outerScope, scopeValues));
    outerScope.$digest();
  }

  it('sets the value correctly', () => {
    injectElement('<input autogrow-input type="text" ng-model="value">', { value: 'Apple' });
    expect(element.val()).toEqual('Apple');
  });

  it('updates the input value if the model changes', () => {
    injectElement('<input autogrow-input type="text" ng-model="value">', { value: 'Apple' });
    outerScope.value = 'Orange';
    outerScope.$digest();
    expect(element.val()).toEqual('Orange');
  });
});
