|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +const { |
| 4 | + fetchMock, |
| 5 | + getAuthHeadersMock, |
| 6 | + handleApiErrorMock, |
| 7 | + handleApiResponseMock, |
| 8 | +} = vi.hoisted(() => ({ |
| 9 | + fetchMock: vi.fn(), |
| 10 | + getAuthHeadersMock: vi.fn(), |
| 11 | + handleApiErrorMock: vi.fn(), |
| 12 | + handleApiResponseMock: vi.fn(), |
| 13 | +})); |
| 14 | + |
| 15 | +vi.mock("next/cache", () => ({ revalidatePath: vi.fn() })); |
| 16 | +vi.mock("next/navigation", () => ({ redirect: vi.fn() })); |
| 17 | + |
| 18 | +vi.mock("@/lib", () => ({ |
| 19 | + apiBaseUrl: "https://api.example.com/api/v1", |
| 20 | + getAuthHeaders: getAuthHeadersMock, |
| 21 | +})); |
| 22 | + |
| 23 | +vi.mock("@/lib/server-actions-helper", () => ({ |
| 24 | + handleApiError: handleApiErrorMock, |
| 25 | + handleApiResponse: handleApiResponseMock, |
| 26 | +})); |
| 27 | + |
| 28 | +import { |
| 29 | + isOnboardingProfileRecorded, |
| 30 | + skipOnboardingProfile, |
| 31 | + submitOnboardingProfile, |
| 32 | +} from "./profile"; |
| 33 | + |
| 34 | +const ANSWERS = { |
| 35 | + declared_cloud_accounts: "11-50", |
| 36 | + declared_team_size: "2-5", |
| 37 | + declared_role: "security", |
| 38 | +} as const; |
| 39 | + |
| 40 | +describe("onboarding profile actions", () => { |
| 41 | + beforeEach(() => { |
| 42 | + vi.stubGlobal("fetch", fetchMock); |
| 43 | + fetchMock.mockReset(); |
| 44 | + getAuthHeadersMock.mockReset().mockResolvedValue({}); |
| 45 | + handleApiErrorMock.mockReset(); |
| 46 | + handleApiResponseMock.mockReset().mockResolvedValue({ data: { id: "p1" } }); |
| 47 | + }); |
| 48 | + |
| 49 | + it("reports a stored profile and sends the declared buckets", async () => { |
| 50 | + // Given |
| 51 | + fetchMock.mockResolvedValue(new Response("{}", { status: 201 })); |
| 52 | + |
| 53 | + // When |
| 54 | + const result = await submitOnboardingProfile(ANSWERS); |
| 55 | + |
| 56 | + // Then |
| 57 | + expect(result).toEqual({ stored: true }); |
| 58 | + const [url, init] = fetchMock.mock.calls[0]; |
| 59 | + expect(url).toBe("https://api.example.com/api/v1/onboarding-profiles"); |
| 60 | + expect(JSON.parse(init.body).data).toEqual({ |
| 61 | + type: "onboarding-profiles", |
| 62 | + attributes: ANSWERS, |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + it("records a skip as its own payload", async () => { |
| 67 | + // Given |
| 68 | + fetchMock.mockResolvedValue(new Response("{}", { status: 201 })); |
| 69 | + |
| 70 | + // When |
| 71 | + const result = await skipOnboardingProfile(); |
| 72 | + |
| 73 | + // Then |
| 74 | + expect(result).toEqual({ stored: true }); |
| 75 | + expect(JSON.parse(fetchMock.mock.calls[0][1].body).data.attributes).toEqual( |
| 76 | + { |
| 77 | + skipped: true, |
| 78 | + }, |
| 79 | + ); |
| 80 | + }); |
| 81 | + |
| 82 | + it("treats a transport failure as not stored", async () => { |
| 83 | + // Given — the request never reaches the API. |
| 84 | + fetchMock.mockRejectedValue(new Error("network down")); |
| 85 | + |
| 86 | + // When |
| 87 | + const result = await submitOnboardingProfile(ANSWERS); |
| 88 | + |
| 89 | + // Then |
| 90 | + expect(result.stored).toBe(false); |
| 91 | + expect(result.error).toBeTruthy(); |
| 92 | + expect(handleApiErrorMock).toHaveBeenCalledTimes(1); |
| 93 | + }); |
| 94 | + |
| 95 | + it("treats a rejection without an errors array as not stored", async () => { |
| 96 | + // Given — e.g. a 403, which `handleApiResponse` returns as a bare error. |
| 97 | + fetchMock.mockResolvedValue(new Response("{}", { status: 403 })); |
| 98 | + handleApiResponseMock.mockResolvedValue({ |
| 99 | + error: "Forbidden", |
| 100 | + status: 403, |
| 101 | + }); |
| 102 | + |
| 103 | + // When |
| 104 | + const result = await submitOnboardingProfile(ANSWERS); |
| 105 | + |
| 106 | + // Then |
| 107 | + expect(result).toEqual({ stored: false, error: "Forbidden" }); |
| 108 | + }); |
| 109 | + |
| 110 | + it("surfaces the API's own message when the payload carries one", async () => { |
| 111 | + // Given |
| 112 | + fetchMock.mockResolvedValue(new Response("{}", { status: 400 })); |
| 113 | + handleApiResponseMock.mockResolvedValue({ |
| 114 | + error: "Bad request", |
| 115 | + errors: [{ detail: "This field is required." }], |
| 116 | + }); |
| 117 | + |
| 118 | + // When |
| 119 | + const result = await submitOnboardingProfile(ANSWERS); |
| 120 | + |
| 121 | + // Then |
| 122 | + expect(result).toEqual({ |
| 123 | + stored: false, |
| 124 | + error: "This field is required.", |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + it("treats a thrown server error as not stored", async () => { |
| 129 | + // Given — `handleApiResponse` throws on 5xx. |
| 130 | + fetchMock.mockResolvedValue(new Response("{}", { status: 500 })); |
| 131 | + handleApiResponseMock.mockRejectedValue(new Error("server error")); |
| 132 | + |
| 133 | + // When |
| 134 | + const result = await skipOnboardingProfile(); |
| 135 | + |
| 136 | + // Then |
| 137 | + expect(result.stored).toBe(false); |
| 138 | + expect(handleApiErrorMock).toHaveBeenCalledTimes(1); |
| 139 | + }); |
| 140 | + |
| 141 | + it("refuses answers outside the declared vocabulary", async () => { |
| 142 | + // When / Then — validation happens before any request. |
| 143 | + await expect( |
| 144 | + submitOnboardingProfile({ |
| 145 | + ...ANSWERS, |
| 146 | + declared_role: "ceo", |
| 147 | + } as unknown as typeof ANSWERS), |
| 148 | + ).rejects.toThrow(); |
| 149 | + expect(fetchMock).not.toHaveBeenCalled(); |
| 150 | + }); |
| 151 | + |
| 152 | + it.each([ |
| 153 | + ["an empty list", { data: [] }, false], |
| 154 | + ["a stored row", { data: [{ id: "p1" }] }, true], |
| 155 | + ])("reads %s as recorded=%s", async (_, payload, expected) => { |
| 156 | + // Given |
| 157 | + fetchMock.mockResolvedValue( |
| 158 | + new Response(JSON.stringify(payload), { status: 200 }), |
| 159 | + ); |
| 160 | + |
| 161 | + // Then |
| 162 | + expect(await isOnboardingProfileRecorded()).toBe(expected); |
| 163 | + }); |
| 164 | + |
| 165 | + it("returns undefined when the read fails, so the gate fails open", async () => { |
| 166 | + // Given |
| 167 | + fetchMock.mockResolvedValue(new Response("{}", { status: 500 })); |
| 168 | + |
| 169 | + // Then |
| 170 | + expect(await isOnboardingProfileRecorded()).toBeUndefined(); |
| 171 | + }); |
| 172 | +}); |
0 commit comments