
import React from 'react';
import {expect, sinon, mount} from 'test';
import EventHandler from 'lib/EventHandler';
import * as l10n from 'lib/l10n';
import Spinner from 'components/util/Spinner';
import Alert from 'components/util/Alert';
import EncryptedForm from 'components/encrypted-form/encryptedForm';

l10n.mapToLocal();

describe('Encrypt Form tests', () => {
  const sandbox = sinon.createSandbox();

  const mockPort = () => {
    const portMock = {
      _events: {
        emit: [],
        on: [],
        send: []
      },
      on: event => portMock._events.on.push(event),
      emit: event => portMock._events.emit.push(event),
      send: event => {
        portMock._events.send.push(event);
        return new Promise(resolve => {
          resolve(event);
        });
      }
    };
    sandbox.stub(EventHandler, 'connect').returns(portMock);
  };

  const setup = propOverrides => {
    const props = {
      id: 'encrypted-form-test',
      ...propOverrides
    };

    const wrapper = mount(<EncryptedForm {...props} />);

    return {
      props,
      wrapper,
    };
  };

  beforeEach(() => {
    mockPort();
  });

  afterEach(() => {
    sandbox.restore();
  });

  it('should render', () => {
    const {wrapper} = setup();
    expect(wrapper.exists()).to.equal(true);
  });

  describe('Do some integration tests', () => {
    it('should initialize the component and should wait on the form definition event', () => {
      const {wrapper} = setup();
      const component = wrapper.instance();
      expect(component.port._events.on).to.include.members(['encrypted-form-definition', 'error-message',
        'terminate', 'encrypted-form-submit', 'encrypted-form-submit-cancel']);
      expect(component.port._events.emit).to.include.members(['encrypted-form-init']);
      expect(wrapper.find(Spinner).exists()).to.equal(true);
    });

    it('should show the sandbox form component and the form materials', () => {
      const {wrapper} = setup();
      const component = wrapper.instance();
      const event = {
        formDefinition: '<form class="needs-validation" data-recipient="test@mailvelope.com" data-action="https://demo.mailvelope.com/form/"><div class="form-group"><label for="validationCustomSimple">How are you?</label><div class="input-group"><input class="form-control" value="cofveve" name="validationCustomSimple" required="" type="text" id="validationCustomSimple"><div class="invalid-feedback">Please say something?</div></div></div></form>',
        formEncoding: 'html',
        formAction: null,
        formRecipient: 'test@mailvelope.com',
        recipientFpr: 'AA1E 0177 4BDF 7D76 A45B DC2D F11D B125 0C3C 3F1B'
      };
      component.showForm(event);
      wrapper.update();

      expect(wrapper.find('iframe#formSandbox').exists()).to.equal(true);
      expect(wrapper.find('.formWrapper').exists()).to.equal(true);
      expect(wrapper.find('button.btn').exists()).to.equal(true);
    });
    it('should show error message on error-message event', () => {
      const {wrapper} = setup();
      const component = wrapper.instance();
      const error = {
        message: 'Error message!'
      };
      component.showErrorMsg(error);
      wrapper.update();

      const alert = wrapper.find(Alert);
      expect(alert.exists()).to.equal(true);
      expect(alert.text()).to.equal(error.message);
    });
  });
});

@@ -1934,26 +1934,29 @@
 materials', 
-()
+done 
  =%3E %7B%0A      
@@ -2713,24 +2713,51 @@
 orm(event);%0A
+      setTimeout(() =%3E %7B%0A  
       wrappe
@@ -2764,25 +2764,26 @@
 r.update();%0A
-%0A
+  
       expect
@@ -2842,32 +2842,34 @@
 al(true);%0A      
+  
 expect(wrapper.f
@@ -2906,32 +2906,34 @@
 to.equal(true);%0A
+  
       expect(wra
@@ -2974,32 +2974,63 @@
 to.equal(true);%0A
+        done();%0A      %7D, 300);%0A
     %7D);%0A    it('
@@ -3072,34 +3072,36 @@
 message event', 
-()
+done
  =%3E %7B%0A      cons
@@ -3269,16 +3269,43 @@
 error);%0A
+      setTimeout(() =%3E %7B%0A  
       wr
@@ -3320,17 +3320,18 @@
 date();%0A
-%0A
+  
       co
@@ -3361,32 +3361,34 @@
 d(Alert);%0A      
+ 
+ 
 expect(alert.exi
@@ -3406,24 +3406,26 @@
 qual(true);%0A
+  
       expect
@@ -3460,24 +3460,55 @@
 r.message);%0A
+        done();%0A      %7D, 300);%0A
     %7D);%0A  %7D)

Fix encrypted form unit test to work with reactstrap modal