| 1 | import { deriveStatus, routeForStatus } from "@/context/authRouting";
|
| 2 | import type { User } from "@/lib/api";
|
| 3 |
|
| 4 | function user(overrides: Partial<User> = {}): User {
|
| 5 | return {
|
| 6 | id: 1,
|
| 7 | email: "ana@example.com",
|
| 8 | firstName: "Ana",
|
| 9 | updatedAt: new Date(2025, 0, 15),
|
| 10 | active: true,
|
| 11 | ...overrides,
|
| 12 | };
|
| 13 | }
|
| 14 |
|
| 15 | describe("deriveStatus", () => {
|
| 16 | it("stays loading while the session resolves, even with a user in hand", () => {
|
| 17 | expect(deriveStatus(true, null)).toBe("loading");
|
| 18 | expect(deriveStatus(true, user())).toBe("loading");
|
| 19 | });
|
| 20 |
|
| 21 | it("is signed out without a user", () => {
|
| 22 | expect(deriveStatus(false, null)).toBe("signedOut");
|
| 23 | });
|
| 24 |
|
| 25 | it("awaits activation for an account that is not active yet", () => {
|
| 26 | expect(deriveStatus(false, user({ active: false }))).toBe(
|
| 27 | "awaitingActivation",
|
| 28 | );
|
| 29 | });
|
| 30 |
|
| 31 | it("is active for an active account", () => {
|
| 32 | expect(deriveStatus(false, user({ active: true }))).toBe("active");
|
| 33 | });
|
| 34 | });
|
| 35 |
|
| 36 | describe("routeForStatus", () => {
|
| 37 | it("stays put while loading, whatever the user shape", () => {
|
| 38 | expect(routeForStatus("loading", null)).toBeNull();
|
| 39 | expect(routeForStatus("loading", user())).toBeNull();
|
| 40 | });
|
| 41 |
|
| 42 | it("sends a signed-out account to login", () => {
|
| 43 | expect(routeForStatus("signedOut", null)).toBe("/auth/login");
|
| 44 | });
|
| 45 |
|
| 46 | it("sends an unactivated account to activation", () => {
|
| 47 | expect(routeForStatus("awaitingActivation", user({ active: false }))).toBe(
|
| 48 | "/auth/activate",
|
| 49 | );
|
| 50 | });
|
| 51 |
|
| 52 | it("sends an active account that finished onboarding to the feed", () => {
|
| 53 | expect(
|
| 54 | routeForStatus("active", user({ onboardingSeenAt: new Date(2025, 0, 15) })),
|
| 55 | ).toBe("/(tabs)/new");
|
| 56 | });
|
| 57 |
|
| 58 | it("sends an active account that never finished onboarding to onboarding", () => {
|
| 59 | expect(routeForStatus("active", user({ onboardingSeenAt: null }))).toBe(
|
| 60 | "/onboarding",
|
| 61 | );
|
| 62 | expect(routeForStatus("active", user())).toBe("/onboarding");
|
| 63 | });
|
| 64 | });
|