Skip to content

Commit

Permalink
wip: unit tests fix and code coverage improvements
Browse files Browse the repository at this point in the history
Refs: BLAIS5-4525
  • Loading branch information
SidraJaved committed Dec 30, 2024
1 parent 589bded commit a05e6e4
Showing 1 changed file with 71 additions and 69 deletions.
140 changes: 71 additions & 69 deletions src/api/http/users.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ import { addNewUser, deleteUser, editPassword, getAllUsers } from "./users";
import { NewUser, User } from "blaise-api-node-client";
import { requestPromiseJson, requestPromiseJsonList } from "./requestPromise";

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note

Unused import requestPromiseJsonList.

jest.mock("./requestPromise");

jest.mock("./requestPromise", () => {
const actualModule = jest.requireActual("./requestPromise");
return {
...actualModule,
requestPromiseJson: jest.fn()
};
});
const requestPromiseJsonMock = requestPromiseJson as jest.Mock<Promise<[number, JSON]>>;

const userList: User[] = [
Expand All @@ -20,56 +25,48 @@ const userList: User[] = [
describe("Function getAllUsers(filename: string) ", () => {

it("It should return true with data if the list is returned successfully", async () => {
(requestPromiseJsonList as jest.Mock).mockResolvedValue([true, userList]);

mock_server_request_Return_JSON(200, userList);
const [success, users] = await getAllUsers();
expect(success).toBeTruthy();
expect(users).toEqual(userList);
});

it("It should return true with an empty list if a 404 is returned from the server", async () => {
(requestPromiseJsonList as jest.Mock).mockResolvedValue([true, []]);
mock_server_request_Return_JSON(404, []);
const [success, users] = await getAllUsers();
expect(success).toBeTruthy();
expect(users).toEqual([]);
});

it("It should return false with an empty list if request returns an error code", async () => {
(requestPromiseJsonList as jest.Mock).mockResolvedValue([false, []]);
mock_server_request_Return_JSON(500, {});
const [success, users] = await getAllUsers();
expect(success).toBeFalsy();
expect(users).toEqual([]);
});

it("It should return false with an empty list if request JSON is not a list", async () => {
// mock_server_request_function(jest.fn(() => Promise.resolve({
// status: 400,
// json: () => Promise.reject("Failed")
// })));
// let promiseResponse: [number, JSON];
// promiseResponse = [400, JSON.parse(() => Promise.reject("Failed"))];
requestPromiseJsonMock.mockRejectedValue(new Error("Async error"));
mock_server_request_function(jest.fn(() => Promise.resolve({
status: 400,
json: () => Promise.reject("Failed")
})));

const [success, users] = await getAllUsers();
expect(success).toBeFalsy();
expect(users).toEqual([]);
});

it("It should return false with an empty list if request JSON is invalid", async () => {

const promiseResponse: [number, JSON] = [200, JSON.parse(JSON.stringify({ name: "NAME" }))];
requestPromiseJsonMock.mockResolvedValue(promiseResponse);
mock_server_request_Return_JSON(200, { name: "NAME" });
const [success, users] = await getAllUsers();
expect(success).toBeFalsy();
expect(users).toEqual([]);
});

it("It should return false with an empty list if request call fails", async () => {
// mock_server_request_function(jest.fn(() => {
// throw new Error("Network error");
// }));
requestPromiseJsonMock.mockImplementation(() => { throw new Error("Network error"); });

mock_server_request_function(jest.fn(() => {
throw new Error("Network error");
}));
const [success, users] = await getAllUsers();
expect(success).toBeFalsy();
expect(users).toEqual([]);
Expand All @@ -81,62 +78,67 @@ describe("Function getAllUsers(filename: string) ", () => {
});
});

// const newUser: NewUser = {
// name: "New User",
// password: "password",
// role: "DST",
// defaultServerPark: "gusty",
// serverParks: ["gusty"]
// };
const newUser: NewUser = {
name: "New User",
password: "password",
role: "DST",
defaultServerPark: "gusty",
serverParks: ["gusty"]
};

// describe("Function addNewUser(user: User) ", () => {
describe("Function addNewUser(user: User) ", () => {

// it("It should return true if the user has been created successfully", async () => {
// mock_server_request_Return_JSON(201, {});
// const success = await addNewUser(newUser);
// expect(success).toBeTruthy();
// });
let promiseResponse: [number, JSON];

// it("It should return false if a password is not provided", async () => {
// mock_server_request_Return_JSON(201, {});
// const newUser: NewUser = {
// defaultServerPark: "",
// name: "username",
// password: "",
// role: "",
// serverParks: []
// };

// const success = await addNewUser(newUser);
// expect(success).toBeFalsy();
// });
it("It should return true if the user has been created successfully", async () => {
promiseResponse = [201, JSON.parse("{}")];
requestPromiseJsonMock.mockResolvedValue(promiseResponse);

// it("It should return false if a 404 is returned from the server", async () => {
// mock_server_request_Return_JSON(404, []);
// const success = await addNewUser(newUser);
// expect(success).toBeFalsy();
// });
const success = await addNewUser(newUser);
expect(success).toBeTruthy();
});

// it("It should return false if request returns an error code", async () => {
// mock_server_request_Return_JSON(500, {});
// const success = await addNewUser(newUser);
// expect(success).toBeFalsy();
// });
it("It should return false if a password is not provided", async () => {
promiseResponse = [201, JSON.parse("{}")];
requestPromiseJsonMock.mockResolvedValue(promiseResponse);

// it("It should return false if request call fails", async () => {
// mock_server_request_function(jest.fn(() => {
// throw new Error("Network error");
// }));
const newUser: NewUser = {
defaultServerPark: "",
name: "username",
password: "",
role: "",
serverParks: []
};

// const success = await addNewUser(newUser);
// expect(success).toBeFalsy();
// });
const success = await addNewUser(newUser);
expect(success).toBeFalsy();
});

// afterAll(() => {
// jest.clearAllMocks();
// cleanup();
// });
// });
it("It should return false if a 404 is returned from the server", async () => {
promiseResponse = [404, JSON.parse("[]")];
requestPromiseJsonMock.mockResolvedValue(promiseResponse);
const success = await addNewUser(newUser);
expect(success).toBeFalsy();
});

it("It should return false if request returns an error code", async () => {
promiseResponse = [500, JSON.parse("{}")];
requestPromiseJsonMock.mockResolvedValue(promiseResponse);
const success = await addNewUser(newUser);
expect(success).toBeFalsy();
});

it("It should return false if request call fails", async () => {
requestPromiseJsonMock.mockRejectedValue(new Error("Async error"));
const success = await addNewUser(newUser);
expect(success).toBeFalsy();
});

afterAll(() => {
jest.clearAllMocks();
cleanup();
});
});

// describe("Function deleteUser(username: string) ", () => {

Expand Down

0 comments on commit a05e6e4

Please sign in to comment.