Skip to content

Commit

Permalink
fixing test
Browse files Browse the repository at this point in the history
  • Loading branch information
LauraPinilla committed Mar 27, 2024
1 parent 3d2daec commit 257dbf6
Show file tree
Hide file tree
Showing 3 changed files with 207 additions and 4 deletions.
35 changes: 34 additions & 1 deletion blocks/identity-block/features/login/default.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe("Identity Login Feature", () => {
});
});

describe("Identity Login Feature - rejected Login", () => {
describe("Identity Login Feature - rejected Login, general message", () => {
beforeEach(() => {
mockLogin.mockImplementation(() => Promise.reject({ code: 0 }));
global.grecaptcha = {
Expand Down Expand Up @@ -101,6 +101,39 @@ describe("Identity Login Feature - rejected Login", () => {
});
});

describe("Identity Login Feature - rejected Login, error code 130001", () => {
beforeEach(() => {
mockLogin.mockImplementation(() => Promise.reject({ code: 130001}));
global.grecaptcha = {
reset: jest.fn()
}
});

afterEach(() => {
jest.clearAllMocks();
});

it("rejects the login", async () => {
render(<Login customFields={defaultCustomFields} />);

await waitFor(() => expect(screen.getByLabelText("identity-block.email-label")));
fireEvent.change(screen.getByLabelText("identity-block.email-label"), {
target: { value: "[email protected]" },
});

await waitFor(() => expect(screen.getByLabelText("identity-block.password")));
fireEvent.change(screen.getByLabelText("identity-block.password"), {
target: { value: "thisNotIsMyPassword" },
});

await waitFor(() => expect(screen.getByRole("button")));
fireEvent.click(screen.getByRole("button"));

await waitFor(() => expect(mockLogin).toHaveBeenCalled());
await screen.findByText("identity-block.login-form-error.captcha-token-invalid");
});
});

describe("Identity Login Feature - unInitialized", () => {
beforeEach(() => {
useIdentity.mockImplementation(() => ({
Expand Down
6 changes: 3 additions & 3 deletions blocks/identity-block/utils/useRecaptcha.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const useRecaptcha = (challengeIn) => {
const [isRecaptchaEnabled, setIsRecaptchaEnabled] = useState(false);

const setRecaptchaInfo = (isCaptchaEnabled, recaptchaSiteKey, recaptchaScore) => {
if (isCaptchaEnabled && recaptchaSiteKey && recaptchaScore) {
if (isCaptchaEnabled && recaptchaSiteKey && !!String(recaptchaScore)) {
if (recaptchaScore === "-1") {
setSiteKey(recaptchaSiteKey);
setRecaptchaVersion(RECAPTCHA_V2);
Expand All @@ -34,12 +34,12 @@ const useRecaptcha = (challengeIn) => {
const checkCaptcha = async () => {
const identityConfig = await Identity.getConfig();
const { recaptchaSiteKey, recaptchaScore } = identityConfig;
if (["signup", "signin", "magicLink"].includes(challengeIn)) {
if ([RECAPTCHA_LOGIN, RECAPTCHA_SIGNUP, RECAPTCHA_MAGICLINK].includes(challengeIn)) {
const isIdentityCaptchaEnabled = identityConfig?.[`${challengeIn}Recaptcha`];
setRecaptchaInfo(isIdentityCaptchaEnabled, recaptchaSiteKey, recaptchaScore);
}

if (challengeIn === "checkout") {
if (challengeIn === RECAPTCHA_CHECKOUT) {
const salesConfig = await Sales.getConfig();
const isSalesCaptchaEnabled = salesConfig?.checkoutRecaptchaEnabled;
setRecaptchaInfo(isSalesCaptchaEnabled, recaptchaSiteKey, recaptchaScore);
Expand Down
170 changes: 170 additions & 0 deletions blocks/identity-block/utils/useRecaptcha.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
import React from "react";
import { waitFor, renderHook } from "@testing-library/react";
import useRecaptcha from "./useRecaptcha";
import { useIdentity } from "@wpmedia/arc-themes-components";

// Mock setTimeout and clearTimeout
jest.useFakeTimers();

jest.mock("@arc-publishing/sdk-identity", () => ({
__esModule: true,
default: {
apiOrigin: "",
options: jest.fn(),
},
}));

jest.mock("fusion:properties", () =>
jest.fn(() => ({
api: {
identity: {
origin: "https://corecomponents-arc-demo-3-prod.api.cdn.arcpublishing.com",
},
retail: {
origin: "https://corecomponents-arc-demo-3-prod.api.cdn.arcpublishing.com",
endpoint: "/retail/public/v1/offer/live/",
},
},
})),
);

jest.mock("fusion:context", () => ({
__esModule: true,
useFusionContext: () => ({
arcSite: "Test Site",
}),
}));

const mockIdentity = {
getConfig: jest.fn(() =>
Promise.resolve({
signupRecaptcha: true,
signinRecaptcha: true,
magicLinkRecaptcha: true,
recaptchaScore: "-1",
recaptchaSiteKey: "6LemcOMhAAAAAEGptytQEAMK_SfH4ZAGJ_e4652C",
}),
),
};

const mockSales = {
getConfig: jest.fn(() =>
Promise.resolve({
checkoutRecaptchaEnabled: false,
}),
),
};

jest.mock("@wpmedia/arc-themes-components", () => ({
...jest.requireActual("@wpmedia/arc-themes-components"),
useIdentity: jest.fn(() => ({
isInitialized: true,
Identity: {
...mockIdentity,
},
})),
useSales: jest.fn(() => ({
isInitialized: true,
Sales: {
...mockSales,
},
})),
}));

describe("useRecaptcha", () => {
it("reCaptcha v2 enabled for signIn", async () => {
const { result } = renderHook(() => useRecaptcha("signin"));

expect(result.current.recaptchaVersion).toBe(undefined);
expect(result.current.siteKey).toBe(undefined);
expect(result.current.isRecaptchaEnabled).toBe(false);

await waitFor(() => expect(result.current.recaptchaVersion).toEqual("V2"));

await waitFor(() => expect(result.current.isRecaptchaEnabled).toEqual(true));

await waitFor(() =>
expect(result.current.siteKey).toEqual("6LemcOMhAAAAAEGptytQEAMK_SfH4ZAGJ_e4652C"),
);
});

it("reCaptcha v2 enabled for signUp", async () => {
const { result } = renderHook(() => useRecaptcha("signup"));

expect(result.current.recaptchaVersion).toBe(undefined);
expect(result.current.siteKey).toBe(undefined);
expect(result.current.isRecaptchaEnabled).toBe(false);

await waitFor(() => expect(result.current.recaptchaVersion).toEqual("V2"));

await waitFor(() => expect(result.current.isRecaptchaEnabled).toEqual(true));

await waitFor(() =>
expect(result.current.siteKey).toEqual("6LemcOMhAAAAAEGptytQEAMK_SfH4ZAGJ_e4652C"),
);
});

it("reCaptcha v2 enabled for magicLink", async () => {
const { result } = renderHook(() => useRecaptcha("magicLink"));

expect(result.current.recaptchaVersion).toBe(undefined);
expect(result.current.siteKey).toBe(undefined);
expect(result.current.isRecaptchaEnabled).toBe(false);

await waitFor(() => expect(result.current.recaptchaVersion).toEqual("V2"));

await waitFor(() => expect(result.current.isRecaptchaEnabled).toEqual(true));

await waitFor(() =>
expect(result.current.siteKey).toEqual("6LemcOMhAAAAAEGptytQEAMK_SfH4ZAGJ_e4652C"),
);
});

it("reCaptcha v2 enabled for magicLink", async () => {
const { result } = renderHook(() => useRecaptcha("magicLink"));

expect(result.current.recaptchaVersion).toBe(undefined);
expect(result.current.siteKey).toBe(undefined);
expect(result.current.isRecaptchaEnabled).toBe(false);

await waitFor(() => expect(result.current.recaptchaVersion).toEqual("V2"));

await waitFor(() => expect(result.current.isRecaptchaEnabled).toEqual(true));

await waitFor(() =>
expect(result.current.siteKey).toEqual("6LemcOMhAAAAAEGptytQEAMK_SfH4ZAGJ_e4652C"),
);
});

it("reCaptcha v2 disabled for signIn", async () => {

useIdentity.mockReturnValueOnce({
isInitialized: true,
Identity: {
getConfig: jest.fn(() =>
Promise.resolve({
signupRecaptcha: false, // Change the mocked data here
signinRecaptcha: false,
magicLinkRecaptcha: false,
recaptchaScore: "-1",
recaptchaSiteKey: "6LemcOMhAAAAAEGptytQEAMK_SfH4ZAGJ_e4652C",
}),
),
},
});

const { result } = renderHook(() => useRecaptcha("signin"));

expect(result.current.recaptchaVersion).toBe(undefined);
expect(result.current.siteKey).toBe(undefined);
expect(result.current.isRecaptchaEnabled).toBe(false);

await waitFor(() => expect(result.current.recaptchaVersion).toEqual(undefined));

await waitFor(() => expect(result.current.isRecaptchaEnabled).toEqual(false));

await waitFor(() =>
expect(result.current.siteKey).toEqual(undefined),
);
});
});

0 comments on commit 257dbf6

Please sign in to comment.