-
Notifications
You must be signed in to change notification settings - Fork 0
fix: capture user_id in sightings and generate routes #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,149 @@ | ||||||
| import { POST } from "@/app/api/books/generate/route" | ||||||
| import { createRouteHandlerClient } from "@supabase/auth-helpers-nextjs" | ||||||
| import { cookies } from "next/headers" | ||||||
| import { NextResponse } from "next/server" | ||||||
|
|
||||||
| // Mock Supabase | ||||||
| jest.mock("@supabase/auth-helpers-nextjs", () => ({ | ||||||
| createRouteHandlerClient: jest.fn(), | ||||||
| })) | ||||||
|
|
||||||
| jest.mock("@supabase/supabase-js", () => ({ | ||||||
| createClient: jest.fn(() => ({ | ||||||
| from: jest.fn(() => ({ | ||||||
| insert: jest.fn(() => ({ | ||||||
| select: jest.fn(() => ({ | ||||||
| single: jest.fn(() => ({ | ||||||
| data: { id: "test-book-id" }, | ||||||
| error: null, | ||||||
| })), | ||||||
| })), | ||||||
| })), | ||||||
| })), | ||||||
| })), | ||||||
| })) | ||||||
|
|
||||||
| // Mock Next.js headers | ||||||
| jest.mock("next/headers", () => ({ | ||||||
| cookies: jest.fn(), | ||||||
| })) | ||||||
|
|
||||||
| // Mock internal libs | ||||||
| jest.mock("@/lib/id_generator", () => ({ | ||||||
| generateBookId: jest.fn().mockResolvedValue("TEST-CODE-123"), | ||||||
| })) | ||||||
| jest.mock("@/lib/book-utils", () => ({ | ||||||
| parseBookMetadata: jest.fn(() => ({ | ||||||
| title: "Test Book", | ||||||
| author: "Test Author", | ||||||
| cover_url: "http://example.com/cover.jpg", | ||||||
| isbn: "1234567890", | ||||||
| })), | ||||||
| })) | ||||||
|
|
||||||
| describe("Generate API Auth", () => { | ||||||
| const mockCookies = { | ||||||
| getAll: jest.fn(), | ||||||
| get: jest.fn(), | ||||||
| } | ||||||
| const mockSupabase = { | ||||||
| auth: { | ||||||
| getUser: jest.fn(), | ||||||
| getSession: jest.fn(), | ||||||
| }, | ||||||
| from: jest.fn(), | ||||||
| } | ||||||
|
|
||||||
| beforeEach(() => { | ||||||
| jest.clearAllMocks(); | ||||||
| (cookies as jest.Mock).mockResolvedValue(mockCookies); | ||||||
|
||||||
| (createRouteHandlerClient as jest.Mock).mockReturnValue(mockSupabase); | ||||||
| const { createClient } = require("@supabase/supabase-js"); | ||||||
| (createClient as jest.Mock).mockReturnValue(mockSupabase); | ||||||
|
|
||||||
| // Default valid book generation mocks | ||||||
| const mockInsertSightings = jest.fn().mockResolvedValue({ error: null }); | ||||||
| const mockInsertBooks = jest.fn().mockReturnValue({ | ||||||
| select: jest.fn().mockReturnValue({ | ||||||
| single: jest.fn().mockResolvedValue({ data: { id: "book-123" }, error: null }) | ||||||
| }) | ||||||
| }); | ||||||
|
|
||||||
| mockSupabase.from.mockImplementation((table) => { | ||||||
| if (table === 'books') { | ||||||
| return { insert: mockInsertBooks } | ||||||
| } | ||||||
| if (table === 'sightings') { | ||||||
| return { insert: mockInsertSightings } | ||||||
| } | ||||||
| return { select: jest.fn() } | ||||||
| }) | ||||||
|
||||||
| }) | |
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused import NextResponse.