mercredi 11 mars 2020

React - Testing controller component with jest/enzyme

I'm in bit of a dead-end as I'm not sure how I should go about testing this specific "component". So basically, I have a controller component which is a top-level component. It doesn't take in any props, and it is rendered by a route component. The controller component has several functions, which some are passed into a child component and are triggered by event handlers.

Additionally, the controller component uses an API that is attached to the global window object. The API takes in a callback function which then will be called when you call certain methods on the API, after the methods have been run. Right now, I have no idea how I should try to test the controller. I have tested all child components and verified that everything works, but some of these functions within the controller component would be crucial to test that they actually do work.

const MyController = () => {
const [api, setApi] = useState(null)

useEffect(() => {
const globalApi = window.globalApi
setApi(globalApi)
init()
}, [])

function callBack(e) {
    console.log(e)
}

function init() {
        api.init(callBack)
}

function close() {
    api.close()
}

return (
    <MyComponent 
    close={close}
    />
)

}

Aucun commentaire:

Enregistrer un commentaire