Skip to content

Commit

Permalink
test: Add tests for different types of errors
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimir-cucu committed Jan 23, 2024
1 parent 4d2b0ce commit d151e15
Showing 1 changed file with 133 additions and 4 deletions.
137 changes: 133 additions & 4 deletions src/store/app/thunks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ import {
import { logOut, connectAndStartPolling } from "./thunks";

describe("thunks", () => {
const consoleError = console.error;
beforeEach(() => {
console.error = jest.fn();
});
afterEach(() => {
console.error = consoleError;
});

it("logOut", async () => {
const action = logOut();
const dispatch = jest.fn();
Expand Down Expand Up @@ -84,9 +92,69 @@ describe("thunks", () => {
);
});

it("connectAndStartPolling should catch error", async () => {
const consoleError = console.error;
console.error = jest.fn();
it("connectAndStartPolling should catch error instanceof Error", async () => {
const dispatch = jest
.fn()
// Successfuly dispatch connectAndStartPolling/pending.
.mockImplementationOnce(() => {})
// Throw error when trying to dispatch connectAndPollControllers.
.mockImplementationOnce(() => {
// eslint-disable-next-line no-throw-literal
throw "Error while dispatching connectAndPollControllers!";
});
const getState = jest.fn(() =>
rootStateFactory.build({
general: generalStateFactory.build({
config: configFactory.build({
isJuju: true,
}),
controllerConnections: {
"wss://example.com": {
user: {
"display-name": "eggman",
identity: "user-eggman@external",
"controller-access": "",
"model-access": "",
},
},
},
credentials: {
"wss://example.com": credentialFactory.build(),
},
}),
juju: jujuStateFactory.build({
controllers: {
"wss://example.com": [
controllerFactory.build({
path: "/",
uuid: "uuid123",
version: "1",
}),
],
},
}),
}),
);
const action = connectAndStartPolling();
await action(dispatch, getState, null);
expect(dispatch).toHaveBeenCalledWith(
appActions.connectAndPollControllers({
controllers: [["wss://controller.example.com", undefined, false]],
isJuju: true,
}),
);
expect(console.error).toHaveBeenCalledWith(
"Error while triggering the connection and polling of models.",
"Error while dispatching connectAndPollControllers!",
);
expect(dispatch).toHaveBeenCalledWith(
generalActions.storeConnectionError(
"Unable to connect: Error while dispatching connectAndPollControllers!",
),
);
});

it("connectAndStartPolling should catch string error", async () => {
const dispatch = jest
.fn()
// Successfuly dispatch connectAndStartPolling/pending.
Expand Down Expand Up @@ -145,6 +213,67 @@ describe("thunks", () => {
"Unable to connect: Error while dispatching connectAndPollControllers!",
),
);
console.error = consoleError;
});

it("connectAndStartPolling should catch non-standard type of error", async () => {
const dispatch = jest
.fn()
// Successfuly dispatch connectAndStartPolling/pending.
.mockImplementationOnce(() => {})
// Throw error when trying to dispatch connectAndPollControllers.
.mockImplementationOnce(() => {
// eslint-disable-next-line no-throw-literal
throw ["Unknown error."];
});
const getState = jest.fn(() =>
rootStateFactory.build({
general: generalStateFactory.build({
config: configFactory.build({
isJuju: true,
}),
controllerConnections: {
"wss://example.com": {
user: {
"display-name": "eggman",
identity: "user-eggman@external",
"controller-access": "",
"model-access": "",
},
},
},
credentials: {
"wss://example.com": credentialFactory.build(),
},
}),
juju: jujuStateFactory.build({
controllers: {
"wss://example.com": [
controllerFactory.build({
path: "/",
uuid: "uuid123",
version: "1",
}),
],
},
}),
}),
);
const action = connectAndStartPolling();
await action(dispatch, getState, null);
expect(dispatch).toHaveBeenCalledWith(
appActions.connectAndPollControllers({
controllers: [["wss://controller.example.com", undefined, false]],
isJuju: true,
}),
);
expect(console.error).toHaveBeenCalledWith(
"Error while triggering the connection and polling of models.",
["Unknown error."],
);
expect(dispatch).toHaveBeenCalledWith(
generalActions.storeConnectionError(
"Unable to connect: Something went wrong. View the console log for more details.",
),
);
});
});

0 comments on commit d151e15

Please sign in to comment.