import React from 'react';
import renderer from 'react-test-renderer';
import applyMarkdown, {useStyle} from './apply-markdown';

const renderFragments = frags => {
  return renderer.create(<div>{frags}</div>).toJSON().children;
};

const coolResult = [
  'be ',
  {
    type: 'b',
    props: {
      className: 'info'
    },
    children: ['cool']
  }
];

describe('applyMarkdown()', () => {
  it('should return fn that returns whole string', () => {
    expect(applyMarkdown()('dog')).toBe('dog');
    expect(applyMarkdown(null)('cat')).toBe('cat');
  });

  it('should handle string property', () => {
    let mark = applyMarkdown({'*': 'info'}),
      frag = renderFragments(mark('be *cool*'));

    expect(frag).toEqual([
      'be ',
      {
        type: 'span',
        props: {
          className: 'info'
        },
        children: ['cool']
      }
    ]);
  });

  it('should handle react element', () => {
    let mark = applyMarkdown({'*': <b className="info" />}),
      frag = renderFragments(mark('be *cool*'));
    expect(frag).toEqual(coolResult);
  });

  it('should handle class component', () => {
    class Component extends React.Component {
      render() {
        return <b className="info">{this.props.children}</b>;
      }
    }
    let mark = applyMarkdown({'*': Component}),
      frag = renderFragments(mark('be *cool*'));
    expect(frag).toEqual(coolResult);
  });

  it('should handle functional component', () => {
    const Component = ({children}) => <b className="info">{children}</b>;
    let mark = applyMarkdown({'*': Component}),
      frag = renderFragments(mark('be *cool*'));
    expect(frag).toEqual(coolResult);
  });

  it('should handle composition component', () => {
    const Component = props => <b {...props}>{props.children}</b>;
    let mark = applyMarkdown({'*': <Component className="info" />}),
      frag = renderFragments(mark('be *cool*'));
    expect(frag).toEqual(coolResult);
  });
});

describe('.useStyle', () => {
  it('should return null', () => {
    expect(useStyle()).toBe(null);
    expect(useStyle(null, null)).toBe(null);
  });

  it('should return spec object', () => {
    const spec = {
      '*': 'info'
    };
    expect(useStyle(undefined, spec)).toBe(spec);
    expect(useStyle(null, spec)).toBe(spec);
  });

  it('should transform style into spec', () => {
    const style = {
      info: 'gzVnrw',
      warn: 'jHmeYN'
    };
    const spec = {
      '*': 'info',
      '!': 'warn'
    };
    expect(useStyle(style, spec)).toEqual({
      '!': 'jHmeYN',
      '*': 'gzVnrw'
    });
  });
});

@@ -159,19 +159,8 @@
 s =%3E
- %7B%0A  return
  ren
@@ -176,48 +176,23 @@
 ate(
-%3Cdiv%3E%7Bfrags%7D%3C/div%3E).toJSON().children;%0A%7D
+frags).toJSON()
 ;%0A%0Ac
@@ -837,207 +837,8 @@
 );%0A%0A
-  it('should handle react element', () =%3E %7B%0A    let mark = applyMarkdown(%7B'*': %3Cb className=%22info%22 /%3E%7D),%0A      frag = renderFragments(mark('be *cool*'));%0A    expect(frag).toEqual(coolResult);%0A  %7D);%0A%0A
   it
@@ -1277,32 +1277,32 @@
 %7Bchildren%7D%3C/b%3E;%0A
+
     let mark = a
@@ -1429,290 +1429,8 @@
  %7D);
-%0A%0A  it('should handle composition component', () =%3E %7B%0A    const Component = props =%3E %3Cb %7B...props%7D%3E%7Bprops.children%7D%3C/b%3E;%0A    let mark = applyMarkdown(%7B'*': %3CComponent className=%22info%22 /%3E%7D),%0A      frag = renderFragments(mark('be *cool*'));%0A    expect(frag).toEqual(coolResult);%0A  %7D);
 %0A%7D);

update tests