2020
2121import { screen } from '@testing-library/react' ;
2222import userEvent from '@testing-library/user-event' ;
23+ import { byRole } from '~shared/helpers/testSelector' ;
2324import { ComponentQualifier } from '~shared/types/component' ;
25+ import AlmIntegrationsServiceMock from '~sq-server-commons/api/mocks/AlmIntegrationsServiceMock' ;
2426import AlmSettingsServiceMock from '~sq-server-commons/api/mocks/AlmSettingsServiceMock' ;
2527import BranchesServiceMock from '~sq-server-commons/api/mocks/BranchesServiceMock' ;
28+ import { mockGitHubRepository } from '~sq-server-commons/helpers/mocks/alm-integrations' ;
29+ import { mockProjectAlmBindingResponse } from '~sq-server-commons/helpers/mocks/alm-settings' ;
2630import { mockMainBranch , mockPullRequest } from '~sq-server-commons/helpers/mocks/branch-like' ;
2731import { mockComponent } from '~sq-server-commons/helpers/mocks/component' ;
2832import { mockCurrentUser , mockLoggedInUser } from '~sq-server-commons/helpers/testMocks' ;
@@ -35,13 +39,29 @@ jest.mock('~sq-server-commons/api/favorites', () => ({
3539 addFavorite : jest . fn ( ) . mockResolvedValue ( { } ) ,
3640 removeFavorite : jest . fn ( ) . mockResolvedValue ( { } ) ,
3741} ) ) ;
42+ jest . mock ( '~adapters/helpers/users' , ( ) => ( {
43+ ...jest . requireActual < typeof import ( '~adapters/helpers/users' ) > ( '~adapters/helpers/users' ) ,
44+ useCurrentUser : ( ) => ( { currentUser : mockLoggedInUser ( ) } ) ,
45+ } ) ) ;
46+
47+ const ui = {
48+ bindProjectLink : byRole ( 'link' , { name : 'project_navigation.binding_status.bind' } ) ,
49+ bindingLink : byRole ( 'link' , {
50+ name : / p r o j e c t _ n a v i g a t i o n .b i n d i n g _ s t a t u s .b o u n d _ t o _ x / ,
51+ } ) ,
52+ bindingLogo : byRole ( 'img' , {
53+ name : / p r o j e c t _ n a v i g a t i o n .b i n d i n g _ s t a t u s .b o u n d _ t o _ x / ,
54+ } ) ,
55+ } ;
3856
3957const handler = new BranchesServiceMock ( ) ;
4058const almHandler = new AlmSettingsServiceMock ( ) ;
59+ const almIntegrationsHandler = new AlmIntegrationsServiceMock ( ) ;
4160
4261beforeEach ( ( ) => {
4362 handler . reset ( ) ;
4463 almHandler . reset ( ) ;
64+ almIntegrationsHandler . reset ( ) ;
4565} ) ;
4666
4767it ( 'should render correctly when there is only 1 branch' , async ( ) => {
@@ -108,6 +128,65 @@ it('should show the correct help tooltip when branch support is not enabled', as
108128 ) . toBeInTheDocument ( ) ;
109129} ) ;
110130
131+ it ( 'should show "bind project" link when project is not bound and user can bind project' , async ( ) => {
132+ renderHeader ( {
133+ component : mockComponent ( {
134+ breadcrumbs : [ { name : 'project' , key : 'project' , qualifier : ComponentQualifier . Project } ] ,
135+ configuration : {
136+ showSettings : true ,
137+ } ,
138+ } ) ,
139+ currentUser : mockLoggedInUser ( ) ,
140+ } ) ;
141+
142+ expect ( await ui . bindProjectLink . find ( ) ) . toBeInTheDocument ( ) ;
143+ } ) ;
144+
145+ it ( 'should show GitHub logo linking to repository when project is bound to GitHub' , async ( ) => {
146+ almIntegrationsHandler . githubRepositories = [
147+ mockGitHubRepository ( {
148+ url : 'https://github.com/org/repo' ,
149+ } ) ,
150+ ] ;
151+ almHandler . projectsBindings [ 'project-bound' ] = mockProjectAlmBindingResponse ( {
152+ key : 'project-bound' ,
153+ slug : 'org/repo' ,
154+ } ) ;
155+ renderHeader ( {
156+ component : mockComponent ( {
157+ breadcrumbs : [ { name : 'project' , key : 'project' , qualifier : ComponentQualifier . Project } ] ,
158+ configuration : {
159+ showSettings : true ,
160+ } ,
161+ key : 'project-bound' ,
162+ } ) ,
163+ currentUser : mockLoggedInUser ( ) ,
164+ } ) ;
165+
166+ expect ( await ui . bindingLink . find ( ) ) . toBeInTheDocument ( ) ;
167+ } ) ;
168+
169+ it ( 'should show GitLab logo (without link) when project is bound to GitLab' , async ( ) => {
170+ almHandler . projectsBindings [ 'project-bound' ] = mockProjectAlmBindingResponse ( {
171+ alm : AlmKeys . GitLab ,
172+ key : 'project-bound' ,
173+ slug : 'gitlab-repo' ,
174+ } ) ;
175+ renderHeader ( {
176+ component : mockComponent ( {
177+ breadcrumbs : [ { name : 'project' , key : 'project' , qualifier : ComponentQualifier . Project } ] ,
178+ configuration : {
179+ showSettings : true ,
180+ } ,
181+ key : 'project-bound' ,
182+ } ) ,
183+ currentUser : mockLoggedInUser ( ) ,
184+ } ) ;
185+
186+ expect ( await ui . bindingLogo . find ( ) ) . toBeInTheDocument ( ) ;
187+ expect ( ui . bindingLink . query ( ) ) . not . toBeInTheDocument ( ) ;
188+ } ) ;
189+
111190
112191function renderHeader (
113192 props ?: Partial < HeaderProps > ,
@@ -118,8 +197,8 @@ function renderHeader(
118197 '/' ,
119198 < Header
120199 component = { mockComponent ( {
121- key : 'header-project' ,
122200 breadcrumbs : [ { name : 'project' , key : 'project' , qualifier : ComponentQualifier . Project } ] ,
201+ key : 'header-project' ,
123202 } ) }
124203 currentUser = { mockCurrentUser ( ) }
125204 { ...props }
0 commit comments