import {ReactElement} from 'react'
import {render, RenderResult} from '@testing-library/react'
import $ from 'jquery'
import {printDomElem} from './test-util-print-dom-elem'
import {DomQueryPreset} from './test-util-dom-query-preset'

export class BaseHelperV2<Props> {
    preset = new DomQueryPreset()

    eq = this.preset.eq

    incl = this.preset.incl

    $ = $

    constructor(
        renderView: (
            update: (props: Partial<Props>) => void,
            props: Props
        ) => ReactElement,
        public props: Record<string, any> = {}
    ) {
        // eslint-disable-next-line prefer-const
        let wrapper: RenderResult<any>
        const update = (nextProps: Partial<Props>) => {
            Object.assign(props, nextProps)
            wrapper.rerender(renderView(update, props as Props))
        }

        const result = renderView(update, props as Props)
        // @ts-ignore type
        wrapper = render(result)
    }

    expectBody = () => {
        return expect(
            printDomElem(document.body)
        )
    }
}
