gitlab-org--gitlab-foss/spec/javascripts/matchers.js

58 lines
1.6 KiB
JavaScript
Raw Normal View History

2018-04-08 06:20:05 -04:00
export default {
2018-06-21 08:22:40 -04:00
toContainText: () => ({
compare(vm, text) {
if (!(vm.$el instanceof HTMLElement)) {
throw new Error('vm.$el is not a DOM element!');
}
const result = {
pass: vm.$el.innerText.includes(text),
};
return result;
},
}),
2018-04-08 06:20:05 -04:00
toHaveSpriteIcon: () => ({
compare(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'));
2018-06-21 08:22:40 -04:00
const matchingIcon = iconReferences.find(reference =>
reference.getAttribute('xlink:href').endsWith(`#${iconName}`),
);
2018-04-08 06:20:05 -04:00
const result = {
pass: !!matchingIcon,
};
if (result.pass) {
result.message = `${element.outerHTML} contains the sprite icon "${iconName}"!`;
} else {
result.message = `${element.outerHTML} does not contain the sprite icon "${iconName}"!`;
2018-06-21 08:22:40 -04:00
const existingIcons = iconReferences.map(reference => {
2018-04-08 06:20:05 -04:00
const iconUrl = reference.getAttribute('xlink:href');
return `"${iconUrl.replace(/^.+#/, '')}"`;
});
if (existingIcons.length > 0) {
result.message += ` (only found ${existingIcons.join(',')})`;
}
}
return result;
},
}),
2018-06-21 08:22:40 -04:00
toRender: () => ({
compare(vm) {
const result = {
pass: vm.$el.nodeType !== Node.COMMENT_NODE,
};
return result;
},
}),
2018-04-08 06:20:05 -04:00
};