Skip to content

Commit 13d16f0

Browse files
committed
Update profile tests for bug fixes and edit mode
1 parent efade16 commit 13d16f0

2 files changed

Lines changed: 101 additions & 24 deletions

File tree

src/features/profile/Profile.test.tsx

Lines changed: 97 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
import { render, screen, waitFor } from '@testing-library/react';
1+
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
22
import fetchMock, {
33
disableFetchMocks,
44
enableFetchMocks,
55
} from 'jest-fetch-mock';
66
import type { MockParams } from 'jest-fetch-mock';
7+
import { ThemeProvider } from '@mui/material';
78
import { Provider } from 'react-redux';
89
import { MemoryRouter as Router } from 'react-router-dom';
910
import Routes from '../../app/Routes';
1011
import { createTestStore } from '../../app/store';
1112
import { baseApi } from '../../common/api';
13+
import { theme } from '../../theme';
1214
import { setAuth, TokenInfo } from '../auth/authSlice';
1315
import {
1416
realname,
@@ -18,12 +20,34 @@ import {
1820
} from '../common';
1921
import {
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

2852
export 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(/infobox/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
});

src/stories/components/Profile.stories.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ComponentStory, ComponentMeta } from '@storybook/react';
22

33
import { Profile, ProfileParams } from '../../features/profile/Profile';
4+
import { KBASEUITEST_PROFILE } from '../../test/data/user_profile';
45

56
export default {
67
title: 'Components/Profile',
@@ -42,9 +43,9 @@ Default.args = {
4243
narrativesLink: '/profile/narratives',
4344
pageTitle: 'Some profile',
4445
profileLink: '/profile',
45-
profileData: {},
46-
realname: 'Some Realname',
47-
username: 'someusername',
46+
profileData: KBASEUITEST_PROFILE.profile,
47+
realname: KBASEUITEST_PROFILE.user.realname,
48+
username: KBASEUITEST_PROFILE.user.username,
4849
viewMine: true,
4950
viewNarratives: false,
5051
};

0 commit comments

Comments
 (0)