fleet/frontend/components/EmailTokenRedirect/EmailTokenRedirect.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

65 lines
1.8 KiB
JavaScript

import React from 'react';
import { mount } from 'enzyme';
import { connectedComponent, reduxMockStore } from 'test/helpers';
import ConnectedEmailTokenRedirect, { EmailTokenRedirect } from 'components/EmailTokenRedirect/EmailTokenRedirect';
import Kolide from 'kolide';
import { userStub } from 'test/stubs';
describe('EmailTokenRedirect - component', () => {
beforeEach(() => {
jest.spyOn(Kolide.users, 'confirmEmailChange')
.mockImplementation(() => Promise.resolve({ ...userStub, email: 'new@email.com' }));
});
const authStore = {
auth: {
user: userStub,
},
};
const token = 'KFBR392';
const defaultProps = {
params: {
token,
},
};
describe('componentWillMount', () => {
it('calls the API when a token and user are present', () => {
const mockStore = reduxMockStore(authStore);
mount(connectedComponent(ConnectedEmailTokenRedirect, {
mockStore,
props: defaultProps,
}));
expect(Kolide.users.confirmEmailChange).toHaveBeenCalledWith(userStub, token);
});
it('does not call the API when only a token is present', () => {
const mockStore = reduxMockStore({ auth: {} });
mount(connectedComponent(ConnectedEmailTokenRedirect, {
mockStore,
props: defaultProps,
}));
expect(Kolide.users.confirmEmailChange).not.toHaveBeenCalled();
});
});
describe('componentWillReceiveProps', () => {
it('calls the API when a user is received', () => {
const mockStore = reduxMockStore();
const props = { dispatch: mockStore.dispatch, token };
const Component = mount(<EmailTokenRedirect {...props} />);
expect(Kolide.users.confirmEmailChange).not.toHaveBeenCalled();
Component.setProps({ user: userStub });
expect(Kolide.users.confirmEmailChange).toHaveBeenCalledWith(userStub, token);
});
});
});