11import React from "react"
2+ import { MockedProvider } from "@apollo/client/testing"
23import {
34 fireEvent ,
45 render ,
@@ -7,8 +8,10 @@ import {
78 within ,
89} from "@testing-library/react"
910import { UserAccountView } from "../user-account-view"
11+ import { GET_USER_BY_ORCID , UPDATE_USER } from "../user-query"
1012
1113const baseUser = {
14+ id : "1" ,
1215 name : "John Doe" ,
13161417 orcid : "0000-0001-2345-6789" ,
@@ -18,61 +21,132 @@ const baseUser = {
1821 github : "johndoe" ,
1922}
2023
24+ const mocks = [
25+ {
26+ request : {
27+ query : GET_USER_BY_ORCID ,
28+ variables : { userId : baseUser . id } ,
29+ } ,
30+ result : {
31+ data : {
32+ user : baseUser ,
33+ } ,
34+ } ,
35+ } ,
36+ {
37+ request : {
38+ query : UPDATE_USER ,
39+ variables : {
40+ id : baseUser . id ,
41+ location : "Marin, CA" ,
42+ links : [ "https://newlink.com" ] ,
43+ institution : "New University" ,
44+ } ,
45+ } ,
46+ result : {
47+ data : {
48+ updateUser : {
49+ id : baseUser . id ,
50+ location : "Marin, CA" ,
51+ links : [ "https://newlink.com" ] ,
52+ institution : "New University" ,
53+ } ,
54+ } ,
55+ } ,
56+ } ,
57+ ]
58+
2159describe ( "<UserAccountView />" , ( ) => {
2260 it ( "should render the user details correctly" , ( ) => {
23- render ( < UserAccountView user = { baseUser } /> )
24-
25- // Check if user details are rendered
61+ render (
62+ < MockedProvider mocks = { mocks } addTypename = { false } >
63+ < UserAccountView user = { baseUser } />
64+ </ MockedProvider > ,
65+ )
2666 expect ( screen . getByText ( "Name:" ) ) . toBeInTheDocument ( )
2767 expect ( screen . getByText ( "John Doe" ) ) . toBeInTheDocument ( )
2868 expect ( screen . getByText ( "Email:" ) ) . toBeInTheDocument ( )
2969 expect ( screen . getByText ( "[email protected] " ) ) . toBeInTheDocument ( ) 3070 expect ( screen . getByText ( "ORCID:" ) ) . toBeInTheDocument ( )
3171 expect ( screen . getByText ( "0000-0001-2345-6789" ) ) . toBeInTheDocument ( )
72+ expect ( screen . getByText ( "GitHub:" ) ) . toBeInTheDocument ( )
3273 expect ( screen . getByText ( "johndoe" ) ) . toBeInTheDocument ( )
3374 } )
3475
35- it ( "should render links with EditableContent" , async ( ) => {
36- render ( < UserAccountView user = { baseUser } /> )
37- const institutionSection = within (
38- screen . getByText ( "Institution" ) . closest ( ".user-meta-block" ) ,
76+ it ( "should render location with EditableContent" , async ( ) => {
77+ render (
78+ < MockedProvider mocks = { mocks } addTypename = { false } >
79+ < UserAccountView user = { baseUser } />
80+ </ MockedProvider > ,
3981 )
82+ const locationSection = within ( screen . getByTestId ( "location-section" ) )
83+ expect ( screen . getByText ( "Location" ) ) . toBeInTheDocument ( )
84+ const editButton = locationSection . getByText ( "Edit" )
85+ fireEvent . click ( editButton )
86+ const textbox = locationSection . getByRole ( "textbox" )
87+ fireEvent . change ( textbox , { target : { value : "Marin, CA" } } )
88+ const saveButton = locationSection . getByText ( "Save" )
89+ fireEvent . click ( saveButton )
90+ await waitFor ( ( ) => {
91+ expect ( locationSection . getByText ( "Marin, CA" ) ) . toBeInTheDocument ( )
92+ } )
93+ } )
94+
95+ it ( "should render institution with EditableContent" , async ( ) => {
96+ render (
97+ < MockedProvider mocks = { mocks } addTypename = { false } >
98+ < UserAccountView user = { baseUser } />
99+ </ MockedProvider > ,
100+ )
101+ const institutionSection = within ( screen . getByTestId ( "institution-section" ) )
40102 expect ( screen . getByText ( "Institution" ) ) . toBeInTheDocument ( )
41103 const editButton = institutionSection . getByText ( "Edit" )
42104 fireEvent . click ( editButton )
43105 const textbox = institutionSection . getByRole ( "textbox" )
44106 fireEvent . change ( textbox , { target : { value : "New University" } } )
45107 const saveButton = institutionSection . getByText ( "Save" )
46- const closeButton = institutionSection . getByText ( "Close" )
47108 fireEvent . click ( saveButton )
48- fireEvent . click ( closeButton )
49- // Add debug step
50- await waitFor ( ( ) => screen . debug ( ) )
51- // Use a flexible matcher to check for text
52- await waitFor ( ( ) =>
109+ await waitFor ( ( ) => {
53110 expect ( institutionSection . getByText ( "New University" ) ) . toBeInTheDocument ( )
54- )
111+ } )
55112 } )
56113
57- it ( "should render location with EditableContent" , async ( ) => {
58- render ( < UserAccountView user = { baseUser } /> )
59- const locationSection = within (
60- screen . getByText ( "Location" ) . closest ( ".user-meta-block" ) ,
114+ it ( "should render links with EditableContent and validation" , async ( ) => {
115+ render (
116+ < MockedProvider mocks = { mocks } addTypename = { false } >
117+ < UserAccountView user = { baseUser } />
118+ </ MockedProvider > ,
61119 )
62- expect ( screen . getByText ( "Location" ) ) . toBeInTheDocument ( )
63- const editButton = locationSection . getByText ( "Edit" )
120+ const linksSection = within ( screen . getByTestId ( "links-section" ) )
121+ expect ( screen . getByText ( "Links" ) ) . toBeInTheDocument ( )
122+ const editButton = linksSection . getByText ( "Edit" )
64123 fireEvent . click ( editButton )
65- const textbox = locationSection . getByRole ( "textbox" )
66- fireEvent . change ( textbox , { target : { value : "Marin, CA" } } )
67- const saveButton = locationSection . getByText ( "Save" )
68- const closeButton = locationSection . getByText ( "Close" )
124+ const textbox = linksSection . getByRole ( "textbox" )
125+ fireEvent . change ( textbox , { target : { value : "https://newlink.com" } } )
126+ const saveButton = linksSection . getByText ( "Add" )
69127 fireEvent . click ( saveButton )
70- fireEvent . click ( closeButton )
71- // Add debug step
72- await waitFor ( ( ) => screen . debug ( ) )
73- // Use a flexible matcher to check for text
74- await waitFor ( ( ) =>
75- expect ( locationSection . getByText ( "Marin, CA" ) ) . toBeInTheDocument ( )
128+ await waitFor ( ( ) => {
129+ expect ( linksSection . getByText ( "https://newlink.com" ) ) . toBeInTheDocument ( )
130+ } )
131+ } )
132+
133+ it ( "should show an error message when invalid URL is entered in links section" , async ( ) => {
134+ render (
135+ < MockedProvider mocks = { mocks } addTypename = { false } >
136+ < UserAccountView user = { baseUser } />
137+ </ MockedProvider > ,
76138 )
139+ const linksSection = within ( screen . getByTestId ( "links-section" ) )
140+ const editButton = linksSection . getByText ( "Edit" )
141+ fireEvent . click ( editButton )
142+ const textbox = linksSection . getByRole ( "textbox" )
143+ fireEvent . change ( textbox , { target : { value : "invalid-url" } } )
144+ const saveButton = linksSection . getByText ( "Add" )
145+ fireEvent . click ( saveButton )
146+ await waitFor ( ( ) => {
147+ expect (
148+ linksSection . getByText ( "Invalid URL format. Please use a valid link." ) ,
149+ ) . toBeInTheDocument ( )
150+ } )
77151 } )
78152} )
0 commit comments