I have an element with a long class that contains a dynamic suffix to denote the type of icon that should be displayed, think of some-very-long-classname-for-an-icon-{iconType}
, where {iconType}
is likely to be filled with a string like success
or error
etc.
In React Testing Library, I am wanting to assert that I have a particular icon class applied, however I don’t want to clutter up my tests by having the full prefix some-very-long-classname-for-an-
at each part.
Rather than store this prefix as a variable I was wondering if it would be possible to use regex to specify? Something along the lines of /icon-success$/
.
I was thinking of using toHaveClass
as I know this will work when specifying the full class name for the test:
expect(myComp).toHaveClass('some-very-long-classname-for-an-icon-success');
however, upon my travels I have found that toHaveClass
doesn’t take Regex 🙁
I did think of doing something along the lines of
expect(myComp).toHaveClass(expect.stringContaining('icon-success'));
expect(myComp).toHaveClass(expect.stringMatching(/icon-success$/));
however these also didn’t seem to work…