Testing private routes
pulse00 opened this issue · 3 comments
I'm trying to test the privateRoute
component to make sure redirectToLoginWithMessage
is called and the loader is rendered when not authenticated etc.
I've managed to test the components markup, but i can't find a way to test the call to redirectToLoginWithMessage
.
Here`s what i have so far:
import { describeWithDOM, mount } from 'enzyme';
import { expect, spy } from '../../utils/chai';
import { createStore } from 'redux';
import React from 'react';
import privateRoute from 'router/privateRoute';
import { PrivatePage } from 'ui/PrivatePage';
import initStore from 'config/store';
describeWithDOM('<PrivatePage />', () => {
it('should render loader when not authenticated', () => {
const WrappedRoute = privateRoute(PrivatePage);
const store = initStore();
const component = mount(<WrappedRoute store={store}></WrappedRoute>);
// if the state is unauthenticated, loader should be visible and the
// private component should not be there
expect(component.find('.loader')).to.have.length(1);
expect(component.find('.privatePage')).to.have.length(0);
// TODO: how can we test that `redirectToLoginWithMessage` has been called?
});
);
});
In your ComponentTest you are importing the stateless component, so you're able to inject the action under test as a prop. But how does this work for connected components such as the privateRoute
?
I guess we should separate the connected version of this function from the base version just like we did for MyComponent.
I like that you are adding new tests to the application! Would you mind turn this into a PR?
I guess we should separate the connected version of this function from the base version just like we did for MyComponent.
I've tried this already, however i lost the reference to the Wrapped
component then, but i guess it's just my lack of es6 experience :) I'll look into this approach some more, thanks!
I like that you are adding new tests to the application! Would you mind turn this into a PR?
Sure! As soon as i fixed the above issue, i'll send you a PR.