-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3d2daec
commit 257dbf6
Showing
3 changed files
with
207 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 = { | ||
|
@@ -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(() => ({ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
); | ||
}); | ||
}); |