summaryrefslogtreecommitdiff
path: root/spec/frontend/__helpers__/matchers/to_match_interpolated_text.js
blob: 41e69bffd885218a921976bc0adcc90a91b42649 (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
// Custom matchers are object methods and should be traditional functions to be able to access `utils` on `this`
export function toMatchInterpolatedText(received, match) {
  let clearReceived;
  let clearMatch;

  try {
    clearReceived = received.replace(/\s\s+/gm, ' ').replace(/\s\./gm, '.').trim();
  } catch (e) {
    return { actual: received, message: 'The received value is not a string', pass: false };
  }
  try {
    clearMatch = match.replace(/%{\w+}/gm, '').trim();
  } catch (e) {
    return { message: 'The comparator value is not a string', pass: false };
  }
  const pass = clearReceived === clearMatch;
  const message = pass
    ? () => `
        Expected to not be: ${this.utils.printExpected(clearMatch)}
        Received:           ${this.utils.printReceived(clearReceived)}
        `
    : () =>
        `
      Expected to be: ${this.utils.printExpected(clearMatch)}
      Received:       ${this.utils.printReceived(clearReceived)}
      `;

  return { actual: received, message, pass };
}