diff --git a/src/api/http/users.test.ts b/src/api/http/users.test.ts index 32dea45..3f78bd8 100644 --- a/src/api/http/users.test.ts +++ b/src/api/http/users.test.ts @@ -8,8 +8,13 @@ import { addNewUser, deleteUser, editPassword, getAllUsers } from "./users"; import { NewUser, User } from "blaise-api-node-client"; import { requestPromiseJson, requestPromiseJsonList } from "./requestPromise"; -jest.mock("./requestPromise"); - +jest.mock("./requestPromise", () => { + const actualModule = jest.requireActual("./requestPromise"); + return { + ...actualModule, + requestPromiseJson: jest.fn() + }; +}); const requestPromiseJsonMock = requestPromiseJson as jest.Mock>; const userList: User[] = [ @@ -20,35 +25,31 @@ 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(); @@ -56,20 +57,16 @@ describe("Function getAllUsers(filename: string) ", () => { }); 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([]); @@ -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) ", () => {