I am trying to increase the testing coverage of a React component I am developing. This component, however, has a disabled
property, which basically stops all the component's functionallities.
However, this puts me in the annoying position of having to test the disabled
condition for every single function in my code.
Below is the relevant testing coverage:
Is there a way to test all of this at once using jest and/or react-testing library? Or is there a way for me to re-write my code so this isn't necessary?
I thank you in advance for any help provided.
You can use a loop to generate variations of tests.
describe("Component", () => {
[false, true].map((disabled) => {
it(`does a thing with disabled=${disabled}`, () => { ... });
});
});
Alternately, annotate those lines with /* istanbul ignore next */
:
/* istanbul ignore next */
if(disabled) return;
Even better though, just get rid of the lines where you don't really need them – pretty sure you don't need to check for the disabled state in the event handlers, if they're not wired up to components in the first place...