|
| 1 | +import type { SupabaseClient } from "@supabase/supabase-js"; |
| 2 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 3 | +import { liveProvider } from "../../src/liveProvider"; |
| 4 | + |
| 5 | +describe("liveProvider", () => { |
| 6 | + afterEach(() => { |
| 7 | + vi.restoreAllMocks(); |
| 8 | + }); |
| 9 | + |
| 10 | + const createMockSupabaseClient = () => { |
| 11 | + const realtimeChannel = { |
| 12 | + on: vi.fn().mockReturnThis(), |
| 13 | + subscribe: vi.fn().mockReturnThis(), |
| 14 | + }; |
| 15 | + |
| 16 | + const client = { |
| 17 | + channel: vi.fn().mockReturnValue(realtimeChannel), |
| 18 | + removeChannel: vi.fn(), |
| 19 | + rest: { schemaName: "public" }, |
| 20 | + } as unknown as SupabaseClient<any, any, any>; |
| 21 | + |
| 22 | + return { client, realtimeChannel }; |
| 23 | + }; |
| 24 | + |
| 25 | + it("uses only the first realtime filter and warns when multiple filters are provided", () => { |
| 26 | + const warnSpy = vi |
| 27 | + .spyOn(console, "warn") |
| 28 | + .mockImplementation(() => undefined); |
| 29 | + const { client, realtimeChannel } = createMockSupabaseClient(); |
| 30 | + const provider = liveProvider(client); |
| 31 | + |
| 32 | + provider.subscribe({ |
| 33 | + channel: "resources/posts", |
| 34 | + types: ["created", "updated"], |
| 35 | + callback: vi.fn(), |
| 36 | + params: { |
| 37 | + filters: [ |
| 38 | + { field: "id", operator: "eq", value: 1 }, |
| 39 | + { field: "status", operator: "eq", value: "published" }, |
| 40 | + ], |
| 41 | + }, |
| 42 | + }); |
| 43 | + |
| 44 | + expect(client.channel).toHaveBeenCalledWith( |
| 45 | + "resources/posts:INSERT|UPDATE:id=eq.1", |
| 46 | + ); |
| 47 | + expect(realtimeChannel.on).toHaveBeenCalledTimes(2); |
| 48 | + expect(realtimeChannel.on).toHaveBeenCalledWith( |
| 49 | + "postgres_changes", |
| 50 | + expect.objectContaining({ |
| 51 | + event: "INSERT", |
| 52 | + filter: "id=eq.1", |
| 53 | + schema: "public", |
| 54 | + table: "posts", |
| 55 | + }), |
| 56 | + expect.any(Function), |
| 57 | + ); |
| 58 | + expect(realtimeChannel.on).toHaveBeenCalledWith( |
| 59 | + "postgres_changes", |
| 60 | + expect.objectContaining({ |
| 61 | + event: "UPDATE", |
| 62 | + filter: "id=eq.1", |
| 63 | + schema: "public", |
| 64 | + table: "posts", |
| 65 | + }), |
| 66 | + expect.any(Function), |
| 67 | + ); |
| 68 | + expect(realtimeChannel.subscribe).toHaveBeenCalledTimes(1); |
| 69 | + expect(warnSpy).toHaveBeenCalledTimes(1); |
| 70 | + expect(warnSpy).toHaveBeenCalledWith( |
| 71 | + expect.stringContaining(`Using only the first filter: "id=eq.1".`), |
| 72 | + ); |
| 73 | + }); |
| 74 | + |
| 75 | + it("does not warn when a single realtime filter is provided", () => { |
| 76 | + const warnSpy = vi |
| 77 | + .spyOn(console, "warn") |
| 78 | + .mockImplementation(() => undefined); |
| 79 | + const { client } = createMockSupabaseClient(); |
| 80 | + const provider = liveProvider(client); |
| 81 | + |
| 82 | + provider.subscribe({ |
| 83 | + channel: "resources/posts", |
| 84 | + types: ["updated"], |
| 85 | + callback: vi.fn(), |
| 86 | + params: { |
| 87 | + filters: [{ field: "status", operator: "eq", value: "published" }], |
| 88 | + }, |
| 89 | + }); |
| 90 | + |
| 91 | + expect(client.channel).toHaveBeenCalledWith( |
| 92 | + "resources/posts:UPDATE:status=eq.published", |
| 93 | + ); |
| 94 | + expect(warnSpy).not.toHaveBeenCalled(); |
| 95 | + }); |
| 96 | +}); |
0 commit comments