2021-04-12 13:32:25 +00:00
|
|
|
import nock from "nock";
|
|
|
|
|
import { find } from "lodash";
|
2016-11-07 16:42:39 +00:00
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
import { reduxMockStore } from "test/helpers";
|
|
|
|
|
import mocks from "test/mocks";
|
|
|
|
|
import helpers from "components/queries/QueryPageWrapper/helpers";
|
2016-11-07 16:42:39 +00:00
|
|
|
|
2017-03-02 22:07:01 +00:00
|
|
|
const { queries: queryMocks } = mocks;
|
|
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
describe("QueryPageWrapper - helpers", () => {
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
nock.cleanAll();
|
|
|
|
|
});
|
2016-11-07 16:42:39 +00:00
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
const queryID = "10";
|
2016-11-07 16:42:39 +00:00
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
describe("#fetchQuery", () => {
|
2016-11-07 16:42:39 +00:00
|
|
|
const { fetchQuery } = helpers;
|
2021-04-12 13:32:25 +00:00
|
|
|
const bearerToken = "abc123";
|
2016-11-07 16:42:39 +00:00
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2021-06-07 01:28:37 +00:00
|
|
|
global.localStorage.setItem("FLEET::auth_token", bearerToken);
|
2016-11-07 16:42:39 +00:00
|
|
|
});
|
|
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
describe("when the API call is successful", () => {
|
|
|
|
|
it("dispatches a load successful action", (done) => {
|
2017-03-02 22:07:01 +00:00
|
|
|
queryMocks.load.valid(bearerToken, queryID);
|
2016-11-07 16:42:39 +00:00
|
|
|
const mockStore = reduxMockStore();
|
|
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
fetchQuery(mockStore.dispatch, queryID).then(() => {
|
|
|
|
|
const dispatchedActions = mockStore.getActions().map((action) => {
|
|
|
|
|
return action.type;
|
2016-11-07 16:42:39 +00:00
|
|
|
});
|
2021-04-12 13:32:25 +00:00
|
|
|
expect(dispatchedActions).toContainEqual("queries_LOAD_SUCCESS");
|
|
|
|
|
|
|
|
|
|
done();
|
|
|
|
|
});
|
2016-11-07 16:42:39 +00:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
describe("when the API call is unsuccessful", () => {
|
2021-06-22 23:44:40 +00:00
|
|
|
it("pushes to the manage queries page", (done) => {
|
2017-03-02 22:07:01 +00:00
|
|
|
queryMocks.load.invalid(bearerToken, queryID);
|
2016-11-07 16:42:39 +00:00
|
|
|
const mockStore = reduxMockStore();
|
|
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
fetchQuery(mockStore.dispatch, queryID).then(() => {
|
|
|
|
|
const dispatchedActions = mockStore.getActions();
|
|
|
|
|
const locationChangeAction = find(dispatchedActions, {
|
|
|
|
|
type: "@@router/CALL_HISTORY_METHOD",
|
2016-11-07 16:42:39 +00:00
|
|
|
});
|
2021-04-12 13:32:25 +00:00
|
|
|
expect(locationChangeAction).toBeTruthy();
|
|
|
|
|
expect(locationChangeAction.payload).toEqual({
|
|
|
|
|
method: "push",
|
2021-06-22 23:44:40 +00:00
|
|
|
args: ["/queries/manage"],
|
2021-04-12 13:32:25 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
done();
|
|
|
|
|
});
|
2016-11-07 16:42:39 +00:00
|
|
|
});
|
|
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
it("renders a flash error message", (done) => {
|
2017-03-02 22:07:01 +00:00
|
|
|
queryMocks.load.invalid(bearerToken, queryID);
|
2016-11-07 16:42:39 +00:00
|
|
|
const mockStore = reduxMockStore();
|
|
|
|
|
|
|
|
|
|
fetchQuery(mockStore.dispatch, queryID)
|
|
|
|
|
.then(() => {
|
|
|
|
|
const dispatchedActions = mockStore.getActions();
|
2021-04-12 13:32:25 +00:00
|
|
|
const flashMessageAction = find(dispatchedActions, {
|
|
|
|
|
type: "RENDER_FLASH",
|
|
|
|
|
});
|
2016-11-07 16:42:39 +00:00
|
|
|
|
2020-12-01 18:15:12 +00:00
|
|
|
expect(flashMessageAction).toBeTruthy();
|
2021-06-22 23:44:40 +00:00
|
|
|
|
|
|
|
|
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",
|
|
|
|
|
});
|
|
|
|
|
}
|
2016-11-07 16:42:39 +00:00
|
|
|
|
|
|
|
|
done();
|
|
|
|
|
})
|
|
|
|
|
.catch(done);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|