@@ -2,14 +2,14 @@ import { render, screen } from '@testing-library/react'
22import userEvent from '@testing-library/user-event'
33import { MemoryRouter } from 'react-router-dom'
44import sinon from 'sinon'
5- import { afterAll , beforeAll , describe , expect , test , vi } from 'vitest'
5+ import { afterAll , beforeAll , afterEach , describe , expect , test , vi , beforeEach } from 'vitest'
66
77import { NOOP_TELEMETRY_SERVICE } from '@sourcegraph/shared/src/telemetry/telemetryService'
88import { MockedTestProvider } from '@sourcegraph/shared/src/testing/apollo'
99import { AnchorLink , RouterLink , setLinkComponent } from '@sourcegraph/wildcard'
1010import { renderWithBrandedContext } from '@sourcegraph/wildcard/src/testing'
1111
12- import { CodyProApiProvider } from '../cody/management/api/react-query/CodyProApiProvider '
12+ import * as codyProHooks from '../cody/useCodyProNavLinks '
1313
1414import { UserNavItem , type UserNavItemProps } from './UserNavItem'
1515
@@ -28,6 +28,14 @@ describe('UserNavItem', () => {
2828 setLinkComponent ( AnchorLink )
2929 } )
3030
31+ const useCodyProNavLinksMock = vi . spyOn ( codyProHooks , 'useCodyProNavLinks' )
32+ beforeEach ( ( ) => {
33+ useCodyProNavLinksMock . mockReturnValue ( [ ] )
34+ } )
35+ afterEach ( ( ) => {
36+ useCodyProNavLinksMock . mockReset ( )
37+ } )
38+
3139 const USER : UserNavItemProps [ 'authenticatedUser' ] = {
3240 username : 'alice' ,
3341 displayName : 'alice doe' ,
@@ -58,22 +66,18 @@ describe('UserNavItem', () => {
5866 emails : [ ] ,
5967 }
6068
61- const isSourcegraphDotCom = true
62-
6369 test ( 'simple' , ( ) => {
6470 expect (
6571 render (
6672 < MemoryRouter >
6773 < MockedTestProvider >
68- < CodyProApiProvider isSourcegraphDotCom = { isSourcegraphDotCom } >
69- < UserNavItem
70- showKeyboardShortcutsHelp = { ( ) => undefined }
71- authenticatedUser = { USER }
72- isSourcegraphDotCom = { true }
73- showFeedbackModal = { ( ) => undefined }
74- telemetryService = { NOOP_TELEMETRY_SERVICE }
75- />
76- </ CodyProApiProvider >
74+ < UserNavItem
75+ showKeyboardShortcutsHelp = { ( ) => undefined }
76+ authenticatedUser = { USER }
77+ isSourcegraphDotCom = { true }
78+ showFeedbackModal = { ( ) => undefined }
79+ telemetryService = { NOOP_TELEMETRY_SERVICE }
80+ />
7781 </ MockedTestProvider >
7882 </ MemoryRouter >
7983 ) . asFragment ( )
@@ -83,15 +87,13 @@ describe('UserNavItem', () => {
8387 test ( 'logout click triggers page refresh instead of performing client-side only navigation' , async ( ) => {
8488 const result = renderWithBrandedContext (
8589 < MockedTestProvider >
86- < CodyProApiProvider isSourcegraphDotCom = { isSourcegraphDotCom } >
87- < UserNavItem
88- showKeyboardShortcutsHelp = { ( ) => undefined }
89- authenticatedUser = { USER }
90- isSourcegraphDotCom = { isSourcegraphDotCom }
91- showFeedbackModal = { ( ) => undefined }
92- telemetryService = { NOOP_TELEMETRY_SERVICE }
93- />
94- </ CodyProApiProvider >
90+ < UserNavItem
91+ showKeyboardShortcutsHelp = { ( ) => undefined }
92+ authenticatedUser = { USER }
93+ isSourcegraphDotCom = { true }
94+ showFeedbackModal = { ( ) => undefined }
95+ telemetryService = { NOOP_TELEMETRY_SERVICE }
96+ />
9597 </ MockedTestProvider >
9698 )
9799
@@ -104,4 +106,54 @@ describe('UserNavItem', () => {
104106 expect ( result . locationRef . entries . length ) . toBe ( 1 )
105107 expect ( result . locationRef . entries . find ( ( { pathname } ) => pathname . includes ( 'sign-out' ) ) ) . toBe ( undefined )
106108 } )
109+
110+ describe ( 'Cody Pro section' , ( ) => {
111+ const setup = ( isSourcegraphDotCom : boolean ) => {
112+ renderWithBrandedContext (
113+ < MockedTestProvider >
114+ < UserNavItem
115+ showKeyboardShortcutsHelp = { ( ) => undefined }
116+ authenticatedUser = { USER }
117+ isSourcegraphDotCom = { isSourcegraphDotCom }
118+ showFeedbackModal = { ( ) => undefined }
119+ telemetryService = { NOOP_TELEMETRY_SERVICE }
120+ />
121+ </ MockedTestProvider >
122+ )
123+ userEvent . click ( screen . getByRole ( 'button' ) )
124+ }
125+
126+ describe ( 'dotcom' , ( ) => {
127+ test ( 'renders provided links' , ( ) => {
128+ const links = [
129+ { to : '/foo' , label : 'Foo' } ,
130+ { to : '/bar' , label : 'Bar' } ,
131+ ]
132+ useCodyProNavLinksMock . mockReturnValue ( links )
133+ setup ( true )
134+
135+ for ( const link of links ) {
136+ const el = screen . getByText ( link . label )
137+ expect ( el ) . toHaveAttribute ( 'href' , link . to )
138+ }
139+ } )
140+
141+ test ( 'is not rendered if no links provided' , ( ) => {
142+ useCodyProNavLinksMock . mockReturnValue ( [ ] )
143+ setup ( true )
144+
145+ expect ( useCodyProNavLinksMock ) . toHaveBeenCalled ( )
146+ expect ( screen . queryByText ( 'Cody Pro' ) ) . not . toBeInTheDocument ( )
147+ } )
148+ } )
149+
150+ describe ( 'enterprise' , ( ) => {
151+ test ( 'is not rendered' , ( ) => {
152+ setup ( false )
153+
154+ // Cody Pro section is not rendered thus useCodyProNavLinks hook is not called
155+ expect ( useCodyProNavLinksMock ) . not . toHaveBeenCalled ( )
156+ } )
157+ } )
158+ } )
107159} )
0 commit comments