11import React from 'react' ;
2- import { mount } from 'enzyme' ;
2+ import { mount } from 'enzyme' ;
33
44import Client from '@/client' ;
55
6- import { LinkTooltip } from './link_tooltip' ;
6+ import { LinkTooltip } from './link_tooltip' ;
77
88jest . mock ( '@/client' , ( ) => ( {
99 getIssue : jest . fn ( ) ,
1010 getPullRequest : jest . fn ( ) ,
1111} ) ) ;
1212
13- jest . mock ( 'react-markdown' , ( ) => ( ) => < div /> ) ;
13+ jest . mock ( 'react-markdown' , ( ) => ( ) => < div /> ) ;
1414
1515describe ( 'LinkTooltip' , ( ) => {
1616 const baseProps = {
@@ -37,11 +37,7 @@ describe('LinkTooltip', () => {
3737 } ) ;
3838
3939 test ( 'should fetch issue for github.com link' , ( ) => {
40- // We need to use mount or wait for useEffect?
41- // shallow renders the component, useEffect is a hook.
42- // Enzyme shallow supports hooks in newer versions, but let's check if we need to manually trigger logic.
43- // The component uses useEffect to call initData.
44- wrapper = mount ( < LinkTooltip { ...baseProps } /> ) ;
40+ wrapper = mount ( < LinkTooltip { ...baseProps } /> ) ;
4541 expect ( Client . getIssue ) . toHaveBeenCalledWith ( 'mattermost' , 'mattermost-plugin-github' , '1' ) ;
4642 } ) ;
4743
@@ -50,7 +46,7 @@ describe('LinkTooltip', () => {
5046 ...baseProps ,
5147 href : 'https://github.com/mattermost/mattermost-plugin-github/pull/2' ,
5248 } ;
53- wrapper = mount ( < LinkTooltip { ...props } /> ) ;
49+ wrapper = mount ( < LinkTooltip { ...props } /> ) ;
5450 expect ( Client . getPullRequest ) . toHaveBeenCalledWith ( 'mattermost' , 'mattermost-plugin-github' , '2' ) ;
5551 } ) ;
5652
@@ -60,7 +56,7 @@ describe('LinkTooltip', () => {
6056 href : 'https://github.example.com/mattermost/mattermost-plugin-github/issues/3' ,
6157 enterpriseURL : 'https://github.example.com' ,
6258 } ;
63- wrapper = mount ( < LinkTooltip { ...props } /> ) ;
59+ wrapper = mount ( < LinkTooltip { ...props } /> ) ;
6460 expect ( Client . getIssue ) . toHaveBeenCalledWith ( 'mattermost' , 'mattermost-plugin-github' , '3' ) ;
6561 } ) ;
6662
@@ -70,7 +66,7 @@ describe('LinkTooltip', () => {
7066 href : 'https://github.example.com/mattermost/mattermost-plugin-github/pull/4' ,
7167 enterpriseURL : 'https://github.example.com' ,
7268 } ;
73- wrapper = mount ( < LinkTooltip { ...props } /> ) ;
69+ wrapper = mount ( < LinkTooltip { ...props } /> ) ;
7470 expect ( Client . getPullRequest ) . toHaveBeenCalledWith ( 'mattermost' , 'mattermost-plugin-github' , '4' ) ;
7571 } ) ;
7672
@@ -80,7 +76,7 @@ describe('LinkTooltip', () => {
8076 href : 'https://github.example.com/mattermost/mattermost-plugin-github/issues/5' ,
8177 enterpriseURL : 'https://github.example.com/' ,
8278 } ;
83- wrapper = mount ( < LinkTooltip { ...props } /> ) ;
79+ wrapper = mount ( < LinkTooltip { ...props } /> ) ;
8480 expect ( Client . getIssue ) . toHaveBeenCalledWith ( 'mattermost' , 'mattermost-plugin-github' , '5' ) ;
8581 } ) ;
8682
@@ -90,7 +86,108 @@ describe('LinkTooltip', () => {
9086 href : 'https://other-github.com/mattermost/mattermost-plugin-github/issues/6' ,
9187 enterpriseURL : 'https://github.example.com' ,
9288 } ;
93- wrapper = mount ( < LinkTooltip { ...props } /> ) ;
89+ wrapper = mount ( < LinkTooltip { ...props } /> ) ;
9490 expect ( Client . getIssue ) . not . toHaveBeenCalled ( ) ;
9591 } ) ;
92+
93+ test ( 'should use html_url for opened by link if available' , async ( ) => {
94+ Client . getIssue . mockResolvedValueOnce ( {
95+ id : 1 ,
96+ title : 'Test Issue' ,
97+ body : 'Description' ,
98+ user : {
99+ login : 'testuser' ,
100+ html_url : 'https://github.com/testuser/profile' ,
101+ } ,
102+ state : 'open' ,
103+ labels : [ ] ,
104+ created_at : '2023-01-01T00:00:00Z' ,
105+ } ) ;
106+
107+ wrapper = mount ( < LinkTooltip { ...baseProps } /> ) ;
108+
109+ await new Promise ( ( resolve ) => setTimeout ( resolve , 0 ) ) ;
110+ wrapper . update ( ) ;
111+
112+ const link = wrapper . find ( '.opened-by a' ) ;
113+ expect ( link . exists ( ) ) . toBe ( true ) ;
114+ expect ( link . prop ( 'href' ) ) . toBe ( 'https://github.com/testuser/profile' ) ;
115+ } ) ;
116+
117+ test ( 'should fallback to enterprise URL for opened by link if html_url missing' , async ( ) => {
118+ Client . getIssue . mockResolvedValueOnce ( {
119+ id : 1 ,
120+ title : 'Test Enterprise Issue' ,
121+ body : 'Description' ,
122+ user : {
123+ login : 'entuser' ,
124+ } ,
125+ state : 'open' ,
126+ labels : [ ] ,
127+ created_at : '2023-01-01T00:00:00Z' ,
128+ } ) ;
129+
130+ const props = {
131+ ...baseProps ,
132+ href : 'https://github.example.com/mattermost/mattermost-plugin-github/issues/3' ,
133+ enterpriseURL : 'https://github.example.com' ,
134+ } ;
135+ wrapper = mount ( < LinkTooltip { ...props } /> ) ;
136+
137+ await new Promise ( ( resolve ) => setTimeout ( resolve , 0 ) ) ;
138+ wrapper . update ( ) ;
139+
140+ const link = wrapper . find ( '.opened-by a' ) ;
141+ expect ( link . exists ( ) ) . toBe ( true ) ;
142+ expect ( link . prop ( 'href' ) ) . toBe ( 'https://github.example.com/entuser' ) ;
143+ } ) ;
144+
145+ test ( 'should handle enterprise URL with trailing slash for opened by link fallback' , async ( ) => {
146+ Client . getIssue . mockResolvedValueOnce ( {
147+ id : 1 ,
148+ title : 'Test Enterprise Issue' ,
149+ body : 'Description' ,
150+ user : {
151+ login : 'entuser' ,
152+ } ,
153+ state : 'open' ,
154+ labels : [ ] ,
155+ created_at : '2023-01-01T00:00:00Z' ,
156+ } ) ;
157+
158+ const props = {
159+ ...baseProps ,
160+ href : 'https://github.example.com/mattermost/mattermost-plugin-github/issues/3' ,
161+ enterpriseURL : 'https://github.example.com/' ,
162+ } ;
163+ wrapper = mount ( < LinkTooltip { ...props } /> ) ;
164+
165+ await new Promise ( ( resolve ) => setTimeout ( resolve , 0 ) ) ;
166+ wrapper . update ( ) ;
167+
168+ const link = wrapper . find ( '.opened-by a' ) ;
169+ expect ( link . prop ( 'href' ) ) . toBe ( 'https://github.example.com/entuser' ) ;
170+ } ) ;
171+
172+ test ( 'should default to github.com for opened by link if no enterpriseURL and no html_url' , async ( ) => {
173+ Client . getIssue . mockResolvedValueOnce ( {
174+ id : 1 ,
175+ title : 'Test Issue' ,
176+ body : 'Description' ,
177+ user : {
178+ login : 'clouduser' ,
179+ } ,
180+ state : 'open' ,
181+ labels : [ ] ,
182+ created_at : '2023-01-01T00:00:00Z' ,
183+ } ) ;
184+
185+ wrapper = mount ( < LinkTooltip { ...baseProps } /> ) ;
186+
187+ await new Promise ( ( resolve ) => setTimeout ( resolve , 0 ) ) ;
188+ wrapper . update ( ) ;
189+
190+ const link = wrapper . find ( '.opened-by a' ) ;
191+ expect ( link . prop ( 'href' ) ) . toBe ( 'https://github.com/clouduser' ) ;
192+ } ) ;
96193} ) ;
0 commit comments