fleet/frontend/components/queries/QueryPageWrapper/helpers.tests.js
2021-06-06 18:28:37 -07:00

84 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("FLEET::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);
});
});
});
});