xtablo-source/xtablo-expo/stores/__tests__/auth.test.ts
2026-05-03 09:28:46 +02:00

124 lines
2.8 KiB
TypeScript

jest.mock("@/lib/supabase", () => ({
supabase: {
auth: {
getSession: jest.fn(),
onAuthStateChange: jest.fn(),
setSession: jest.fn(),
signInWithIdToken: jest.fn(),
signInWithOAuth: jest.fn(),
signInWithPassword: jest.fn(),
signOut: jest.fn(),
signUp: jest.fn(),
},
},
}));
jest.mock("@/lib/api", () => ({
api: {
get: jest.fn(),
},
}));
jest.mock("@/lib/purchases", () => ({
ensurePurchasesConfigured: jest.fn(),
}));
jest.mock("expo-web-browser", () => ({
maybeCompleteAuthSession: jest.fn(),
openAuthSessionAsync: jest.fn(),
}));
jest.mock("expo-auth-session", () => ({
makeRedirectUri: jest.fn(() => "xtablo://redirect"),
}));
jest.mock("expo-auth-session/build/QueryParams", () => ({
getQueryParams: jest.fn(() => ({ params: {}, errorCode: null })),
}));
jest.mock("react-native", () => ({
Linking: {
addEventListener: jest.fn(),
getInitialURL: jest.fn(),
},
}));
jest.mock("expo-apple-authentication", () => ({
AppleAuthenticationScope: {
EMAIL: "EMAIL",
FULL_NAME: "FULL_NAME",
},
signInAsync: jest.fn(),
}));
import { useAuthStore } from "@/stores/auth";
import { supabase } from "@/lib/supabase";
const mockedSupabase = supabase as jest.Mocked<typeof supabase>;
const mockedSignUp = mockedSupabase.auth.signUp as jest.Mock;
describe("auth store signUp", () => {
beforeEach(() => {
jest.clearAllMocks();
useAuthStore.setState({
initialized: false,
loading: false,
session: null,
user: null,
});
});
it("sends snake_case signup metadata and a full name", async () => {
mockedSignUp.mockResolvedValue({
data: {
session: {
user: { id: "user-id" },
},
},
error: null,
} as never);
await useAuthStore.getState().signUp(
"owner@example.com",
"password123",
"Ada",
"Lovelace",
"Analytical Engines"
);
expect(mockedSupabase.auth.signUp).toHaveBeenCalledWith({
email: "owner@example.com",
password: "password123",
options: {
data: {
company_name: "Analytical Engines",
first_name: "Ada",
last_name: "Lovelace",
name: "Ada Lovelace",
},
},
});
});
it("throws a helpful error when signup does not create a session", async () => {
mockedSignUp.mockResolvedValue({
data: {
session: null,
user: { id: "user-id" },
},
error: null,
} as never);
await expect(
useAuthStore.getState().signUp(
"owner@example.com",
"password123",
"Ada",
"Lovelace",
"Analytical Engines"
)
).rejects.toThrow(
"Impossible d'ouvrir votre session après l'inscription. Si cette adresse existe déjà, essayez de vous connecter."
);
});
});