score:3

Accepted answer

turns out that my first answer didn't work for me at the end... i kept running the test and fixing whatever it was complaining about, but there was always something else...

so with some help from a colleague, the solution was to stop react echarts to call echarts by mocking the function triggering the rendering.

let spy: any

beforeall(() => {
    spy = jest.spyon(echarts, 'getinstancebydom').mockimplementation(() => {
        return {
            hideloading: jest.fn(),
            setoption: jest.fn(),
            showloading: jest.fn()
        }
    })
})
afterall(() => {
    spy.mockrestore()
})
it('renders show correctly', () => {
    const tree = renderer
        .create(
            <show />
        )
        .tojson()
    expect(tree).tomatchsnapshot()
})

in our case we were not interested in actually testing the canvas rendering part, so that worked for us.

score:0

yes, i had to mock the ref object:

function createnodemock(element: any) {
    return {
        getsomething: jest.fn(() => 'pizza'),
        setsomething: jest.fn(),
        property: null,
    }
}
it('renders show correctly', () => {
    const options = { createnodemock }
    const tree = renderer
        .create(
            <show/>,
            options
        )
        .tojson()
    expect(tree).tomatchsnapshot()
})

every time i ran into an error running the test, i went to where the method or property was called, console logged, looked what the value was while in the browser and set the data in the mockdata. maybe there is a better way of doing this, but it got it done...


Related Query

More Query from same tag