import commonModule from '../..';

describe('wsCurrency directive', () => {
  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);
      outerScope.$digest();
      element.isolateScope();
      angular.element(element.find('input'));
    }));
  }

  describe('Not actionable', () => {
    injectElement('<ws-notification actionable="false" type="neutral" dismissible="false" body-string="test123"/>');

    it('should not render a button', () => {
      expect(element.html()).toContain('test123');
      expect(element.html()).not.toContain('button primary-action');
    });
  });

  describe('Actionable', () => {
    injectElement('<ws-notification action-string="take action" actionable="true" type="neutral" dismissible="false" body-string="test123"/>');

    it('should render a button', () => {
      expect(element.html()).toContain('test123');
      expect(element.html()).toContain('button primary-action');
      expect(element.html()).toContain('take action');
    });
  });

  describe('Not Dismissible', () => {
    injectElement('<ws-notification dismissible="false" actionable="false" type="neutral" body-string="test123"/>');

    it('should not render the close button', () => {
      expect(element.html()).toContain('test123');
      expect(element.html()).not.toContain('close');
    });
  });

  describe('Dismissible', () => {
    injectElement('<ws-notification dismiss="console.log(\'hit\')" dismissible="true" actionable="false" type="neutral" body-string="test123"/>');

    it('should not render the close button', () => {
      expect(element.html()).toContain('test123');
      expect(element.html()).toContain('close');
    });

    it('clicking the close button should dismiss the notification', () => {
      expect(element.html()).toContain('close');

      const button = angular.element(element.find('a'));

      button[0].dispatchEvent(new Event('click'));
      outerScope.$apply();

      expect(element.html()).not.toContain('close');
      expect(element.html()).not.toContain('test123');
    });
  });

  describe('without a header', () => {
    injectElement('<ws-notification dismissible="false" actionable="false" type="neutral" body-string="test123"/>');

    it('should not render a header', () => {
      expect(element.html()).toContain('test123');
      expect(element.html()).not.toContain('h2');
    });
  });

  describe('with a header', () => {
    injectElement('<ws-notification header-string="testheader" dismissible="false" actionable="false" type="neutral" body-string="test123"/>');

    it('should render a header', () => {
      expect(element.html()).toContain('test123');
      expect(element.html()).toContain('h2');
      expect(element.html()).toContain('testheader');
    });
  });
});
