I am facing an error TypeError: Cannot read property 'dispatchEvent' of null when trying to run an enzyme test to simulate onGridReady
of an ag-grid table.
My questions and places where I am getting stuck...
- How do I gracefully test the
onGridReady
function properly so that it doesn't error out and I can test that it has been called once ? - How do I test the other function
onGridSizeChanged
? - The Coverage report says I am missing the line
setTableData
to test - how do I test that line ?
This is how myfiles look
myTable.tsx
imports...
const myTable: React.FC = () => {
//intialize state variables
const onGridReady = ({ api }: { api: GridApi }) => {
setGridApi(api)
api.sizeColumnsToFit()
api.setHeaderHeight(10)
}
const onGridSizeChanged = () => {
gridApi?.sizeColumnsToFit()
}
useEffect(() => {
if (myTableData) {
if (selectedCountryName !== '') {
setTableData(objectCopy(myTableData))
}
}
}, [myTableData, selectedCountryName])
useEffect(() => {
// do some stuff
}, [businessDate])
return <div className={'my-table'}>
<Grid container spacing={12}>
<div>
<h1/>Title</h1>
</div>
<Grid item xs={12}>
<div data-testid="my-table-grid">
<AgGridReact
containerProps={{ style: { height: `${containerHeight}px` } }}
columnDefs={myTableColumns(moment(fromDate), moment(toDate))}
onGridReady={onGridReady}
onGridSizeChanged={onGridSizeChanged}
onModelUpdated={onGridSizeChanged}
rowData={tableData}
/>
</div>
</Grid>
</Grid>
</div >
}
export default myTable
myTable.test.tsx
import myTable from './myTable'
// other imports...
Enzyme.configure({ adapter: new Adapter() })
const onGridReady = jest.fn()
function setUpMyTableComponent() {
const wrapper = shallow(
<Provider store={store}>
<myTable >
<ColumnLayout>
<ColumnLayout>
<DataGrid onGridReady={onGridReady} />
</ColumnLayout>
</ColumnLayout>
</myTable >
</Provider>
)
return { wrapper }
}
describe('myTable', () => {
it('checking myTable', () => {
const { wrapper } = setUpMyTableComponent()
expect(wrapper).toBeDefined
expect(wrapper.find('.my-table')).toBeDefined
})
it('simulate onGridReady', async () => {
const { wrapper } = setUpMyTableComponent()
const aggrid = wrapper.find(AgGridReact)
dataGrid.simulate('onGridReady')
await tick()
expect(onGridReady).toHaveBeenCalled()
})
function tick() {
return new Promise(resolve => {
setTimeout(resolve, 0);
})
}
})