diff options
Diffstat (limited to 'spec/frontend/__helpers__/matchers/to_have_tracking_attributes.js')
-rw-r--r-- | spec/frontend/__helpers__/matchers/to_have_tracking_attributes.js | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/spec/frontend/__helpers__/matchers/to_have_tracking_attributes.js b/spec/frontend/__helpers__/matchers/to_have_tracking_attributes.js new file mode 100644 index 00000000000..fd3f3aa042f --- /dev/null +++ b/spec/frontend/__helpers__/matchers/to_have_tracking_attributes.js @@ -0,0 +1,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 }; +}; |