11import React from 'react' ;
2- import { render , screen , fireEvent } from '@testing-library/react' ;
2+ import { render , screen , fireEvent , within , waitFor } from '@testing-library/react' ;
33import { UserAccountView } from '../user-account-view' ;
44
55const baseUser = {
@@ -23,57 +23,47 @@ describe('<UserAccountView />', () => {
2323 expect ( screen . getByText ( '[email protected] ' ) ) . toBeInTheDocument ( ) ; 2424 expect ( screen . getByText ( 'ORCID:' ) ) . toBeInTheDocument ( ) ;
2525 expect ( screen . getByText ( '0000-0001-2345-6789' ) ) . toBeInTheDocument ( ) ;
26- expect ( screen . getByText ( 'Connect your github ' ) ) . toBeInTheDocument ( ) ;
26+ expect ( screen . getByText ( 'johndoe ' ) ) . toBeInTheDocument ( ) ;
2727 } ) ;
2828
29- it ( 'should render links with EditableContent' , ( ) => {
29+ it ( 'should render links with EditableContent' , async ( ) => {
3030 render ( < UserAccountView user = { baseUser } /> ) ;
31-
32- // Editable Links section
33- const linksSection = screen . getByText ( 'Links' ) ;
34- expect ( linksSection ) . toBeInTheDocument ( ) ;
35-
36- const linkItems = screen . getAllByRole ( 'listitem' ) ;
37- expect ( linkItems ) . toHaveLength ( 2 ) ; // initially 2 links
38-
39- // Edit a link
40- fireEvent . click ( screen . getByText ( 'Edit' ) ) ;
41- fireEvent . change ( screen . getByRole ( 'textbox' ) , { target : { value : 'https://newlink.com' } } ) ;
42- fireEvent . click ( screen . getByText ( 'Save' ) ) ;
43-
44- // Check if new link was added
45- expect ( screen . getByText ( 'https://newlink.com' ) ) . toBeInTheDocument ( ) ;
31+ const institutionSection = within ( screen . getByText ( 'Institution' ) . closest ( '.user-meta-block' ) ) ;
32+ expect ( screen . getByText ( 'Institution' ) ) . toBeInTheDocument ( ) ;
33+ const editButton = institutionSection . getByText ( 'Edit' ) ;
34+ fireEvent . click ( editButton ) ;
35+ const textbox = institutionSection . getByRole ( 'textbox' ) ;
36+ fireEvent . change ( textbox , { target : { value : 'New University' } } ) ;
37+ const saveButton = institutionSection . getByText ( 'Save' ) ;
38+ const closeButton = institutionSection . getByText ( 'Close' ) ;
39+ fireEvent . click ( saveButton ) ;
40+ fireEvent . click ( closeButton ) ;
41+ // Add debug step
42+ await waitFor ( ( ) => screen . debug ( ) ) ;
43+ // Use a flexible matcher to check for text
44+ await waitFor ( ( ) =>
45+ expect ( institutionSection . getByText ( 'New University' ) ) . toBeInTheDocument ( )
46+ ) ;
4647 } ) ;
48+
4749
48- it ( 'should render location with EditableContent' , ( ) => {
50+ it ( 'should render location with EditableContent' , async ( ) => {
4951 render ( < UserAccountView user = { baseUser } /> ) ;
50-
51- // Editable Location section
52- const locationSection = screen . getByText ( 'Location' ) ;
53- expect ( locationSection ) . toBeInTheDocument ( ) ;
54-
55- // Edit location
56- fireEvent . click ( screen . getByText ( 'Edit' ) ) ;
57- fireEvent . change ( screen . getByRole ( 'textbox' ) , { target : { value : 'Los Angeles, USA' } } ) ;
58- fireEvent . click ( screen . getByText ( 'Save' ) ) ;
59-
60- // Verify updated location
61- expect ( screen . getByText ( 'Los Angeles, USA' ) ) . toBeInTheDocument ( ) ;
62- } ) ;
63-
64- it ( 'should render institution with EditableContent' , ( ) => {
65- render ( < UserAccountView user = { baseUser } /> ) ;
66-
67- // Editable Institution section
68- const institutionSection = screen . getByText ( 'Institution' ) ;
69- expect ( institutionSection ) . toBeInTheDocument ( ) ;
70-
71- // Edit institution
72- fireEvent . click ( screen . getByText ( 'Edit' ) ) ;
73- fireEvent . change ( screen . getByRole ( 'textbox' ) , { target : { value : 'New University' } } ) ;
74- fireEvent . click ( screen . getByText ( 'Save' ) ) ;
75-
76- // Verify updated institution
77- expect ( screen . getByText ( 'New University' ) ) . toBeInTheDocument ( ) ;
52+ const locationSection = within ( screen . getByText ( 'Location' ) . closest ( '.user-meta-block' ) ) ;
53+ expect ( screen . getByText ( 'Location' ) ) . toBeInTheDocument ( ) ;
54+ const editButton = locationSection . getByText ( 'Edit' ) ;
55+ fireEvent . click ( editButton ) ;
56+ const textbox = locationSection . getByRole ( 'textbox' ) ;
57+ fireEvent . change ( textbox , { target : { value : 'Marin, CA' } } ) ;
58+ const saveButton = locationSection . getByText ( 'Save' ) ;
59+ const closeButton = locationSection . getByText ( 'Close' ) ;
60+ fireEvent . click ( saveButton ) ;
61+ fireEvent . click ( closeButton ) ;
62+ // Add debug step
63+ await waitFor ( ( ) => screen . debug ( ) ) ;
64+ // Use a flexible matcher to check for text
65+ await waitFor ( ( ) =>
66+ expect ( locationSection . getByText ( 'Marin, CA' ) ) . toBeInTheDocument ( )
67+ ) ;
7868 } ) ;
7969} ) ;
0 commit comments