/* eslint-disable react/prop-types, react/display-name, jsx-a11y/no-static-element-interactions */
import React from 'react';
import { mount } from 'enzyme';

import GenericFormContainer from 'control/components/recipes/GenericFormContainer';

describe('<GenericFormContainer>', () => {
  const props = {
    form: () => <div />,
    formAction: async () => true,
    onFailure: () => {},
    onSuccess: () => {},
    formProps: {},
  };

  it('should work', () => {
    const wrapper = () => mount(<GenericFormContainer {...props} />);

    expect(wrapper).not.toThrow();
  });

  it('should nest the `form` prop as a child', () => {
    const CustomEl = () => <div id="test">Custom</div>;

    const wrapper = mount(<GenericFormContainer {...props} form={CustomEl} />);

    expect(wrapper.find('#test').length).toBe(1);
    expect(wrapper.find('#test').text()).toContain('Custom');
  });

  it('should fire the formAction on submission', () => {
    const CustomEl = ({ onSubmit }) => <div onClick={onSubmit}>Test</div>;
    let fired = false;
    const wrapper = mount(
      <GenericFormContainer
        {...props}
        form={CustomEl}
        formAction={() => {
          fired = true;
        }}
      />,
    );

    wrapper.find(CustomEl).simulate('click');
    expect(fired).toBe(true);
  });

  it('should handle the formAction failing', () => {
    const CustomEl = ({ onSubmit }) => <div onClick={onSubmit}>Test</div>;
    let failed = false;
    const wrapper = mount(
      <GenericFormContainer
        {...props}
        form={CustomEl}
        onFailure={() => {
          failed = true;
        }}
        formAction={() => {
          throw new Error('Form action failed');
        }}
      />,
    );

    wrapper.find(CustomEl).simulate('click');
    expect(failed).toBe(true);
  });

  it('should handle the formAction succeeding', async () => {
    const CustomEl = ({ onSubmit }) => <div onClick={onSubmit}>Test</div>;

    const success = new Promise(r => r(true));

    const wrapper = mount(
      <GenericFormContainer
        {...props}
        form={CustomEl}
        onSuccess={() => {
          success.resolve();
        }}
      />,
    );

    wrapper.find(CustomEl).simulate('click');
    expect(await success).toBe(true);
  });

  it('should pass the formProps object to the form element', () => {
    const CustomEl = custProps => <div id="test" {...custProps}>Custom</div>;
    const testProps = { test: 1, woo: 'hoo' };
    const wrapper = mount(
      <GenericFormContainer
        {...props}
        form={CustomEl}
        formProps={testProps}
      />,
    );

    expect(wrapper.find('#test').props().test).toBe(1);
    expect(wrapper.find('#test').props().woo).toBe('hoo');
  });
});

@@ -1935,32 +1935,49 @@
 t%7D%3ETest%3C/div%3E;%0A%0A
+    let resolve;%0A
     const succes
@@ -2001,15 +2001,24 @@
  =%3E 
-r(true)
+%7B resolve = r; %7D
 );%0A%0A
@@ -2156,24 +2156,16 @@
     
-success.
 resolve(
 );%0A 
@@ -2160,16 +2160,20 @@
 resolve(
+true
 );%0A     

Fix async test