@joinmeow/cognito-passwordless-auth
Version:
Passwordless authentication with Amazon Cognito: FIDO2 (WebAuthn, support for Passkeys)
56 lines (55 loc) • 2.35 kB
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
import { renderHook, act } from "@testing-library/react";
import { usePasswordless, PasswordlessContextProvider, } from "../react/hooks.js";
import { signInWithRedirect } from "../hosted-oauth.js";
import { configure } from "../config.js";
import { retrieveTokens } from "../storage.js";
// Mock dependencies
jest.mock("../hosted-oauth");
jest.mock("../config");
jest.mock("../storage");
const mockSignInWithRedirect = signInWithRedirect;
const mockConfigure = configure;
const mockRetrieveTokens = retrieveTokens;
describe("React OAuth Integration - Simple", () => {
let mockConfig;
beforeEach(() => {
jest.clearAllMocks();
mockConfig = {
clientId: "test-client-id",
debug: jest.fn(),
storage: {
getItem: jest.fn(),
setItem: jest.fn(),
removeItem: jest.fn(),
},
};
mockConfigure.mockReturnValue(mockConfig);
mockSignInWithRedirect.mockResolvedValue(undefined);
mockRetrieveTokens.mockResolvedValue(undefined); // No tokens initially
});
const wrapper = ({ children }) => (_jsx(PasswordlessContextProvider, { children: children }));
describe("signInWithRedirect", () => {
it("should call hosted-oauth signInWithRedirect and update status", async () => {
const { result } = renderHook(() => usePasswordless(), { wrapper });
// Wait for initial load to complete
await act(async () => {
await new Promise((resolve) => setTimeout(resolve, 100));
});
// Initial state should not be signing in
expect(result.current.signInStatus).not.toBe("STARTING_SIGN_IN_WITH_REDIRECT");
// Call signInWithRedirect
act(() => {
result.current.signInWithRedirect({
provider: "Google",
});
});
// Should update status - the hook shows SIGNING_IN during the process
expect(["STARTING_SIGN_IN_WITH_REDIRECT", "SIGNING_IN"]).toContain(result.current.signInStatus);
// Should call the hosted-oauth function
expect(mockSignInWithRedirect).toHaveBeenCalledWith({
provider: "Google",
});
});
});
});