query-helpers.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.getElementError = getElementError;
  6. exports.getMultipleElementsFoundError = getMultipleElementsFoundError;
  7. exports.queryAllByAttribute = queryAllByAttribute;
  8. exports.queryByAttribute = queryByAttribute;
  9. exports.makeSingleQuery = makeSingleQuery;
  10. exports.makeGetAllQuery = makeGetAllQuery;
  11. exports.makeFindQuery = makeFindQuery;
  12. exports.buildQueries = buildQueries;
  13. exports.wrapSingleQueryWithSuggestion = exports.wrapAllByQueryWithSuggestion = void 0;
  14. var _suggestions = require("./suggestions");
  15. var _matches = require("./matches");
  16. var _waitFor = require("./wait-for");
  17. var _config = require("./config");
  18. function getElementError(message, container) {
  19. return (0, _config.getConfig)().getElementError(message, container);
  20. }
  21. function getMultipleElementsFoundError(message, container) {
  22. return getElementError(`${message}\n\n(If this is intentional, then use the \`*AllBy*\` variant of the query (like \`queryAllByText\`, \`getAllByText\`, or \`findAllByText\`)).`, container);
  23. }
  24. function queryAllByAttribute(attribute, container, text, {
  25. exact = true,
  26. collapseWhitespace,
  27. trim,
  28. normalizer
  29. } = {}) {
  30. const matcher = exact ? _matches.matches : _matches.fuzzyMatches;
  31. const matchNormalizer = (0, _matches.makeNormalizer)({
  32. collapseWhitespace,
  33. trim,
  34. normalizer
  35. });
  36. return Array.from(container.querySelectorAll(`[${attribute}]`)).filter(node => matcher(node.getAttribute(attribute), node, text, matchNormalizer));
  37. }
  38. function queryByAttribute(attribute, container, text, ...args) {
  39. const els = queryAllByAttribute(attribute, container, text, ...args);
  40. if (els.length > 1) {
  41. throw getMultipleElementsFoundError(`Found multiple elements by [${attribute}=${text}]`, container);
  42. }
  43. return els[0] || null;
  44. } // this accepts a query function and returns a function which throws an error
  45. // if more than one elements is returned, otherwise it returns the first
  46. // element or null
  47. function makeSingleQuery(allQuery, getMultipleError) {
  48. return (container, ...args) => {
  49. const els = allQuery(container, ...args);
  50. if (els.length > 1) {
  51. const elementStrings = els.map(element => getElementError(null, element).message).join('\n\n');
  52. throw getMultipleElementsFoundError(`${getMultipleError(container, ...args)}
  53. Here are the matching elements:
  54. ${elementStrings}`, container);
  55. }
  56. return els[0] || null;
  57. };
  58. }
  59. function getSuggestionError(suggestion, container) {
  60. return (0, _config.getConfig)().getElementError(`A better query is available, try this:
  61. ${suggestion.toString()}
  62. `, container);
  63. } // this accepts a query function and returns a function which throws an error
  64. // if an empty list of elements is returned
  65. function makeGetAllQuery(allQuery, getMissingError) {
  66. return (container, ...args) => {
  67. const els = allQuery(container, ...args);
  68. if (!els.length) {
  69. throw (0, _config.getConfig)().getElementError(getMissingError(container, ...args), container);
  70. }
  71. return els;
  72. };
  73. } // this accepts a getter query function and returns a function which calls
  74. // waitFor and passing a function which invokes the getter.
  75. function makeFindQuery(getter) {
  76. return (container, text, options, waitForOptions) => {
  77. return (0, _waitFor.waitFor)(() => {
  78. return getter(container, text, options);
  79. }, {
  80. container,
  81. ...waitForOptions
  82. });
  83. };
  84. }
  85. const wrapSingleQueryWithSuggestion = (query, queryAllByName, variant) => (container, ...args) => {
  86. const element = query(container, ...args);
  87. const [{
  88. suggest = (0, _config.getConfig)().throwSuggestions
  89. } = {}] = args.slice(-1);
  90. if (element && suggest) {
  91. const suggestion = (0, _suggestions.getSuggestedQuery)(element, variant);
  92. if (suggestion && !queryAllByName.endsWith(suggestion.queryName)) {
  93. throw getSuggestionError(suggestion.toString(), container);
  94. }
  95. }
  96. return element;
  97. };
  98. exports.wrapSingleQueryWithSuggestion = wrapSingleQueryWithSuggestion;
  99. const wrapAllByQueryWithSuggestion = (query, queryAllByName, variant) => (container, ...args) => {
  100. const els = query(container, ...args);
  101. const [{
  102. suggest = (0, _config.getConfig)().throwSuggestions
  103. } = {}] = args.slice(-1);
  104. if (els.length && suggest) {
  105. // get a unique list of all suggestion messages. We are only going to make a suggestion if
  106. // all the suggestions are the same
  107. const uniqueSuggestionMessages = [...new Set(els.map(element => {
  108. var _getSuggestedQuery;
  109. return (_getSuggestedQuery = (0, _suggestions.getSuggestedQuery)(element, variant)) == null ? void 0 : _getSuggestedQuery.toString();
  110. }))];
  111. if ( // only want to suggest if all the els have the same suggestion.
  112. uniqueSuggestionMessages.length === 1 && !queryAllByName.endsWith((0, _suggestions.getSuggestedQuery)(els[0], variant).queryName)) {
  113. throw getSuggestionError(uniqueSuggestionMessages[0], container);
  114. }
  115. }
  116. return els;
  117. };
  118. exports.wrapAllByQueryWithSuggestion = wrapAllByQueryWithSuggestion;
  119. function buildQueries(queryAllBy, getMultipleError, getMissingError) {
  120. const queryBy = wrapSingleQueryWithSuggestion(makeSingleQuery(queryAllBy, getMultipleError), queryAllBy.name, 'query');
  121. const getAllBy = makeGetAllQuery(queryAllBy, getMissingError);
  122. const getBy = makeSingleQuery(getAllBy, getMultipleError);
  123. const getByWithSuggestions = wrapSingleQueryWithSuggestion(getBy, queryAllBy.name, 'get');
  124. const getAllWithSuggestions = wrapAllByQueryWithSuggestion(getAllBy, queryAllBy.name.replace('query', 'get'), 'getAll');
  125. const findAllBy = makeFindQuery(wrapAllByQueryWithSuggestion(getAllBy, queryAllBy.name, 'findAll'));
  126. const findBy = makeFindQuery(wrapSingleQueryWithSuggestion(getBy, queryAllBy.name, 'find'));
  127. return [queryBy, getAllWithSuggestions, getByWithSuggestions, findAllBy, findBy];
  128. }