display-value.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.findByDisplayValue = exports.findAllByDisplayValue = exports.getAllByDisplayValue = exports.getByDisplayValue = exports.queryAllByDisplayValue = exports.queryByDisplayValue = void 0;
  6. var _queryHelpers = require("../query-helpers");
  7. var _helpers = require("../helpers");
  8. var _allUtils = require("./all-utils");
  9. const queryAllByDisplayValue = (container, value, {
  10. exact = true,
  11. collapseWhitespace,
  12. trim,
  13. normalizer
  14. } = {}) => {
  15. (0, _helpers.checkContainerType)(container);
  16. const matcher = exact ? _allUtils.matches : _allUtils.fuzzyMatches;
  17. const matchNormalizer = (0, _allUtils.makeNormalizer)({
  18. collapseWhitespace,
  19. trim,
  20. normalizer
  21. });
  22. return Array.from(container.querySelectorAll(`input,textarea,select`)).filter(node => {
  23. if (node.tagName === 'SELECT') {
  24. const selectedOptions = Array.from(node.options).filter(option => option.selected);
  25. return selectedOptions.some(optionNode => matcher((0, _allUtils.getNodeText)(optionNode), optionNode, value, matchNormalizer));
  26. } else {
  27. return matcher(node.value, node, value, matchNormalizer);
  28. }
  29. });
  30. };
  31. const getMultipleError = (c, value) => `Found multiple elements with the display value: ${value}.`;
  32. const getMissingError = (c, value) => `Unable to find an element with the display value: ${value}.`;
  33. const queryAllByDisplayValueWithSuggestions = (0, _queryHelpers.wrapAllByQueryWithSuggestion)(queryAllByDisplayValue, queryAllByDisplayValue.name, 'queryAll');
  34. exports.queryAllByDisplayValue = queryAllByDisplayValueWithSuggestions;
  35. const [queryByDisplayValue, getAllByDisplayValue, getByDisplayValue, findAllByDisplayValue, findByDisplayValue] = (0, _allUtils.buildQueries)(queryAllByDisplayValue, getMultipleError, getMissingError);
  36. exports.findByDisplayValue = findByDisplayValue;
  37. exports.findAllByDisplayValue = findAllByDisplayValue;
  38. exports.getByDisplayValue = getByDisplayValue;
  39. exports.getAllByDisplayValue = getAllByDisplayValue;
  40. exports.queryByDisplayValue = queryByDisplayValue;