|
| 1 | +import { renderHook } from '@testing-library/react' |
| 2 | +import { vi, describe, afterEach, test, expect, beforeEach } from 'vitest' |
| 3 | + |
| 4 | +import { CodyProRoutes } from './codyProRoutes' |
| 5 | +import * as subscriptionQueries from './management/api/react-query/subscriptions' |
| 6 | +import type { SubscriptionSummary } from './management/api/teamSubscriptions' |
| 7 | +import { useCodyProNavLinks } from './useCodyProNavLinks' |
| 8 | + |
| 9 | +describe('useCodyProNavLinks', () => { |
| 10 | + const useSubscriptionSummaryMock = vi.spyOn(subscriptionQueries, 'useSubscriptionSummary') |
| 11 | + |
| 12 | + afterEach(() => { |
| 13 | + useSubscriptionSummaryMock.mockReset() |
| 14 | + }) |
| 15 | + |
| 16 | + test('returns empty array if subscription summary is undefined', () => { |
| 17 | + useSubscriptionSummaryMock.mockReturnValue({ data: undefined } as ReturnType< |
| 18 | + typeof subscriptionQueries.useSubscriptionSummary |
| 19 | + >) |
| 20 | + const { result } = renderHook(() => useCodyProNavLinks()) |
| 21 | + expect(result.current).toHaveLength(0) |
| 22 | + }) |
| 23 | + |
| 24 | + test('returns empty array user is not admin', () => { |
| 25 | + const summary: SubscriptionSummary = { |
| 26 | + teamId: '018ff1b3-118c-7789-82e4-ab9106eed204', |
| 27 | + userRole: 'member', |
| 28 | + teamCurrentMembers: 2, |
| 29 | + teamMaxMembers: 6, |
| 30 | + subscriptionStatus: 'active', |
| 31 | + cancelAtPeriodEnd: false, |
| 32 | + } |
| 33 | + useSubscriptionSummaryMock.mockReturnValue({ data: summary } as ReturnType< |
| 34 | + typeof subscriptionQueries.useSubscriptionSummary |
| 35 | + >) |
| 36 | + const { result } = renderHook(() => useCodyProNavLinks()) |
| 37 | + expect(result.current).toHaveLength(0) |
| 38 | + }) |
| 39 | + |
| 40 | + describe('user is admin', () => { |
| 41 | + const summary: SubscriptionSummary = { |
| 42 | + teamId: '018ff1b3-118c-7789-82e4-ab9106eed204', |
| 43 | + userRole: 'admin', |
| 44 | + teamCurrentMembers: 2, |
| 45 | + teamMaxMembers: 6, |
| 46 | + subscriptionStatus: 'active', |
| 47 | + cancelAtPeriodEnd: false, |
| 48 | + } |
| 49 | + |
| 50 | + const setUseEmbeddedUI = (useEmbeddedUI: boolean) => { |
| 51 | + vi.stubGlobal('context', { |
| 52 | + frontendCodyProConfig: { |
| 53 | + stripePublishableKey: 'pk_test_123', |
| 54 | + sscBaseUrl: '', |
| 55 | + useEmbeddedUI, |
| 56 | + }, |
| 57 | + }) |
| 58 | + } |
| 59 | + |
| 60 | + beforeEach(() => { |
| 61 | + vi.stubGlobal('context', {}) |
| 62 | + useSubscriptionSummaryMock.mockReturnValue({ data: summary } as ReturnType< |
| 63 | + typeof subscriptionQueries.useSubscriptionSummary |
| 64 | + >) |
| 65 | + }) |
| 66 | + |
| 67 | + test.skip('returns links to subscription and team management pages if embedded UI is enabled', () => { |
| 68 | + setUseEmbeddedUI(true) |
| 69 | + const { result } = renderHook(() => useCodyProNavLinks()) |
| 70 | + expect(result.current).toHaveLength(2) |
| 71 | + expect(result.current[0].label).toBe('Manage subscription') |
| 72 | + expect(result.current[0].to).toBe(CodyProRoutes.SubscriptionManage) |
| 73 | + expect(result.current[1].label).toBe('Manage team') |
| 74 | + expect(result.current[1].to).toBe(CodyProRoutes.ManageTeam) |
| 75 | + }) |
| 76 | + |
| 77 | + test('returns link to subscription management page if embedded UI is disabled', () => { |
| 78 | + setUseEmbeddedUI(false) |
| 79 | + const { result } = renderHook(() => useCodyProNavLinks()) |
| 80 | + expect(result.current).toHaveLength(1) |
| 81 | + expect(result.current[0].label).toBe('Manage subscription') |
| 82 | + expect(result.current[0].to).toBe('https://accounts.sourcegraph.com/cody/subscription') |
| 83 | + }) |
| 84 | + }) |
| 85 | +}) |
0 commit comments