Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 58 additions & 2 deletions src/createBrowserClient.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
import { describe, it, expect } from "vitest";
import { describe, it, expect, vi, beforeEach } from "vitest";

import { MAX_CHUNK_SIZE, stringToBase64URL } from "./utils";
import { CookieOptions } from "./types";
import { createBrowserClient } from "./createBrowserClient";

describe("createServerClient", () => {
// Spy on createClient to capture auth options passed through
const createClientSpy = vi.fn().mockReturnValue({
auth: {},
realtime: {},
});

vi.mock("@supabase/supabase-js", () => ({
createClient: (...args: any[]) => createClientSpy(...args),
}));

describe("createBrowserClient", () => {
beforeEach(() => {
createClientSpy.mockClear();
});

describe("validation", () => {
it("should throw an error on empty URL and anon key", async () => {
expect(() => {
Expand All @@ -16,4 +30,46 @@ describe("createServerClient", () => {
}).toThrow();
});
});

describe("auth options passthrough", () => {
it("should respect autoRefreshToken when explicitly set to false", () => {
createBrowserClient("http://localhost", "anon-key", {
isSingleton: false,
auth: { autoRefreshToken: false },
});

const passedOptions = createClientSpy.mock.calls[0][2];
expect(passedOptions.auth.autoRefreshToken).toBe(false);
});

it("should respect detectSessionInUrl when explicitly set to false", () => {
createBrowserClient("http://localhost", "anon-key", {
isSingleton: false,
auth: { detectSessionInUrl: false },
});

const passedOptions = createClientSpy.mock.calls[0][2];
expect(passedOptions.auth.detectSessionInUrl).toBe(false);
});

it("should respect persistSession when explicitly set to false", () => {
createBrowserClient("http://localhost", "anon-key", {
isSingleton: false,
auth: { persistSession: false },
});

const passedOptions = createClientSpy.mock.calls[0][2];
expect(passedOptions.auth.persistSession).toBe(false);
});

it("should use defaults when auth options are not provided", () => {
createBrowserClient("http://localhost", "anon-key", {
isSingleton: false,
});

const passedOptions = createClientSpy.mock.calls[0][2];
// defaults come from isBrowser() for autoRefreshToken/detectSessionInUrl, true for persistSession
expect(passedOptions.auth.persistSession).toBe(true);
});
});
});
6 changes: 3 additions & 3 deletions src/createBrowserClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ export function createBrowserClient<
? { storageKey: options.cookieOptions.name }
: null),
flowType: "pkce",
autoRefreshToken: isBrowser(),
detectSessionInUrl: isBrowser(),
persistSession: true,
autoRefreshToken: options?.auth?.autoRefreshToken ?? isBrowser(),
detectSessionInUrl: options?.auth?.detectSessionInUrl ?? isBrowser(),
persistSession: options?.auth?.persistSession ?? true,
storage,
...(options?.cookies &&
"encode" in options.cookies &&
Expand Down