Welcome to mirror list, hosted at ThFree Co, Russian Federation.

to_have_tracking_attributes.js « matchers « __helpers__ « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fd3f3aa042f6806e4d2499e42be6be3286777de7 (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
import { diff } from 'jest-diff';
import { isObject, mapValues, isEqual } from 'lodash';

export const toHaveTrackingAttributes = (actual, obj) => {
  if (!(actual instanceof Element)) {
    return { actual, message: () => 'The received value must be an Element.', pass: false };
  }

  if (!isObject(obj)) {
    return {
      message: () => `The matching object must be an object. Found ${obj}.`,
      pass: false,
    };
  }

  const actualAttributes = mapValues(obj, (val, key) => actual.getAttribute(`data-track-${key}`));

  const matcherPass = isEqual(actualAttributes, obj);

  const failMessage = () => {
    // We can match, but still fail because we're in a `expect...not.` context
    if (matcherPass) {
      return `Expected the element's tracking attributes not to match. Found that they matched ${JSON.stringify(
        obj,
      )}.`;
    }

    const objDiff = diff(actualAttributes, obj);
    return `Expected the element's tracking attributes to match the given object. Diff:
${objDiff}
`;
  };

  return { actual, message: failMessage, pass: matcherPass };
};