1- import { render , screen , waitFor } from '@testing-library/react' ;
1+ import { fireEvent , render , screen , waitFor } from '@testing-library/react' ;
22import fetchMock , {
33 disableFetchMocks ,
44 enableFetchMocks ,
55} from 'jest-fetch-mock' ;
66import type { MockParams } from 'jest-fetch-mock' ;
7+ import { ThemeProvider } from '@mui/material' ;
78import { Provider } from 'react-redux' ;
89import { MemoryRouter as Router } from 'react-router-dom' ;
910import Routes from '../../app/Routes' ;
1011import { createTestStore } from '../../app/store' ;
1112import { baseApi } from '../../common/api' ;
13+ import { theme } from '../../theme' ;
1214import { setAuth , TokenInfo } from '../auth/authSlice' ;
1315import {
1416 realname ,
@@ -18,12 +20,34 @@ import {
1820} from '../common' ;
1921import {
2022 Profile ,
21- ProfileInfobox ,
2223 ProfileNarrativesMessage ,
23- ProfileResume ,
2424 ProfileView ,
2525 ProfileWrapper ,
2626} from './Profile' ;
27+ import { ProfileData } from './profileTypes' ;
28+
29+ const EMPTY_PROFILE_DATA : ProfileData = {
30+ metadata : { createdBy : 'test' , created : new Date ( ) . toISOString ( ) } ,
31+ preferences : { } ,
32+ userdata : {
33+ organization : '' ,
34+ department : '' ,
35+ city : '' ,
36+ state : '' ,
37+ postalCode : '' ,
38+ country : '' ,
39+ researchStatement : '' ,
40+ gravatarDefault : 'identicon' ,
41+ avatarOption : 'gravatar' ,
42+ researchInterests : [ ] ,
43+ researchInterestsOther : null ,
44+ jobTitle : '' ,
45+ jobTitleOther : '' ,
46+ fundingSource : '' ,
47+ affiliations : [ ] ,
48+ } ,
49+ synced : { gravatarHash : '' } ,
50+ } ;
2751
2852export const initialState = {
2953 auth : {
@@ -37,7 +61,7 @@ export const initialState = {
3761 username : usernameRequested ,
3862 realname : realname ,
3963 } ,
40- profile : { } ,
64+ profile : EMPTY_PROFILE_DATA ,
4165 } ,
4266 } ,
4367} ;
@@ -55,7 +79,7 @@ export const profileResponseOKFactory = (
5579 username : username ,
5680 realname : realname ,
5781 } ,
58- profile : { } ,
82+ profile : EMPTY_PROFILE_DATA ,
5983 } ,
6084 ] ,
6185 ] ,
@@ -107,7 +131,7 @@ describe('Profile related components', () => {
107131 narrativesLink = { '' }
108132 pageTitle = { '' }
109133 profileLink = { '' }
110- profileData = { { } }
134+ profileData = { EMPTY_PROFILE_DATA }
111135 realname = { '' }
112136 username = { '' }
113137 viewMine = { true }
@@ -118,25 +142,22 @@ describe('Profile related components', () => {
118142 ) ;
119143 } ) ;
120144
121- test ( 'renders ProfileInfobox' , ( ) => {
122- render ( < ProfileInfobox realname = { realname } /> ) ;
123- } ) ;
124-
125145 test ( 'renders ProfileNarrativesMessage for another user' , ( ) => {
126146 render ( < ProfileNarrativesMessage realname = { realname } yours = { false } /> ) ;
127147 } ) ;
128148
129- test ( 'renders ProfileResume' , ( ) => {
130- render ( < ProfileResume /> ) ;
131- } ) ;
132-
133149 test ( 'renders ProfileView' , ( ) => {
134150 render (
135- < ProfileView
136- realname = { realname }
137- username = { '' }
138- profileData = { initialState . profile . loggedInProfile . profile }
139- />
151+ < Provider store = { createTestStore ( ) } >
152+ < Router >
153+ < ProfileView
154+ realname = { realname }
155+ username = { '' }
156+ profileData = { EMPTY_PROFILE_DATA }
157+ viewMine = { false }
158+ />
159+ </ Router >
160+ </ Provider >
140161 ) ;
141162 } ) ;
142163
@@ -200,7 +221,8 @@ describe('Profile related components', () => {
200221 usernameRequested
201222 )
202223 ) ;
203- const linkElement = screen . getByText ( / i n f o b o x / i) ;
224+ // With no realname, profile still renders with username visible
225+ const linkElement = screen . getByText ( usernameRequested ) ;
204226 expect ( linkElement ) . toBeInTheDocument ( ) ;
205227 } ) ;
206228
@@ -224,7 +246,9 @@ describe('Profile related components', () => {
224246 ) ;
225247
226248 await waitFor ( ( ) => {
227- const linkElement = screen . getByText ( realnameOther , { exact : false } ) ;
249+ const linkElement = screen . getByText ( realnameOther , {
250+ exact : false ,
251+ } ) ;
228252 expect ( linkElement ) . toBeInTheDocument ( ) ;
229253 } ) ;
230254 } ) ;
@@ -249,4 +273,56 @@ describe('Profile related components', () => {
249273 } ) ;
250274 } ) ;
251275 } ) ;
276+
277+ test ( 'shows Edit button when viewing own profile' , ( ) => {
278+ render (
279+ < Provider store = { createTestStore ( ) } >
280+ < Router >
281+ < ProfileView
282+ realname = "Test"
283+ username = "test"
284+ profileData = { EMPTY_PROFILE_DATA }
285+ viewMine = { true }
286+ />
287+ </ Router >
288+ </ Provider >
289+ ) ;
290+ expect ( screen . getByText ( 'Edit Profile' ) ) . toBeInTheDocument ( ) ;
291+ } ) ;
292+
293+ test ( 'hides Edit button when viewing another profile' , ( ) => {
294+ render (
295+ < Provider store = { createTestStore ( ) } >
296+ < Router >
297+ < ProfileView
298+ realname = "Test"
299+ username = "test"
300+ profileData = { EMPTY_PROFILE_DATA }
301+ viewMine = { false }
302+ />
303+ </ Router >
304+ </ Provider >
305+ ) ;
306+ expect ( screen . queryByText ( 'Edit Profile' ) ) . not . toBeInTheDocument ( ) ;
307+ } ) ;
308+
309+ test ( 'clicking Edit Profile shows edit form' , ( ) => {
310+ render (
311+ < Provider store = { createTestStore ( initialState ) } >
312+ < ThemeProvider theme = { theme } >
313+ < Router >
314+ < ProfileView
315+ realname = "Test"
316+ username = "test"
317+ profileData = { EMPTY_PROFILE_DATA }
318+ viewMine = { true }
319+ />
320+ </ Router >
321+ </ ThemeProvider >
322+ </ Provider >
323+ ) ;
324+ fireEvent . click ( screen . getByText ( 'Edit Profile' ) ) ;
325+ expect ( screen . getByText ( 'Save' ) ) . toBeInTheDocument ( ) ;
326+ expect ( screen . getByText ( 'Cancel' ) ) . toBeInTheDocument ( ) ;
327+ } ) ;
252328} ) ;
0 commit comments