May 2019
Intermediate to advanced
496 pages
10h 38m
English
Link components are one way to cause a URL change, but clearly, they require the user to click on them. To programmatically change a URL, we need to use the history prop that React Router gives us. This has a push function, which we can test using a standard spy.
In the test that follows, App is passed a history object, which replicates the object that a Router component would give us:
describe('App', () => { let historySpy; beforeEach(() => { historySpy = jest.fn(); }); it('navigates to / when AppointmentFormLoader is saved', () => { render(<App history={{ push: historySpy }} />); const onSave = routeFor('/addAppointment').props.render().props.onSave; onSave(); expect(historySpy).toHaveBeenCalledWith('/'); ...Read now
Unlock full access