summaryrefslogtreecommitdiff
path: root/spec/frontend/matchers.js
diff options
context:
space:
mode:
authorNathan Friend <nathan@gitlab.com>2019-08-12 15:22:58 -0400
committerNathan Friend <nathan@gitlab.com>2019-08-12 15:22:58 -0400
commitda306cdb0f3125d0b9682d11c2ec007cdfd1746f (patch)
tree8873c93631737d1989703c9d52b1de34ab133446 /spec/frontend/matchers.js
parent22f3142b83fb08589eb1212cc378996975b2157f (diff)
downloadgitlab-ce-da306cdb0f3125d0b9682d11c2ec007cdfd1746f.tar.gz
Convert spec/javascripts/environments/*rollback* tests to Jest61800-migrate-environment-rollback-tests-to-jest
This commit converts two Jasmine tests into Jest tests.
Diffstat (limited to 'spec/frontend/matchers.js')
-rw-r--r--spec/frontend/matchers.js38
1 files changed, 38 insertions, 0 deletions
diff --git a/spec/frontend/matchers.js b/spec/frontend/matchers.js
new file mode 100644
index 00000000000..35c362d0bf5
--- /dev/null
+++ b/spec/frontend/matchers.js
@@ -0,0 +1,38 @@
+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'));
+ const matchingIcon = iconReferences.find(reference =>
+ reference.getAttribute('xlink:href').endsWith(`#${iconName}`),
+ );
+
+ 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}"!`;
+
+ const existingIcons = iconReferences.map(reference => {
+ const iconUrl = reference.getAttribute('xlink:href');
+ return `"${iconUrl.replace(/^.+#/, '')}"`;
+ });
+ if (existingIcons.length > 0) {
+ message += ` (only found ${existingIcons.join(',')})`;
+ }
+ }
+
+ return {
+ pass,
+ message: () => message,
+ };
+ },
+};