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