I have a component Student component as follows
import axios from 'axios';
import React from 'react';
import { useEffect, useState } from 'react';
function Student(props) {
const [studentRecord, setStudentRecord] = useState(props.stRecord);
const [studentSubjects, setStudentSubjects] = useState(null);
function getStudentSubjects() {
let apicalladdress = '/studentapi/GetStudentSubjects/' + studentRecord.studentNumber;
axios.get(apicalladdress).then((result) => {
setStudentSubjects(result.data);
});
}
useEffect(() => {
getStudentSubjects();
}, [studentRecord]);
return (
<div>
<div>{studentRecord.studentNumber}</div>
<div>{setStudentSubjects[0].subjectName}</div>
</div>
);
}
I have a test created as below
import {StudentSubjectsData} from "../globalDataProvider";
import AxiosMock from "axios"
it("make sure student renders",async ()=>{
const mockStudentSubjects=await Promise.resolve({data: StudentSubjectsData()});
AxiosMock.get.mockResolvedValue(mockStudentSubjects);
render (<Student stRecord={StudentRecord}/>);
}
But I am getting following errors
Error 1. An update to Student component inside a test was not wrapped in act(...) for line :setStudentSubjects(result.data);
Error 2. For following print line, I am getting error TypeError: Cannot read properties of undefined (reading subjectName)
<div>{setStudentSubjects[0].subjectName}</div>
Any suggestions please...