import React from 'react';
import {mount, render} from '../../build/enzyme';
import {Highlighter, HIGHLIGHT_TYPE} from './Highlighter';
import {Store} from '../data/Store';

describe('Highlighter', () => {
    const opts = {
        highlightRegexp: /highlight/gi,
        filterRegexp: /found/gi,
        highlight: text => `<a>${text}</a>`
    };

    it('check default emphazis', () => {
        const instance = mount(<Highlighter />).instance();
        expect(instance.emphazis('matchedText')).toBe('<strong>matchedText</strong>');
    });

    it('check alternate emphazis', () => {
        const instance = mount(
            <Highlighter listHighlightType={HIGHLIGHT_TYPE.BACKGROUND} />
        ).instance();
        expect(instance.emphazis('matchedText')).toBe(
            '<span class="rc-highlight">matchedText</span>'
        );
    });

    it('check custom emphazis', () => {
        const instance = mount(<Highlighter highlight={opts.highlight} />).instance();
        expect(instance.emphazis('matchedText')).toBe('<a>matchedText</a>');
    });

    it('check default render', () => {
        const rendered = render(<Highlighter>The child text</Highlighter>);
        expect(rendered.html()).toBe('<span>The child text</span>');
    });

    it('check default render with title', () => {
        const rendered = render(<Highlighter title={'The Title'}>The child text</Highlighter>);
        expect(rendered.html()).toBe('<span title="The Title">The child text</span>');
    });

    it('check render with rich text', () => {
        const rendered = render(
            <Highlighter {...opts}>{'text <i>highlight</i> <b>found</b>'}</Highlighter>
        );
        expect(rendered.html()).toBe('<span>text <i><a>highlight</a></i> <b>found</b></span>');
    });

    it('should not highlight, becouse child has not been found', () => {
        const rendered = render(<Highlighter {...opts}>highlight</Highlighter>);
        expect(rendered.html()).toBe('<span>highlight</span>');
    });

    it('should highlight, becouse child has been found', () => {
        const rendered = render(<Highlighter {...opts}>highlight found</Highlighter>);
        expect(rendered.html()).toBe('<span><a>highlight</a> found</span>');
    });

    describe('Extra highlight cases', () => {
        it('should highlight escaped ampersand', () => {
            const newOpts = {
                ...opts,
                filterRegexp: /&/gi,
                highlightRegexp: /&/gi
            };
            const rendered = render(<Highlighter {...newOpts}>highlight & found</Highlighter>);
            expect(rendered.html()).toBe('<span>highlight <a>&amp;</a> found</span>');
        });

        it('should highlight escaped double quotes', () => {
            const newOpts = {
                ...opts,
                filterRegexp: /"/gi,
                highlightRegexp: /"/gi
            };
            const rendered = render(<Highlighter {...newOpts}>highlight "found"</Highlighter>);
            expect(rendered.html()).toBe('<span>highlight <a>&quot;</a>found<a>&quot;</a></span>');
        });

        it('should not highlight specifically marked text', () => {
            const newOpts = {
                ...opts,
                filterRegexp: /high/gi,
                highlightRegexp: /high/gi
            };
            const rendered = render(
                <Highlighter {...newOpts}>{'text highlight <nhl>highlight</nhl>'}</Highlighter>
            );
            expect(rendered.html()).toBe('<span>text <a>high</a>light <nhl>highlight</nhl></span>');
        });
    });

    describe('Default Store regexps', () => {
        it('should work too', () => {
            const newOpts = {
                ...opts,
                filterRegexp: Store.searchStringToRegExp('text to match'),
                highlightRegexp: Store.getHighlightRegExp('text to match')
            };
            const rendered = render(
                <Highlighter {...newOpts}>highlight found in text to match</Highlighter>
            );
            expect(rendered.html()).toBe(
                '<span>highlight found in <a>text</a> <a>to</a> <a>match</a></span>'
            );
        });

        it('should work on rich text', () => {
            const newOpts = {
                ...opts,
                filterRegexp: Store.searchStringToRegExp('text to match'),
                highlightRegexp: Store.getHighlightRegExp('text to match')
            };
            const rendered = render(
                <Highlighter {...newOpts}>{'highlight found in text <b>to</b> match'}</Highlighter>
            );
            expect(rendered.html()).toBe(
                '<span>highlight found in <a>text</a> <b><a>to</a></b> <a>match</a></span>'
            );
        });
    });
});
