|
| 1 | +/** |
| 2 | + * |
| 3 | + * Copyright © 2026 Ping Identity Corporation. All right reserved. |
| 4 | + * |
| 5 | + * This software may be modified and distributed under the terms |
| 6 | + * of the MIT license. See the LICENSE file for details. |
| 7 | + * |
| 8 | + **/ |
| 9 | + |
| 10 | +import { callbackType } from '@forgerock/journey-client'; |
| 11 | +import { expect, within } from 'storybook/test'; |
| 12 | + |
| 13 | +import { createJourneyStep } from '$journey/_utilities/step.mock'; |
| 14 | +import { journeyStore } from '$journey/journey.store'; |
| 15 | +import { |
| 16 | + invisibleGrecaptchaEnterprise, |
| 17 | + visibleGrecaptchaEnterprise, |
| 18 | +} from './recaptcha-enterprise.mock'; |
| 19 | +import RecaptchaEnterprise from './recaptcha-enterprise.story.svelte'; |
| 20 | + |
| 21 | +function mockGrecaptchaEnterprise() { |
| 22 | + window.grecaptcha = { |
| 23 | + enterprise: { |
| 24 | + ready: (cb) => cb(), |
| 25 | + render: () => 'widget-id', |
| 26 | + execute: async () => 'enterprise-token', |
| 27 | + reset: () => undefined, |
| 28 | + getResponse: () => 'enterprise-token', |
| 29 | + }, |
| 30 | + }; |
| 31 | +} |
| 32 | + |
| 33 | +function makeCallbackMetadata(mode) { |
| 34 | + return { |
| 35 | + derived: { |
| 36 | + canForceUserInputOptionality: false, |
| 37 | + isFirstInvalidInput: false, |
| 38 | + isReadyForSubmission: false, |
| 39 | + isSelfSubmitting: false, |
| 40 | + isUserInputRequired: false, |
| 41 | + isPasskeyAutofillEligible: false, |
| 42 | + }, |
| 43 | + idx: 0, |
| 44 | + initOptions: { mode }, |
| 45 | + }; |
| 46 | +} |
| 47 | + |
| 48 | +export default { |
| 49 | + argTypes: { |
| 50 | + callback: { control: false }, |
| 51 | + callbackMetadata: { control: false }, |
| 52 | + }, |
| 53 | + component: RecaptchaEnterprise, |
| 54 | + parameters: { |
| 55 | + layout: 'fullscreen', |
| 56 | + }, |
| 57 | + title: 'Callbacks/ReCaptchaEnterprise', |
| 58 | +}; |
| 59 | + |
| 60 | +export const VisibleEnterprise = { |
| 61 | + args: { |
| 62 | + callback: createJourneyStep(visibleGrecaptchaEnterprise).getCallbackOfType( |
| 63 | + callbackType.ReCaptchaEnterpriseCallback, |
| 64 | + ), |
| 65 | + callbackMetadata: makeCallbackMetadata('normal'), |
| 66 | + }, |
| 67 | + play: async () => { |
| 68 | + mockGrecaptchaEnterprise(); |
| 69 | + }, |
| 70 | +}; |
| 71 | + |
| 72 | +export const InvisibleEnterprise = { |
| 73 | + args: { |
| 74 | + callback: createJourneyStep(invisibleGrecaptchaEnterprise).getCallbackOfType( |
| 75 | + callbackType.ReCaptchaEnterpriseCallback, |
| 76 | + ), |
| 77 | + callbackMetadata: makeCallbackMetadata('invisible'), |
| 78 | + }, |
| 79 | + play: async () => { |
| 80 | + mockGrecaptchaEnterprise(); |
| 81 | + journeyStore.update((v) => ({ ...v, recaptchaAction: 'LOGIN' })); |
| 82 | + }, |
| 83 | +}; |
| 84 | + |
| 85 | +export const InvisibleEnterpriseError = { |
| 86 | + args: { ...InvisibleEnterprise.args }, |
| 87 | + play: async ({ canvasElement }) => { |
| 88 | + mockGrecaptchaEnterprise(); |
| 89 | + window.frHandleCaptchaInvisibleError?.(); |
| 90 | + const canvas = within(canvasElement); |
| 91 | + await expect(canvas.findByText(/CAPTCHA verification failed/i)).resolves.toBeInTheDocument(); |
| 92 | + }, |
| 93 | +}; |
0 commit comments