|
| 1 | +/** |
| 2 | + * @file This file provides a comprehensive mock for the Auth0 ACUL JS Login screen. |
| 3 | + * It is designed to be structurally aligned with the official SDK, enabling robust |
| 4 | + * and isolated testing of our components. |
| 5 | + * |
| 6 | + * It mocks the following core parts of the SDK: |
| 7 | + * - `Login` class: {@link https://auth0.github.io/universal-login/classes/Classes.Login.html} |
| 8 | + * - `ScreenMembersOnLogin`: {@link https://auth0.github.io/universal-login/interfaces/Classes.ScreenMembersOnLogin.html} |
| 9 | + * - `TransactionMembersOnLogin`: {@link https://auth0.github.io/universal-login/interfaces/Classes.TransactionMembersOnLogin.html} |
| 10 | + */ |
| 11 | +import type { |
| 12 | + ScreenMembersOnLogin, |
| 13 | + TransactionMembersOnLogin, |
| 14 | +} from "@auth0/auth0-acul-js"; |
| 15 | + |
| 16 | +/** |
| 17 | + * Defines the "contract" for our mock. It combines the methods from the main |
| 18 | + * `Login` class with the `screen` and `transaction` data structures. |
| 19 | + * This provides a single, type-safe object to control in our tests. |
| 20 | + */ |
| 21 | +export interface MockLoginInstance { |
| 22 | + login: jest.Mock; |
| 23 | + federatedLogin: jest.Mock; |
| 24 | + getError: jest.Mock; |
| 25 | + screen: ScreenMembersOnLogin; |
| 26 | + transaction: TransactionMembersOnLogin; |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Factory function to create a new mock instance of the `Login` class. |
| 31 | + * This ensures each test gets a clean, isolated mock object that is |
| 32 | + * structurally aligned with the official SDK documentation. |
| 33 | + */ |
| 34 | +export const createMockLoginInstance = (): MockLoginInstance => ({ |
| 35 | + login: jest.fn(), |
| 36 | + federatedLogin: jest.fn(), |
| 37 | + getError: jest.fn(() => []), // Returns empty array by default |
| 38 | + screen: { |
| 39 | + name: "login", |
| 40 | + texts: { |
| 41 | + title: "Mock Welcome Title", |
| 42 | + description: "Mock description text.", |
| 43 | + usernamePlaceholder: "Username", |
| 44 | + emailPlaceholder: "Email Address", |
| 45 | + usernameOrEmailPlaceholder: "Username or Email Address", |
| 46 | + passwordPlaceholder: "Password", |
| 47 | + buttonText: "Mock Continue", |
| 48 | + forgotPasswordText: "Can't log in?", |
| 49 | + separatorText: "Or", |
| 50 | + signupActionLinkText: "Sign up", |
| 51 | + captchaCodePlaceholder: "Enter the code shown above", |
| 52 | + }, |
| 53 | + // Structurally correct captcha properties |
| 54 | + isCaptchaAvailable: false, |
| 55 | + captchaProvider: null, |
| 56 | + captchaSiteKey: null, |
| 57 | + captchaImage: null, |
| 58 | + captcha: null, |
| 59 | + // Use direct link properties |
| 60 | + signupLink: "/signup", |
| 61 | + resetPasswordLink: "/reset", |
| 62 | + // Base properties that must exist |
| 63 | + links: {}, |
| 64 | + data: {}, |
| 65 | + }, |
| 66 | + transaction: { |
| 67 | + // Declarative boolean flags for UI logic |
| 68 | + isSignupEnabled: true, |
| 69 | + isForgotPasswordEnabled: true, |
| 70 | + isPasskeyEnabled: false, |
| 71 | + hasErrors: false, |
| 72 | + // Default transaction state |
| 73 | + errors: [], |
| 74 | + alternateConnections: [ |
| 75 | + { |
| 76 | + name: "google-oauth2", |
| 77 | + strategy: "google", |
| 78 | + options: { displayName: "Google", showAsButton: true }, |
| 79 | + }, |
| 80 | + { |
| 81 | + name: "github", |
| 82 | + strategy: "github", |
| 83 | + options: { displayName: "Github", showAsButton: true }, |
| 84 | + }, |
| 85 | + ], |
| 86 | + locale: "en", |
| 87 | + state: "g-state", |
| 88 | + currentConnection: null, |
| 89 | + connectionStrategy: null, |
| 90 | + passwordPolicy: { |
| 91 | + minLength: 8, |
| 92 | + policy: "good", |
| 93 | + }, |
| 94 | + allowedIdentifiers: ["email", "username"], |
| 95 | + countryCode: null, |
| 96 | + countryPrefix: null, |
| 97 | + }, |
| 98 | +}); |
| 99 | + |
| 100 | +// Default mock implementation |
| 101 | +const createDefaultMock = () => createMockLoginInstance(); |
| 102 | + |
| 103 | +export default jest.fn().mockImplementation(createDefaultMock); |
0 commit comments