import dom from "../test-setup";
import Markdown from "../../src/components/markdown";
import React, {addons} from 'react/addons';
const TestUtils = addons.TestUtils;

import chai from 'chai';
import spies from 'chai-spies';
chai.use(spies);
let {expect, spy} = chai;

describe('Markdown', () => {
    var markdown;

    beforeEach(() => {
        markdown = new Markdown();
    });

    it('exists', () => {
        expect(Markdown).to.be.ok;
    });

    it('#getDefaultProps', () => {
        var defaultProps = (Markdown.defaultProps);
        expect(Markdown.defaultProps).to.deep.equal({
            tag: 'div',
            content: '',
            inline: false,
            className: ''
        });
    });

    describe('#replaceSymbols', () => {
        it('replaces #hashtags with hashtag links', () => {
            var tagLink = markdown.replaceSymbols('#test');
            expect(tagLink).to.equal("<a href='#/talk/search?query=test'>#test</a>");
        });

        it('replaces ^subject mentions with subject links', () =>{
            markdown.getParams = () => {
                return {
                    owner: 'test',
                    name: 'project'
                };
            };

            var subjectLink = markdown.replaceSymbols('^123456');
            expect(subjectLink).to.equal("<a href='#/projects/test/project/talk/subjects/123456'>test/project - Subject 123456</a>");
        });

        it('does not format subject Ids when not in a routed context', () =>{
            markdown.getParams = Function.prototype;

            var subjectLink = markdown.replaceSymbols('^123456');
            expect(subjectLink).to.equal("123456");
        });

        it('replaces @ownerslug/project-slug^subject_id mentions with links', () => {
            var projectSubjectLink = markdown.replaceSymbols('@owner/project-d^123456');

            expect(projectSubjectLink).to.equal("<a href='#/projects/owner/project-d/talk/subjects/123456'>owner/project-d - Subject 123456</a>");
        });
    });

    describe('#markdownify', () => {
        it('renders markdown', () => {
            var md = markdown.markdownify('# test header');
            expect(md).to.equal('<h1>test header</h1>\n');
        });
    });

    describe('#getHtml', () =>{
        var md = TestUtils.renderIntoDocument(React.createElement(Markdown, { className: 'MyComponent'}, 'Test text'));

        it('returns the formatted html', () => {
            let html = md.getHtml()
            expect(html).to.equal('<p>Test text</p>\n')
        })

        it('renders bare child content on error', () =>{
            md.replaceSymbols = () => {
                throw new Error("fail")
            }
            let html = md.getHtml()
            expect(html).to.equal('Test text')
        })
    })

    describe('#render', () => {
        var editor, md
        before(() => {
            editor = React.createElement(Markdown, { className: 'MyComponent'}, 'Test children')
            md = TestUtils.renderIntoDocument(editor);
        })

        it('renders', () => {
            var markdownDiv = TestUtils.findRenderedDOMComponentWithTag(md, 'div');
            expect(markdownDiv.props.dangerouslySetInnerHTML.__html).to.equal('<p>Test children</p>\n');
        });

        it('calls getHtml in render', () => {
            let replaceSymbolsSpy = spy.on(md, 'getHtml')
            md.render()
            expect(replaceSymbolsSpy).to.have.been.called()
        });
    });
});


@@ -2597,34 +2597,35 @@
 on error', () =%3E
+ 
 %7B%0A
-
             md.r
@@ -2951,32 +2951,44 @@
 e: 'MyComponent'
+, tag: 'div'
 %7D, 'Test childre
@@ -3481,30 +3481,370 @@
 ed()%0A        %7D);
+%0A%0A        it('returns a react component, with customizable tag', () =%3E%7B%0A            var editor = React.createElement(Markdown, %7BclassName: 'PMarkdown', tag: 'p'%7D, 'Test p tag')%0A            var md = TestUtils.renderIntoDocument(editor);%0A%0A            let renderValue = md.render()%0A            expect(renderValue.type).to.equal('p')%0A        %7D)
 %0A    %7D);%0A%7D);%0A%0A
@@ -3838,13 +3838,12 @@
     %7D);%0A
+
 %7D);%0A
-%0A

Test that tag is customizable