How to check an attribute value of a react component in Jest test case?

  javascript, jestjs, reactjs, unit-testing

I have a functional component which renders another component. I need to test an attribute value of that based on state. I’ve tried various options but the tests fail, as it returns HTML body and not particular jsx element value. Here is the component structure-

export default function student(){
    return(.....
    <Personal attribute_1="daisy" .../>
    )
    }

In test file i have implemented the following way(using Jest)-

test('should render based on state',()=>{
const component=render(
<SchoolContext.Provider>
...
</student>
</SchoolContext.Provider>
)
}
)

How should I write the expect statement that will check that when state is something, attribute_1 value should be "daisy"?
I tried getAllByText, screen, getAttribute(‘attribute_1′,’daisy’), component.container.(all kinds of arguments), but not able to figure it out.

Source: Ask Javascript Questions

LEAVE A COMMENT