import commonModule from '../';

describe('wsSignaturePadComponent', () => {
  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-signature-pad on-save="onSave" on-redo="onRedo" />');

    it('clear button should be hidden', () => {
      const buttons = element.find('button');
      expect(buttons.length).toBe(1);
      expect(buttons[0].innerHTML).toBe('Save signature');
    });
  });

  describe('modifying the signature pad', () => {
    injectElement('<ws-signature-pad on-save="onSave" on-redo="onRedo" />');

    it('display both buttons when signature pad is manipulated', () => {
      isolatedScope.state.isEdited = true;
      isolatedScope.$digest();

      const buttons = element.find('button');
      expect(buttons.length).toBe(2);
      expect(buttons[0].innerHTML).toBe('Clear');
      expect(buttons[1].innerHTML).toBe('Save signature');
    });

    it('displays redo when save signature is pressed', () => {
      isolatedScope.state.isEdited = true;
      isolatedScope.$digest();

      const saveButton = element.find('button')[1];
      saveButton.click();

      const buttons = element.find('button');
      expect(buttons.length).toBe(1);
      expect(buttons[0].innerHTML).toBe('Redo');
      expect(isolatedScope.state.isSaved).toBe(true);
    });

    it('displays save signature button when redo is pressed', () => {
      isolatedScope.state.isEdited = true;
      isolatedScope.state.isSaved = true;
      isolatedScope.$digest();

      const redoButton = element.find('button')[0];
      expect(redoButton.innerHTML).toBe('Redo');
      redoButton.click();

      const buttons = element.find('button');
      expect(buttons.length).toBe(1);
      expect(buttons[0].innerHTML).toBe('Save signature');
    });
  });
});
