text.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.queryByText = exports.queryAllByText = exports.getByText = exports.getAllByText = exports.findByText = exports.findAllByText = void 0;
  6. var _queryHelpers = require("../query-helpers");
  7. var _helpers = require("../helpers");
  8. var _allUtils = require("./all-utils");
  9. const queryAllByText = (container, text, {
  10. selector = '*',
  11. exact = true,
  12. collapseWhitespace,
  13. trim,
  14. ignore = (0, _allUtils.getConfig)().defaultIgnore,
  15. normalizer
  16. } = {}) => {
  17. (0, _helpers.checkContainerType)(container);
  18. const matcher = exact ? _allUtils.matches : _allUtils.fuzzyMatches;
  19. const matchNormalizer = (0, _allUtils.makeNormalizer)({
  20. collapseWhitespace,
  21. trim,
  22. normalizer
  23. });
  24. let baseArray = [];
  25. if (typeof container.matches === 'function' && container.matches(selector)) {
  26. baseArray = [container];
  27. }
  28. return [...baseArray, ...Array.from(container.querySelectorAll(selector))]
  29. // 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, options = {}) => {
  34. const {
  35. collapseWhitespace,
  36. trim,
  37. normalizer,
  38. selector
  39. } = options;
  40. const matchNormalizer = (0, _allUtils.makeNormalizer)({
  41. collapseWhitespace,
  42. trim,
  43. normalizer
  44. });
  45. const normalizedText = matchNormalizer(text.toString());
  46. const isNormalizedDifferent = normalizedText !== text.toString();
  47. const isCustomSelector = (selector != null ? selector : '*') !== '*';
  48. return `Unable to find an element with the text: ${isNormalizedDifferent ? `${normalizedText} (normalized from '${text}')` : text}${isCustomSelector ? `, which matches selector '${selector}'` : ''}. 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.`;
  49. };
  50. const queryAllByTextWithSuggestions = (0, _queryHelpers.wrapAllByQueryWithSuggestion)(queryAllByText, queryAllByText.name, 'queryAll');
  51. exports.queryAllByText = queryAllByTextWithSuggestions;
  52. const [queryByText, getAllByText, getByText, findAllByText, findByText] = (0, _allUtils.buildQueries)(queryAllByText, getMultipleError, getMissingError);
  53. exports.findByText = findByText;
  54. exports.findAllByText = findAllByText;
  55. exports.getByText = getByText;
  56. exports.getAllByText = getAllByText;
  57. exports.queryByText = queryByText;