Skip to content

Commit

Permalink
test: unit test updates for screens
Browse files Browse the repository at this point in the history
  • Loading branch information
amberkamboj77 committed Dec 9, 2024
1 parent 9309bd4 commit 77de377
Show file tree
Hide file tree
Showing 5 changed files with 360 additions and 74 deletions.
56 changes: 27 additions & 29 deletions packages/auth0-acul-js/tests/data/test-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,50 +23,48 @@ export const baseContextData = {
},
transaction: {
country_code: {
code: "US",
prefix: "1",
code: "IN",
prefix: "91",
},
alternate_connections: [
{
name: "facebook",
strategy: "facebook",
metadata: {
client_id: "123456",
client_secret: "abcdef",
},
name: "google-oauth2",
strategy: "google",
},
{
name: "linkedin",
strategy: "linkedin",
metadata: {
client_id: "123456",
client_secret: "abcdef",
},
},
{
name: "microsoft",
strategy: "microsoft",
metadata: {
client_id: "123456",
client_secret: "abcdef",
},
name: "github",
strategy: "github",
},
{
name: "apple",
strategy: "apple",
metadata: {
client_id: "123456",
client_secret: "abcdef",
},
name: "twitter",
strategy: "twitter",
},
],
connection: {
name: "Username-Password-Authentication",
strategy: "auth0",
options: {
signup_enabled: true,
username_required: false,
forgot_password_enabled: true,
attributes: {
email: {
signup_status: "required",
},
phone: {
signup_status: "required",
},
username: {
signup_status: "required",
validation: {
max_length: 15,
min_length: 1,
allowed_types: {
email: false,
phone_number: false,
},
},
},
},
authentication_methods: {
password: {
enabled: true,
Expand Down
93 changes: 67 additions & 26 deletions packages/auth0-acul-js/tests/unit/screens/login-id.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import LoginId from "../../../src/screens/login-id";
import { baseContextData } from "../../data/test-data";
import { FormHandler } from "../../../src/utils/form-handler";
import { getPasskeyCredentials } from "../../../src/utils/passkeys";
import { LoginOptions, SocialLoginOptions } from "interfaces/screens/login-id";
import { CustomOptions } from "interfaces/common";
import { Errors } from "../../../src/utils/errors";

jest.mock("../../../src/utils/form-handler");
jest.mock("../../../src/utils/passkeys");
Expand Down Expand Up @@ -38,31 +38,6 @@ describe("LoginId", () => {
);
});

it("should handle login with passkey correctly", async () => {
const mockPasskeyCredentials = {
id: "testId",
rawId: "testRawId",
response: {},
type: "public-key",
};
(getPasskeyCredentials as jest.Mock).mockResolvedValueOnce(
mockPasskeyCredentials
);
const payload: CustomOptions = {
username: "testUser",
password: "testPassword",
};
await loginId.passkeyLogin(payload);

expect(mockFormHandler.submitData).toHaveBeenCalledTimes(1);
expect(mockFormHandler.submitData).toHaveBeenCalledWith(
expect.objectContaining({
...payload,
passkey: JSON.stringify(mockPasskeyCredentials),
})
);
});

it("should throw error when promise is rejected", async () => {
mockFormHandler.submitData.mockRejectedValue(new Error("Mocked reject"));
const payload: LoginOptions = {
Expand Down Expand Up @@ -117,4 +92,70 @@ describe("LoginId", () => {
);
});
});

describe("Passkey Login method", () => {
it("should handle login with passkey correctly", async () => {
const payload: CustomOptions = {
username: "testUser",
password: "testPassword",
};
await loginId.passkeyLogin(payload);

expect(mockFormHandler.submitData).toHaveBeenCalledTimes(1);
expect(mockFormHandler.submitData).toHaveBeenCalledWith(
expect.objectContaining(payload)
);
});

it("should handle errors thrown by getPasskeyCredentials", async () => {
mockFormHandler.submitData.mockRejectedValue(
new Error(Errors.PASSKEY_DATA_UNAVAILABLE)
);
await expect(loginId.passkeyLogin()).rejects.toThrow(
Errors.PASSKEY_DATA_UNAVAILABLE
);
});
});

describe("Pick Country Code method", () => {
it("should submit pick country code action without additional payload", async () => {
await loginId.pickCountryCode();

expect(mockFormHandler.submitData).toHaveBeenCalledWith(
expect.objectContaining({
action: "pick-country-code",
})
);
});

it("should merge additional payload with pick country code action", async () => {
const additionalPayload: CustomOptions = {
countryCode: "+1",
region: "US",
};

await loginId.pickCountryCode(additionalPayload);

expect(mockFormHandler.submitData).toHaveBeenCalledWith(
expect.objectContaining({
...additionalPayload,
action: "pick-country-code",
})
);
});

it("should handle form submission errors for pick country code", async () => {
mockFormHandler.submitData.mockRejectedValue(
new Error("Country code selection failed")
);

const additionalPayload: CustomOptions = {
countryCode: "+1",
};

await expect(loginId.pickCountryCode(additionalPayload)).rejects.toThrow(
"Country code selection failed"
);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import LoginPasswordlessEmailCode from "../../../src/screens/login-passwordless-email-code";
import { baseContextData } from "../../data/test-data";
import { FormHandler } from "../../../src/utils/form-handler";
import { SubmitCodeOptions } from "interfaces/screens/login-passwordless-email-code";
import { CustomOptions } from "interfaces/common";

jest.mock("../../../src/utils/form-handler");

describe("LoginPasswordlessEmailCode", () => {
let loginPasswordlessEmailCode: LoginPasswordlessEmailCode;
let mockFormHandler: { submitData: jest.Mock };

beforeEach(() => {
global.window = Object.create(window);
window.universal_login_context = baseContextData;

loginPasswordlessEmailCode = new LoginPasswordlessEmailCode();

mockFormHandler = {
submitData: jest.fn(),
};
(FormHandler as jest.Mock).mockImplementation(() => mockFormHandler);
});

describe("submitCode method", () => {
it("should handle submitCode with valid payload correctly", async () => {
const payload: SubmitCodeOptions = {
email: "[email protected]",
code: "123456",
};
await loginPasswordlessEmailCode.submitCode(payload);

expect(mockFormHandler.submitData).toHaveBeenCalledTimes(1);
expect(mockFormHandler.submitData).toHaveBeenCalledWith(
expect.objectContaining(payload)
);
});

it("should throw error when promise is rejected", async () => {
mockFormHandler.submitData.mockRejectedValue(new Error("Mocked reject"));
const payload: SubmitCodeOptions = {
email: "[email protected]",
code: "123456",
};
await expect(
loginPasswordlessEmailCode.submitCode(payload)
).rejects.toThrow("Mocked reject");
});

it("should throw error when email is empty", async () => {
mockFormHandler.submitData.mockRejectedValueOnce(
new Error("Invalid email")
);
const payload = { email: "", code: "123456" };

await expect(
loginPasswordlessEmailCode.submitCode(payload)
).rejects.toThrow("Invalid email");
});

it("should throw error when code is empty", async () => {
mockFormHandler.submitData.mockRejectedValueOnce(
new Error("Invalid code")
);
const payload = { email: "[email protected]", code: "" };

await expect(
loginPasswordlessEmailCode.submitCode(payload)
).rejects.toThrow("Invalid code");
});
});

describe("resendCode method", () => {
it("should handle resendCode with valid payload correctly", async () => {
const payload: CustomOptions = {
email: "[email protected]",
};
await loginPasswordlessEmailCode.resendCode(payload);

expect(mockFormHandler.submitData).toHaveBeenCalledTimes(1);
expect(mockFormHandler.submitData).toHaveBeenCalledWith(
expect.objectContaining({
...payload,
action: "resend",
})
);
});

it("should handle resendCode without payload correctly", async () => {
await loginPasswordlessEmailCode.resendCode();

expect(mockFormHandler.submitData).toHaveBeenCalledTimes(1);
expect(mockFormHandler.submitData).toHaveBeenCalledWith(
expect.objectContaining({
action: "resend",
})
);
});

it("should throw error when promise is rejected", async () => {
mockFormHandler.submitData.mockRejectedValue(new Error("Mocked reject"));
const payload: CustomOptions = {
email: "[email protected]",
};
await expect(
loginPasswordlessEmailCode.resendCode(payload)
).rejects.toThrow("Mocked reject");
});
});
});
Loading

0 comments on commit 77de377

Please sign in to comment.