2019-08-12 15:22:58 -04:00
|
|
|
export default {
|
|
|
|
toHaveSpriteIcon: (element, iconName) => {
|
|
|
|
if (!iconName) {
|
|
|
|
throw new Error('toHaveSpriteIcon is missing iconName argument!');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(element instanceof HTMLElement)) {
|
|
|
|
throw new Error(`${element} is not a DOM element!`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const iconReferences = [].slice.apply(element.querySelectorAll('svg use'));
|
2020-08-20 17:10:18 -04:00
|
|
|
const matchingIcon = iconReferences.find(
|
2020-12-23 16:10:24 -05:00
|
|
|
(reference) => reference.parentNode.getAttribute('data-testid') === `${iconName}-icon`,
|
2019-08-12 15:22:58 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
const pass = Boolean(matchingIcon);
|
|
|
|
|
|
|
|
let message;
|
|
|
|
if (pass) {
|
|
|
|
message = `${element.outerHTML} contains the sprite icon "${iconName}"!`;
|
|
|
|
} else {
|
|
|
|
message = `${element.outerHTML} does not contain the sprite icon "${iconName}"!`;
|
|
|
|
|
2020-12-23 16:10:24 -05:00
|
|
|
const existingIcons = iconReferences.map((reference) => {
|
2020-08-20 17:10:18 -04:00
|
|
|
const iconUrl = reference.getAttribute('href');
|
2019-08-12 15:22:58 -04:00
|
|
|
return `"${iconUrl.replace(/^.+#/, '')}"`;
|
|
|
|
});
|
|
|
|
if (existingIcons.length > 0) {
|
|
|
|
message += ` (only found ${existingIcons.join(',')})`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
pass,
|
|
|
|
message: () => message,
|
|
|
|
};
|
|
|
|
},
|
2020-05-21 20:08:07 -04:00
|
|
|
toMatchInterpolatedText(received, match) {
|
|
|
|
let clearReceived;
|
|
|
|
let clearMatch;
|
|
|
|
|
|
|
|
try {
|
2020-12-23 07:10:26 -05:00
|
|
|
clearReceived = received.replace(/\s\s+/gm, ' ').replace(/\s\./gm, '.').trim();
|
2020-05-21 20:08:07 -04:00
|
|
|
} catch (e) {
|
|
|
|
return { actual: received, message: 'The received value is not a string', pass: false };
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
clearMatch = match.replace(/%{\w+}/gm, '').trim();
|
|
|
|
} catch (e) {
|
|
|
|
return { message: 'The comparator value is not a string', pass: false };
|
|
|
|
}
|
|
|
|
const pass = clearReceived === clearMatch;
|
|
|
|
const message = pass
|
|
|
|
? () => `
|
|
|
|
\n\n
|
|
|
|
Expected: ${this.utils.printExpected(clearReceived)}
|
|
|
|
To not equal: ${this.utils.printReceived(clearMatch)}
|
|
|
|
`
|
|
|
|
: () =>
|
|
|
|
`
|
|
|
|
\n\n
|
|
|
|
Expected: ${this.utils.printExpected(clearReceived)}
|
|
|
|
To equal: ${this.utils.printReceived(clearMatch)}
|
|
|
|
`;
|
|
|
|
|
|
|
|
return { actual: received, message, pass };
|
|
|
|
},
|
2019-08-12 15:22:58 -04:00
|
|
|
};
|