All files / src jest-extensions.js

100% Statements 21/21
100% Branches 10/10
100% Functions 6/6
100% Lines 21/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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        5x 2x   3x       1x 2x     1x   4x 4x 2x   1x                 2x   1x                       5x 1x           4x 4x 4x 2x   1x                 2x   1x                          
import {matcherHint, printReceived, printExpected} from 'jest-matcher-utils' //eslint-disable-line import/no-extraneous-dependencies
import {matches} from './utils'
 
function getDisplayName(subject) {
  if (subject && subject.constructor) {
    return subject.constructor.name
  } else {
    return typeof subject
  }
}
 
const assertMessage = (assertionName, message, received, expected) =>
  `${matcherHint(`${assertionName}`, 'received', '')} \n${message}: ` +
  `${printExpected(expected)} \nReceived: ${printReceived(received)}`
 
const extensions = {
  toBeInTheDOM(received) {
    getDisplayName(received)
    if (received) {
      return {
        message: () =>
          `${matcherHint(
            '.not.toBeInTheDOM',
            'received',
            '',
          )} Expected the element not to be present` +
          `\nReceived : ${printReceived(received)}`,
        pass: true,
      }
    } else {
      return {
        message: () =>
          `${matcherHint(
            '.toBeInTheDOM',
            'received',
            '',
          )} Expected the element to be present` +
          `\nReceived : ${printReceived(received)}`,
        pass: false,
      }
    }
  },
 
  toHaveTextContent(htmlElement, checkWith) {
    if (!(htmlElement instanceof HTMLElement))
      throw new Error(
        `The given subject is a ${getDisplayName(
          htmlElement,
        )}, not an HTMLElement`,
      )
 
    const textContent = htmlElement.textContent
    const pass = matches(textContent, htmlElement, checkWith)
    if (pass) {
      return {
        message: () =>
          assertMessage(
            '.not.toHaveTextContent',
            'Expected value not equals to',
            htmlElement,
            checkWith,
          ),
        pass: true,
      }
    } else {
      return {
        message: () =>
          assertMessage(
            '.toHaveTextContent',
            'Expected value equals to',
            htmlElement,
            checkWith,
          ),
        pass: false,
      }
    }
  },
}
 
export default extensions