text.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.findByText = exports.findAllByText = exports.getAllByText = exports.getByText = exports.queryAllByText = exports.queryByText = void 0;
  6. var _queryHelpers = require("../query-helpers");
  7. var _helpers = require("../helpers");
  8. var _config = require("../config");
  9. var _allUtils = require("./all-utils");
  10. const queryAllByText = (container, text, {
  11. selector = '*',
  12. exact = true,
  13. collapseWhitespace,
  14. trim,
  15. ignore = _config.DEFAULT_IGNORE_TAGS,
  16. normalizer
  17. } = {}) => {
  18. (0, _helpers.checkContainerType)(container);
  19. const matcher = exact ? _allUtils.matches : _allUtils.fuzzyMatches;
  20. const matchNormalizer = (0, _allUtils.makeNormalizer)({
  21. collapseWhitespace,
  22. trim,
  23. normalizer
  24. });
  25. let baseArray = [];
  26. if (typeof container.matches === 'function' && container.matches(selector)) {
  27. baseArray = [container];
  28. }
  29. return [...baseArray, ...Array.from(container.querySelectorAll(selector))] // TODO: `matches` according lib.dom.d.ts can get only `string` but according our code it can handle also boolean :)
  30. .filter(node => !ignore || !node.matches(ignore)).filter(node => matcher((0, _allUtils.getNodeText)(node), node, text, matchNormalizer));
  31. };
  32. const getMultipleError = (c, text) => `Found multiple elements with the text: ${text}`;
  33. const getMissingError = (c, text) => `Unable to find an element with the text: ${text}. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible.`;
  34. const queryAllByTextWithSuggestions = (0, _queryHelpers.wrapAllByQueryWithSuggestion)(queryAllByText, queryAllByText.name, 'queryAll');
  35. exports.queryAllByText = queryAllByTextWithSuggestions;
  36. const [queryByText, getAllByText, getByText, findAllByText, findByText] = (0, _allUtils.buildQueries)(queryAllByText, getMultipleError, getMissingError);
  37. exports.findByText = findByText;
  38. exports.findAllByText = findAllByText;
  39. exports.getByText = getByText;
  40. exports.getAllByText = getAllByText;
  41. exports.queryByText = queryByText;