fleet/frontend/components/queries/QueryPageWrapper/helpers.tests.js
Martavis Parker 42272f358f
#1108 send users to manage queries if error (#1154)
* #1108 send users to manage queries if error

* #1108 fixed tests and lint

* #1108 fixed more tests

* #1108 new clause to show different error message

* #1108 fixed logic
2021-06-22 16:44:40 -07:00

96 lines
2.8 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 manage queries 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/manage"],
});
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();
if (
flashMessageAction.payload.message.includes(
"no rows in result set"
)
) {
expect(flashMessageAction.payload).toMatchObject({
alertType: "error",
message: "The query you requested does not exist in Fleet.",
});
} else {
expect(flashMessageAction.payload).toMatchObject({
alertType: "error",
message: "Resource not found",
});
}
done();
})
.catch(done);
});
});
});
});