Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mocking UiRouter Classes with Jest #877

Closed
jennyeg opened this issue Apr 29, 2021 · 4 comments
Closed

Mocking UiRouter Classes with Jest #877

jennyeg opened this issue Apr 29, 2021 · 4 comments
Labels

Comments

@jennyeg
Copy link

jennyeg commented Apr 29, 2021

Hi! In our react app we are using UiRouter. When our team has tried to create unit tests, using jest and enzyme, for our components that are involved with the UiRouter we are getting several errors from the UiRouter. I was wondering if you could provide me with some information on how to appropriately mock the UiRouter in a unit test. The error our team is facing looks like the following:

TypeError: Cannot read property 'rules' of undefined

This is occurring in our router file where we are calling router.urlService.rules.initial( {state: "fakeState"} );

How can we mock the urlService class and the UrlRulesAPI implemented by the urlService class that is provided by the UiRouter so that our unit tests will pass successfully?

Note: we are currently already mocking the uirouter module as follows:
jest.mock('@uirouter/react');

Thank you for your help!

@christopherthielen
Copy link
Member

It depends what you're trying to test, but I've had success mocking just the uirouter.stateService.go function.

For example:

it('triggers a transition to target state', async () => {
const goSpy = jest.spyOn(router.stateService, 'go');
const rendered = mountInRouter(
<UISref to="state2">
<a data-testid="anchor" />
</UISref>
);
rendered.getByTestId('anchor').click();
expect(goSpy).toHaveBeenCalledTimes(1);
expect(goSpy).toHaveBeenCalledWith('state2', expect.anything(), expect.anything());
});

@muz3
Copy link

muz3 commented Aug 19, 2021

@christopherthielen, thanks. this is helpful.

It depends what you're trying to test, but I've had success mocking just the uirouter.stateService.go function.

For example:

it('triggers a transition to target state', async () => {
const goSpy = jest.spyOn(router.stateService, 'go');
const rendered = mountInRouter(
<UISref to="state2">
<a data-testid="anchor" />
</UISref>
);
rendered.getByTestId('anchor').click();
expect(goSpy).toHaveBeenCalledTimes(1);
expect(goSpy).toHaveBeenCalledWith('state2', expect.anything(), expect.anything());
});

@stale
Copy link

stale bot commented Apr 16, 2022

This issue has been automatically marked as stale because it has not had
recent activity. It will be closed if no further activity occurs.

This does not mean that the issue is invalid. Valid issues may be reopened.

Thank you for your contributions!

@stale stale bot added the stale label Apr 16, 2022
@stale
Copy link

stale bot commented May 1, 2022

This issue has been automatically closed.

@stale stale bot closed this as completed May 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants