to-have-text-content.js 1.0 KB

123456789101112131415161718192021222324
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.toHaveTextContent = toHaveTextContent;
  6. var _utils = require("./utils");
  7. function toHaveTextContent(node, checkWith, options = {
  8. normalizeWhitespace: true
  9. }) {
  10. (0, _utils.checkNode)(node, toHaveTextContent, this);
  11. const textContent = options.normalizeWhitespace ? (0, _utils.normalize)(node.textContent) : node.textContent.replace(/\u00a0/g, ' '); // Replace   with normal spaces
  12. const checkingWithEmptyString = textContent !== '' && checkWith === '';
  13. return {
  14. pass: !checkingWithEmptyString && (0, _utils.matches)(textContent, checkWith),
  15. message: () => {
  16. const to = this.isNot ? 'not to' : 'to';
  17. return (0, _utils.getMessage)(this, this.utils.matcherHint(`${this.isNot ? '.not' : ''}.toHaveTextContent`, 'element', ''), checkingWithEmptyString ? `Checking with empty string will always match, use .toBeEmptyDOMElement() instead` : `Expected element ${to} have text content`, checkWith, 'Received', textContent);
  18. }
  19. };
  20. }