debian-mirror-gitlab/spec/javascripts/matchers.js

97 lines
2.8 KiB
JavaScript
Raw Normal View History

2019-03-02 22:35:43 +05:30
import pixelmatch from 'pixelmatch';
2018-05-09 12:01:36 +05:30
export default {
2018-11-08 19:23:39 +05:30
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-05-09 12:01:36 +05:30
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-11-08 19:23:39 +05:30
const matchingIcon = iconReferences.find(reference =>
reference.getAttribute('xlink:href').endsWith(`#${iconName}`),
);
2018-05-09 12:01:36 +05:30
const result = {
2019-09-04 21:01:54 +05:30
pass: Boolean(matchingIcon),
2018-05-09 12:01:36 +05:30
};
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-11-08 19:23:39 +05:30
const existingIcons = iconReferences.map(reference => {
2018-05-09 12:01:36 +05:30
const iconUrl = reference.getAttribute('xlink:href');
return `"${iconUrl.replace(/^.+#/, '')}"`;
});
if (existingIcons.length > 0) {
result.message += ` (only found ${existingIcons.join(',')})`;
}
}
return result;
},
}),
2018-11-08 19:23:39 +05:30
toRender: () => ({
compare(vm) {
const result = {
pass: vm.$el.nodeType !== Node.COMMENT_NODE,
};
return result;
},
}),
2019-03-02 22:35:43 +05:30
toImageDiffEqual: () => {
const getImageData = img => {
const canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
canvas.getContext('2d').drawImage(img, 0, 0);
return canvas.getContext('2d').getImageData(0, 0, img.width, img.height).data;
};
return {
compare(actual, expected, threshold = 0.1) {
if (actual.height !== expected.height || actual.width !== expected.width) {
return {
pass: false,
message: `Expected image dimensions (h x w) of ${expected.height}x${expected.width}.
Received an image with ${actual.height}x${actual.width}`,
};
}
const { width, height } = actual;
const differentPixels = pixelmatch(
getImageData(actual),
getImageData(expected),
null,
width,
height,
{ threshold },
);
return {
pass: differentPixels < 20,
message: `${differentPixels} pixels differ more than ${threshold *
100} percent between input and output.`,
};
},
};
},
2018-05-09 12:01:36 +05:30
};