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 toToggleElement( toggleLabel, selector ) {
|
|
|
|
if ( ! selector ) {
|
|
|
|
return {
|
|
|
|
message: () =>
|
|
|
|
`a selector is required to test element's presence`,
|
|
|
|
pass: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const hasSelectorMatch = async () => !! ( await page.$( selector ) );
|
|
|
|
const initiallyHadSelectorMatch = await hasSelectorMatch();
|
|
|
|
const noChangeError = {
|
|
|
|
message: () =>
|
|
|
|
`element presence did not change after clicking the toggle`,
|
|
|
|
pass: false,
|
|
|
|
};
|
|
|
|
|
2021-09-16 12:16:21 +00:00
|
|
|
await ( typeof toggleLabel === 'string'
|
|
|
|
? await findLabelWithText( toggleLabel )
|
|
|
|
: toggleLabel
|
|
|
|
).click();
|
2020-09-14 07:46:58 +00:00
|
|
|
|
|
|
|
if ( initiallyHadSelectorMatch === ( await hasSelectorMatch() ) ) {
|
|
|
|
return noChangeError;
|
|
|
|
}
|
|
|
|
|
2021-09-16 12:16:21 +00:00
|
|
|
await ( typeof toggleLabel === 'string'
|
|
|
|
? await findLabelWithText( toggleLabel )
|
|
|
|
: toggleLabel
|
|
|
|
).click();
|
2020-09-14 07:46:58 +00:00
|
|
|
|
|
|
|
if ( initiallyHadSelectorMatch !== ( await hasSelectorMatch() ) ) {
|
|
|
|
return noChangeError;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
message: () => `element presence reacted to toggle changes.`,
|
|
|
|
pass: true,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
} );
|