0

enter image description here I need to test a component(ServiceRequestDetails).That component has a layout that uses AppCOntext data. While testing ServiceRequestDetails I got the error TypeError: Cannot read properties of undefined (reading 'darkMode')..

ServiceRequestDetails.js Page. From here I tested text "Service Request Details".But I got an error.Because in the layout component I used Appcontext.

export function ServiceRequestDetails(props) {
    const ctx = useContext(AppContext);
  
    return (
        <>
            <Layout
                page="servicerequestdetails"
                title={navigationLinks()}
                subTitle={pageSubTitle()}
                showHelp={true}
                helpText={"Help"}
                helpIcon={<QuestionCircleOutlined />}
                onHelpClick={() => setShowHelpModal(!showHelpModal)}
                helpToggle={showHelpModal}
                helpTemplate={<ViewServiceRequest />}
            >

                <div className="flex items-center">
                    <div className="pr-2">
                      <img src="/images/icons/services.svg" alt="filter" />
                    </div>
                    <h3 className="text-lg font-medium">Service Request Details</h3>
                </div>

          </Layout>
        </>
    )
}

ServiceRequestDetails.test.js.It is my test case for service request details page. From here how to resolve AppCOntext issue.

test('Testing For title', () => {
  render(
    <Router>
      <ServiceRequestDetails/>
    </Router>
  )
  expect(screen.getByText("Service Request Details")).toBeInTheDocument();
})
Lin Du
  • 88,126
  • 95
  • 281
  • 483
Raju Rekadi
  • 98
  • 1
  • 8
  • check [this](https://stackoverflow.com/questions/54691799/how-to-test-a-react-component-that-is-dependent-on-usecontext-hook) – Yedidya Rashi Jul 08 '22 at 08:04
  • Your code didn't use `ctx`. Please provide [mvce](https://stackoverflow.com/help/minimal-reproducible-example) – Lin Du Jul 14 '22 at 10:04

0 Answers0