// react-addons-shallow-compare and react-addons-test-utils are required for enzyme
// jshint esversion: 6

import {configure} from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import ReactWrapper from 'enzyme/build/ReactWrapper';
import ShallowWrapper from 'enzyme/build/ShallowWrapper';
import ReactDOMServer from 'react-dom/server';
import cheerio from 'cheerio';

configure({adapter: new Adapter()});

const TEST_ID_DELIMITER = '.';
const TEST_ID_ATTRIBUTE = 'data-test-automation-id';

class ReactWrapperWMU extends ReactWrapper {
    findByTestAutomation(path) {
        let w: any = this;
        path = path.split(TEST_ID_DELIMITER);
        while (path.length && w) {
            w = w.find({
                [TEST_ID_ATTRIBUTE]: path.shift()
            });
        }
        return w;
    }
}

export function render(node) {
    const html = ReactDOMServer.renderToStaticMarkup(node);
    return cheerio.load(html).root();
}

export function shallow(node, options) {
    return new ShallowWrapper(node, null, options);
}

export function mount(node, options) {
    const ReactWrapperWMUTS: any = ReactWrapperWMU;
    return new ReactWrapperWMUTS(node, null, options);
}
