@@ -2,14 +2,14 @@ import { render, screen } from '@testing-library/react'
2
2
import userEvent from '@testing-library/user-event'
3
3
import { MemoryRouter } from 'react-router-dom'
4
4
import 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'
6
6
7
7
import { NOOP_TELEMETRY_SERVICE } from '@sourcegraph/shared/src/telemetry/telemetryService'
8
8
import { MockedTestProvider } from '@sourcegraph/shared/src/testing/apollo'
9
9
import { AnchorLink , RouterLink , setLinkComponent } from '@sourcegraph/wildcard'
10
10
import { renderWithBrandedContext } from '@sourcegraph/wildcard/src/testing'
11
11
12
- import { CodyProApiProvider } from '../cody/management/api/react-query/CodyProApiProvider '
12
+ import * as codyProHooks from '../cody/useCodyProNavLinks '
13
13
14
14
import { UserNavItem , type UserNavItemProps } from './UserNavItem'
15
15
@@ -28,6 +28,14 @@ describe('UserNavItem', () => {
28
28
setLinkComponent ( AnchorLink )
29
29
} )
30
30
31
+ const useCodyProNavLinksMock = vi . spyOn ( codyProHooks , 'useCodyProNavLinks' )
32
+ beforeEach ( ( ) => {
33
+ useCodyProNavLinksMock . mockReturnValue ( [ ] )
34
+ } )
35
+ afterEach ( ( ) => {
36
+ useCodyProNavLinksMock . mockReset ( )
37
+ } )
38
+
31
39
const USER : UserNavItemProps [ 'authenticatedUser' ] = {
32
40
username : 'alice' ,
33
41
displayName : 'alice doe' ,
@@ -58,22 +66,18 @@ describe('UserNavItem', () => {
58
66
emails : [ ] ,
59
67
}
60
68
61
- const isSourcegraphDotCom = true
62
-
63
69
test ( 'simple' , ( ) => {
64
70
expect (
65
71
render (
66
72
< MemoryRouter >
67
73
< 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
+ />
77
81
</ MockedTestProvider >
78
82
</ MemoryRouter >
79
83
) . asFragment ( )
@@ -83,15 +87,13 @@ describe('UserNavItem', () => {
83
87
test ( 'logout click triggers page refresh instead of performing client-side only navigation' , async ( ) => {
84
88
const result = renderWithBrandedContext (
85
89
< 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
+ />
95
97
</ MockedTestProvider >
96
98
)
97
99
@@ -104,4 +106,54 @@ describe('UserNavItem', () => {
104
106
expect ( result . locationRef . entries . length ) . toBe ( 1 )
105
107
expect ( result . locationRef . entries . find ( ( { pathname } ) => pathname . includes ( 'sign-out' ) ) ) . toBe ( undefined )
106
108
} )
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
+ } )
107
159
} )
0 commit comments