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
2 changes: 1 addition & 1 deletion __tests__/components/ui/coaching-session-selector.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ vi.mock('@/lib/hooks/use-current-coaching-session', () => ({
}))

vi.mock('@/lib/api/goals', () => ({
useGoalBySession: vi.fn(() => ({
useGoalByRelationship: vi.fn(() => ({
goal: { title: 'Test Goal' },
isLoading: false,
isError: false,
Expand Down
198 changes: 198 additions & 0 deletions __tests__/lib/api/goals.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { GoalApi, useGoalList, useGoalsBySession } from '@/lib/api/goals'
import { EntityApi } from '@/lib/api/entity-api'
import { renderHook } from '@testing-library/react'
import { TestProviders } from '@/test-utils/providers'

// Mock EntityApi
vi.mock('@/lib/api/entity-api', () => ({
EntityApi: {
listFn: vi.fn(),
listNestedFn: vi.fn(),
getFn: vi.fn(),
createFn: vi.fn(),
updateFn: vi.fn(),
deleteFn: vi.fn(),
useEntityList: vi.fn(),
useEntityMutation: vi.fn(),
},
}))

// Mock site config to provide a proper base URL
vi.mock('@/site.config', () => ({
siteConfig: {
env: {
backendServiceURL: 'http://localhost:3000',
},
},
}))

describe('GoalApi.list', () => {
beforeEach(() => {
vi.clearAllMocks()
})

it('sends coaching_relationship_id as query param', async () => {
vi.mocked(EntityApi.listFn).mockResolvedValue([])

await GoalApi.list('rel-123')

expect(EntityApi.listFn).toHaveBeenCalledWith('http://localhost:3000/goals', {
params: {
coaching_relationship_id: 'rel-123',
},
})
})

it('does not send coaching_session_id', async () => {
vi.mocked(EntityApi.listFn).mockResolvedValue([])

await GoalApi.list('rel-123')

const callArgs = vi.mocked(EntityApi.listFn).mock.calls[0]
expect(callArgs[1].params).not.toHaveProperty('coaching_session_id')
})

it('includes sort params when provided', async () => {
vi.mocked(EntityApi.listFn).mockResolvedValue([])

await GoalApi.list('rel-123', 'title', 'asc')

expect(EntityApi.listFn).toHaveBeenCalledWith('http://localhost:3000/goals', {
params: {
coaching_relationship_id: 'rel-123',
sort_by: 'title',
sort_order: 'asc',
},
})
})

it('omits sort params when not provided', async () => {
vi.mocked(EntityApi.listFn).mockResolvedValue([])

await GoalApi.list('rel-123')

const callArgs = vi.mocked(EntityApi.listFn).mock.calls[0]
expect(callArgs[1].params).not.toHaveProperty('sort_by')
expect(callArgs[1].params).not.toHaveProperty('sort_order')
})
})

describe('useGoalList hook', () => {
beforeEach(() => {
vi.clearAllMocks()
})

it('passes coaching_relationship_id to EntityApi.useEntityList', () => {
const mockReturn = {
entities: [],
isLoading: false,
isError: false,
refresh: vi.fn(),
}

vi.mocked(EntityApi.useEntityList).mockReturnValue(mockReturn)

renderHook(
() => useGoalList('rel-456'),
{ wrapper: TestProviders }
)

expect(EntityApi.useEntityList).toHaveBeenCalledWith(
'http://localhost:3000/goals',
expect.any(Function),
'rel-456'
)
})

it('passes null to EntityApi.useEntityList when relationship ID is null', () => {
const mockReturn = {
entities: [],
isLoading: false,
isError: false,
refresh: vi.fn(),
}

vi.mocked(EntityApi.useEntityList).mockReturnValue(mockReturn)

renderHook(
() => useGoalList(null),
{ wrapper: TestProviders }
)

expect(EntityApi.useEntityList).toHaveBeenCalledWith(
'http://localhost:3000/goals',
expect.any(Function),
null
)
})
})

describe('GoalApi.listNested', () => {
beforeEach(() => {
vi.clearAllMocks()
})

it('fetches goals nested under the coaching session endpoint', async () => {
vi.mocked(EntityApi.listNestedFn).mockResolvedValue([])

await GoalApi.listNested('session-123')

expect(EntityApi.listNestedFn).toHaveBeenCalledWith(
'http://localhost:3000/coaching_sessions',
'session-123',
'goals',
{}
)
})
})

describe('useGoalsBySession hook', () => {
beforeEach(() => {
vi.clearAllMocks()
})

it('passes session-scoped URL to EntityApi.useEntityList', () => {
const mockReturn = {
entities: [],
isLoading: false,
isError: false,
refresh: vi.fn(),
}

vi.mocked(EntityApi.useEntityList).mockReturnValue(mockReturn)

renderHook(
() => useGoalsBySession('session-789'),
{ wrapper: TestProviders }
)

expect(EntityApi.useEntityList).toHaveBeenCalledWith(
'http://localhost:3000/coaching_sessions/session-789/goals',
expect.any(Function),
'session-789'
)
})

it('returns goals array from entities', () => {
const mockGoals = [
{ id: 'goal-1', title: 'Test Goal' },
{ id: 'goal-2', title: 'Another Goal' },
]

vi.mocked(EntityApi.useEntityList).mockReturnValue({
entities: mockGoals as any,
isLoading: false,
isError: false,
refresh: vi.fn(),
})

const { result } = renderHook(
() => useGoalsBySession('session-789'),
{ wrapper: TestProviders }
)

expect(result.current.goals).toEqual(mockGoals)
expect(result.current.isLoading).toBe(false)
})
})
21 changes: 21 additions & 0 deletions __tests__/test-utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { DateTime } from "ts-luxon";
import { CoachingSession } from "@/types/coaching-session";
import { CoachingRelationshipWithUserNames } from "@/types/coaching_relationship";
import { Goal } from "@/types/goal";
import { ItemStatus } from "@/types/general";
import { Organization } from "@/types/organization";
import { User } from "@/types/user";
import { OAuthConnection } from "@/types/oauth-connection";
Expand Down Expand Up @@ -84,6 +86,25 @@ export function createSessionAt(minutesFromNow: number): CoachingSession {
});
}

export function createMockGoal(overrides?: Partial<Goal>): Goal {
const now = DateTime.now();
return {
id: "goal-1",
coaching_relationship_id: "rel-1",
created_in_session_id: "session-1",
user_id: "user-1",
title: "Improve communication",
body: "Work on active listening",
status: ItemStatus.NotStarted,
status_changed_at: now,
completed_at: now,
target_date: null,
created_at: now,
updated_at: now,
...overrides,
};
}

export function createMockGoogleOAuthConnectionState(
overrides?: Partial<OAuthConnection>
): OAuthConnection {
Expand Down
Loading
Loading