summaryrefslogtreecommitdiff
path: root/spec/javascripts/matchers.js
blob: 7d1921cabcfde843087e8177d1d4e75f125a9ea0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import pixelmatch from 'pixelmatch';

export default {
  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;
    },
  }),
  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'));
      const matchingIcon = iconReferences.find(reference =>
        reference.getAttribute('xlink:href').endsWith(`#${iconName}`),
      );
      const result = {
        pass: Boolean(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}"!`;

        const existingIcons = iconReferences.map(reference => {
          const iconUrl = reference.getAttribute('xlink:href');
          return `"${iconUrl.replace(/^.+#/, '')}"`;
        });
        if (existingIcons.length > 0) {
          result.message += ` (only found ${existingIcons.join(',')})`;
        }
      }

      return result;
    },
  }),
  toRender: () => ({
    compare(vm) {
      const result = {
        pass: vm.$el.nodeType !== Node.COMMENT_NODE,
      };
      return result;
    },
  }),
  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.`,
        };
      },
    };
  },
};