I am using next-routes
and in my React component I am using below useEffect
code block to detect Router
change event:
useEffect(() => {
// THIS BLOCK WILL BE EXECUTED WHEN THE COMPONENT IS ALREADY MOUNTED AND SCREEN WOULD BE REDNDERED IN WHEN ROUTED CLIENT SIDE
const handleRouterChangeComplete = url => {
// console.log('INNNN handleRouterChangeComplete url = ', url);
// MY REST OF THE LOGIC HERE
};
// THIS BLOCK WILL BE EXECUTED WHEN THE SCREEN WOULD BE REDNDERED IN WHEN ROUTED CLIENT SIDE
Router.events.on('routeChangeComplete', handleRouterChangeComplete);
return () => {
Router.events.off('routeChangeComplete', handleRouterChangeComplete);
};
}, []);
I am using jest
to write unit test case for this component and facing below error:
TypeError: Cannot read property 'on' of undefined
183 |
184 | // THIS BLOCK WILL BE EXECUTED WHEN THE SCREEN WOULD BE REDNDERED IN WHEN ROUTED CLIENT SIDE
> 185 | Router.events.on('routeChangeComplete', handleRouterChangeComplete);
Can anybody please help me with mocking the Router Change
event?
Thanks
For anyone who is stuck with testing the Router change events in nextjs here is an example.
You can use Router.events.emit
to emit the event from your test then assert the callback you are expecting to get fired.
Following is the scenario I am using in the example, however you can modify it to your needs.
Scenario: In my main app, I am using NProgress to display a progress bar on each route change. The custom app looks like this
import Router from 'next/router';
import NProgress from 'nprogress';
Router.events.on('routeChangeStart', () => NProgress.start());
Router.events.on('routeChangeComplete', () => NProgress.done());
Router.events.on('routeChangeError', () => NProgress.done());
export default CustomApp() {
...
}
Now, in my test, I want to test that the callbacks are called appropriately. The jest test looks like this:
import Router from 'next/router;
import NProgress from 'nprogress;
import { render } from '@testing-library/react';
import CustomApp from 'pages/_app';
const mockStart = jest.spyOn(NProgress, 'start');
const mockDone = jest.spyOn(NProgress, 'done');
it('starts nprogress on router change start', () => {
render(<CustomApp />)
Router.events.emit('routeChangeStart');
expect(mockStart).toHaveBeenCalled();
});
it('ends nprogress on router change complete', () => {
render(<CustomApp />)
Router.events.emit('routeChangeComplete');
expect(mockDone).toHaveBeenCalled();
});
You can trigger the relevant change event by using Router.events.emit('event-name')
;
The code was taken from the router implementation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With