2021-09-16 12:16:21 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { findLabelWithText } from '../../../utils';
|
|
|
|
|
2020-09-14 07:46:58 +00:00
|
|
|
expect.extend( {
|
|
|
|
async toToggleRequiredAttrOf( checkboxLabel, selector ) {
|
|
|
|
if ( ! selector ) {
|
|
|
|
return {
|
|
|
|
message: () =>
|
|
|
|
`a selector is required to test element's required attribute`,
|
|
|
|
pass: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const isRequired = async () =>
|
|
|
|
!! ( await page.$eval( selector, ( e ) => e.required ) );
|
|
|
|
const wasInitiallyRequired = await isRequired();
|
|
|
|
const noChangeError = {
|
|
|
|
message: () =>
|
|
|
|
`input's (${ selector }) required attribute did not change after clicking the checkbox`,
|
|
|
|
pass: false,
|
|
|
|
};
|
|
|
|
|
2021-09-16 12:16:21 +00:00
|
|
|
await ( typeof checkboxLabel === 'string'
|
|
|
|
? await findLabelWithText( checkboxLabel )
|
|
|
|
: checkboxLabel
|
|
|
|
).click();
|
2020-09-14 07:46:58 +00:00
|
|
|
|
|
|
|
if ( wasInitiallyRequired === ( await isRequired() ) ) {
|
|
|
|
return noChangeError;
|
|
|
|
}
|
|
|
|
|
2021-09-16 12:16:21 +00:00
|
|
|
await ( typeof checkboxLabel === 'string'
|
|
|
|
? await findLabelWithText( checkboxLabel )
|
|
|
|
: checkboxLabel
|
|
|
|
).click();
|
2020-09-14 07:46:58 +00:00
|
|
|
|
|
|
|
if ( wasInitiallyRequired !== ( await isRequired() ) ) {
|
|
|
|
return noChangeError;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
message: () =>
|
|
|
|
`input's (${ selector }) required attribute reacted to checkbox changes.`,
|
|
|
|
pass: true,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
} );
|