fleet/frontend/components/AuthenticatedRoutes/AuthenticatedRoutes.tests.jsx
Zach Wasserman 0670db66c4
Migrate JS tests to Jest and update libraries (#74)
- Move from Mocha to Jest for JS testing (Jest seems to have better support for
 'watching' tests and a more active community these days).
- Codemod existing tests to Jest syntax (using https://github.com/skovhus/jest-codemods)
- Fix some errors in tests that were previously hidden.
- Update Babel.
2020-12-01 10:15:12 -08:00

127 lines
3.1 KiB
JavaScript

import React from 'react';
import { mount } from 'enzyme';
import { Provider } from 'react-redux';
import AuthenticatedRoutes from './index';
import helpers from '../../test/helpers';
describe('AuthenticatedRoutes - component', () => {
const redirectToLoginAction = {
type: '@@router/CALL_HISTORY_METHOD',
payload: {
method: 'push',
args: ['/login'],
},
};
const redirectToPasswordResetAction = {
type: '@@router/CALL_HISTORY_METHOD',
payload: {
method: 'push',
args: ['/login/reset'],
},
};
const renderedText = 'This text was rendered';
const storeWithUser = {
auth: {
loading: false,
user: {
id: 1,
email: 'hi@thegnar.co',
force_password_reset: false,
},
},
routing: {
locationBeforeTransitions: {},
},
};
const storeWithUserRequiringPwReset = {
auth: {
loading: false,
user: {
id: 1,
email: 'hi@thegnar.co',
force_password_reset: true,
},
},
routing: {
locationBeforeTransitions: {},
},
};
const storeWithoutUser = {
auth: {
loading: false,
user: null,
},
routing: {
locationBeforeTransitions: {},
},
};
const storeLoadingUser = {
auth: {
loading: true,
user: null,
},
routing: {
locationBeforeTransitions: {},
},
};
it('renders if there is a user in state', () => {
const { reduxMockStore } = helpers;
const mockStore = reduxMockStore(storeWithUser);
const component = mount(
<Provider store={mockStore}>
<AuthenticatedRoutes>
<div>{renderedText}</div>
</AuthenticatedRoutes>
</Provider>,
);
expect(component.text()).toEqual(renderedText);
});
it('redirects to reset password is force_password_reset is true', () => {
const { reduxMockStore } = helpers;
const mockStore = reduxMockStore(storeWithUserRequiringPwReset);
mount(
<Provider store={mockStore}>
<AuthenticatedRoutes>
<div>{renderedText}</div>
</AuthenticatedRoutes>
</Provider>,
);
expect(mockStore.getActions()).toContainEqual(redirectToPasswordResetAction);
});
it('redirects to login without a user', () => {
const { reduxMockStore } = helpers;
const mockStore = reduxMockStore(storeWithoutUser);
const component = mount(
<Provider store={mockStore}>
<AuthenticatedRoutes>
<div>{renderedText}</div>
</AuthenticatedRoutes>
</Provider>,
);
expect(mockStore.getActions()).toContainEqual(redirectToLoginAction);
expect(component.html()).toBeFalsy();
});
it('does not redirect to login if the user is loading', () => {
const { reduxMockStore } = helpers;
const mockStore = reduxMockStore(storeLoadingUser);
const component = mount(
<Provider store={mockStore}>
<AuthenticatedRoutes>
<div>{renderedText}</div>
</AuthenticatedRoutes>
</Provider>,
);
expect(mockStore.getActions()).not.toContainEqual(redirectToLoginAction);
expect(component.html()).toBeFalsy();
});
});