fleet/frontend/components/queries/QueryPageWrapper/helpers.tests.js
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

79 lines
2.4 KiB
JavaScript

import nock from 'nock';
import { find } from 'lodash';
import { reduxMockStore } from 'test/helpers';
import mocks from 'test/mocks';
import helpers from 'components/queries/QueryPageWrapper/helpers';
const { queries: queryMocks } = mocks;
describe('QueryPageWrapper - helpers', () => {
afterEach(() => { nock.cleanAll(); });
const queryID = '10';
describe('#fetchQuery', () => {
const { fetchQuery } = helpers;
const bearerToken = 'abc123';
beforeEach(() => {
global.localStorage.setItem('KOLIDE::auth_token', bearerToken);
});
describe('when the API call is successful', () => {
it('dispatches a load successful action', (done) => {
queryMocks.load.valid(bearerToken, queryID);
const mockStore = reduxMockStore();
fetchQuery(mockStore.dispatch, queryID)
.then(() => {
const dispatchedActions = mockStore.getActions().map((action) => { return action.type; });
expect(dispatchedActions).toContainEqual('queries_LOAD_SUCCESS');
done();
});
});
});
describe('when the API call is unsuccessful', () => {
it('pushes to the new query page', (done) => {
queryMocks.load.invalid(bearerToken, queryID);
const mockStore = reduxMockStore();
fetchQuery(mockStore.dispatch, queryID)
.then(() => {
const dispatchedActions = mockStore.getActions();
const locationChangeAction = find(dispatchedActions, { type: '@@router/CALL_HISTORY_METHOD' });
expect(locationChangeAction).toBeTruthy();
expect(locationChangeAction.payload).toEqual({
method: 'push',
args: ['/queries/new'],
});
done();
});
});
it('renders a flash error message', (done) => {
queryMocks.load.invalid(bearerToken, queryID);
const mockStore = reduxMockStore();
fetchQuery(mockStore.dispatch, queryID)
.then(() => {
const dispatchedActions = mockStore.getActions();
const flashMessageAction = find(dispatchedActions, { type: 'RENDER_FLASH' });
expect(flashMessageAction).toBeTruthy();
expect(flashMessageAction.payload).toMatchObject({
alertType: 'error',
message: 'Resource not found',
});
done();
})
.catch(done);
});
});
});
});