import $ from 'jquery';
import ReactDOM from 'react-dom';
import {
  runnerAfterEach,
  runnerBeforeEach,
  runnerDescribe,
  runnerIt
} from '../mocha-runner';
import expect from './helpers/expect';

/**
 * @param {String} name
 * @param {() => void} definition
 */
export function describe(name, definition) {
  runnerDescribe(name, () => {
    definition();

    // Unmount the currently mounted component after each test.
    afterEach(function() {
      unmount();
    });
  });
}

/**
 * @param {String} name
 * @param {() => Promise|void} definition
 */
export function it(name, definition) {
  runnerIt(name, definition);
}

/**
 * @param {() => Promise|void} definition
 */
export function beforeEach(definition) {
  runnerBeforeEach(definition);
}

/**
 * @param {() => Promise|void} definition
 */
export function afterEach(definition) {
  runnerAfterEach(definition);
}

export { expect };

const _container = document.createElement('div');

/**
 * Render the given component.
 *
 * @param {ReactElement} element
 *
 * @returns {jQuery} The component's root DOM node wrapped in jQuery.
 */
export function $render(element) {
  ReactDOM.render(element, _container);

  // Return the first (and only) child in the container wrapped in jQuery.
  return $(_container).children().eq(0);
}

/**
 * Unmount the currently mounted component.
 */
export function unmount() {
  // unmountComponentAtNode will return `false` if there was no component
  // mounted at the given node. That can happen when the component was
  // unmounted inside a test i.e. to test cleanup logic.
  ReactDOM.unmountComponentAtNode(_container);
}

@@ -194,16 +194,41 @@
 pect';%0A%0A
+let componentContainer;%0A%0A
 /**%0A * @
@@ -369,136 +369,134 @@
     
-definition();%0A%0A    // Unmount the currently mounted component after each test.%0A    afterEach(func
+beforeEach(() =%3E %7B%0A      componentContainer = document.createElement('div');%0A    %7D);%0A%0A    defini
 tion()
- %7B
+;%0A
 %0A    
-  unmount();%0A    %7D
+afterEach(unmount
 );%0A 
@@ -923,92 +923,85 @@
 %7D;%0A%0A
-const _container = document.createElement('div');%0A%0A/**%0A * Render the given component
+/**%0A * Render the given component in a freshly created detached DOM container
 .%0A *
@@ -1173,18 +1173,26 @@
 lement, 
-_
 c
+omponentC
 ontainer
@@ -1281,18 +1281,26 @@
 eturn $(
-_
 c
+omponentC
 ontainer
@@ -1642,10 +1642,18 @@
 ode(
-_
 c
+omponentC
 onta

Create the component container before each test