import React from 'react';
import Renderer from 'react-test-renderer';
import Coroutine from '../src/Coroutine';

describe('Coroutine', async () => {
  it('should render empty body until coroutine is resolved', async () => {
    const render = async () => <p>test</p>;
    const TestComponent = Coroutine.create(render);
    const tree = Renderer.create(<TestComponent />);

    const initial = Renderer.create(<noscript />);
    expect(tree.toJSON()).toEqual(initial.toJSON());

    const success = await Renderer.create(<p>test</p>);
    expect(tree.toJSON()).toEqual(success.toJSON());
  });

  it('should pass initial information', async () => {
    const render = async ({ number }) => <p>{ number }</p>;
    const variables = () => ({ number: 13 });
    const TestComponent = Coroutine.create(render, variables);
    const tree = Renderer.create(<TestComponent />);

    const success = await Renderer.create(<p>{13}</p>);
    expect(tree.toJSON()).toEqual(success.toJSON());
  });

  it('should re-render the component if variables are updated', async () => {
    function getVariables() {
      return { number: 0 };
    }

    async function render({ number, forceUpdate }) {
      try {
        return <p>{ number }</p>;
      } finally {
        await true;
        if (number === 0) forceUpdate({ number: 1 });
      }
    }

    const TestComponent = Coroutine.create(render, getVariables);
    const tree = await Renderer.create(<TestComponent />);

    const success = await Renderer.create(<p>{0}</p>);
    expect(tree.toJSON()).toEqual(success.toJSON());

    const updated = await Renderer.create(<p>{1}</p>);
    expect(tree.toJSON()).toEqual(updated.toJSON());
  });
});

@@ -226,34 +226,46 @@
     
-const render = async () =%3E
+async function render() %7B%0A      return
  %3Cp%3E
@@ -270,24 +270,30 @@
 p%3Etest%3C/p%3E;%0A
+    %7D%0A
     const Te
@@ -669,109 +669,149 @@
     
-const render = async (%7B number %7D) =%3E %3Cp%3E%7B number %7D%3C/p%3E;%0A    const variables = () =%3E (%7B number: 13 %7D);
+function getVariables() %7B%0A      return %7B number: 13 %7D;%0A    %7D;%0A%0A    async function render(%7B number %7D) %7B%0A      return %3Cp%3E%7B number %7D%3C/p%3E;%0A    %7D%0A
 %0A   
@@ -858,17 +858,20 @@
 render, 
-v
+getV
 ariables

make tests consistent