Skip to content
Snippets Groups Projects
Commit 091ffcbc authored by Titus Netland's avatar Titus Netland
Browse files

Fixed mock tests of login API to return an Object and not a string

parent db170cd9
No related branches found
No related tags found
1 merge request!12API login mock tests
Pipeline #175984 passed
......@@ -84,7 +84,7 @@
>
Logg inn
</button>
<div class="flex justify-center align-items: flex-end; mb-6 mt-6">
<div class="align-items: flex-end; mb-6 mt-6">
<div class="ml-3 text-sm">
<router-link to="register" class="text-blue-600"
>Ny bruker</router-link
......
......@@ -4,13 +4,14 @@ import axios from "axios";
jest.mock("axios");
describe("testing mocking of apiutil.js", () => {
it("check that login fails with wrong credentials - against mock", async () => {
const loginRequest = {
email: "wrong@email.com",
password: "thisiswrong123"};
const expectedLoginResponse = "Login failed";
const expectedLoginResponse = { isLoggedIn: false, token: "" }
axios.post.mockImplementation(() =>
Promise.resolve({ data: expectedLoginResponse })
......@@ -18,25 +19,24 @@ describe("testing mocking of apiutil.js", () => {
const loginResponse = await doLogin(loginRequest);
expect(loginResponse).toEqual(expectedLoginResponse);
expect(loginResponse.token.isLoggedIn).toEqual(expectedLoginResponse.isLoggedIn);
});
it("check that login succeeds when correct credentials - against mock", async () => {
const loginRequest = {
email: "correct@email.com",
password: "thisiscorrect123"};
const apiResponse = {
response:
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
};
const apiResponse = {isLoggedIn: true, token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM" +
"0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"};
const expectedLoginResponse = "Login failed";
const expectedLoginResponse = {isLoggedIn: false, token: ""};
axios.post.mockImplementation(() => Promise.resolve({ data: apiResponse }));
const loginResponse = await doLogin(loginRequest);
expect(loginResponse).not.toEqual(expectedLoginResponse);
expect(loginResponse.token.isLoggedIn).not.toEqual(expectedLoginResponse.isLoggedIn);
});
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment