scottrippey / next-router-mock

Mock implementation of the Next.js Router
MIT License
400 stars 38 forks source link

Make `reload` a mock function #124

Closed DurchblickerFabian closed 4 months ago

DurchblickerFabian commented 6 months ago

It would be useful if one could use the following snippet to check whether reload was called or not: expect(mockRouter.reload).toHaveBeenCalled();. Right now, this is not possible ("Matcher error: received value must be a mock or spy function").

In my opinion reload does not need any functionality, but if possible, it would be nice to have the possibility to check if it has been called. (Maybe also applicable for the other functions like push, because right now they are all raw functions)

DurchblickerFabian commented 4 months ago

Just an addition: This is probably not very easily solve-able, since introducing spies would limit this library to a specific testing framework if not using a platform agnostic spy/mock library like sinon. I don't know if that would a great solution, basically forcing users of this library to use a specific spy/mock library. Maybe an addition in the README how to possibly set this up by one-self for example with Jest and/or sinon would be great.

DurchblickerFabian commented 4 months ago

After re-evaluation, a user of this library could just register a spy and check if the spy is called. (e.g. const spy = jest.spyOn(mockRouter, 'reload'); and expect(spy).toHaveBeenCalled(); respectively)

Closing this.