@oslokommune/punkt-testing-utils 16.7.3 → 16.7.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,324 +1,256 @@
|
|
|
1
1
|
(function(global, factory) {
|
|
2
|
-
|
|
3
|
-
})(this,
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
useInputEventForNative: false,
|
|
258
|
-
useChangeEventForNative: true,
|
|
259
|
-
setValuePropForNative: false
|
|
260
|
-
}
|
|
261
|
-
);
|
|
262
|
-
if (Array.isArray(valueOrValues) && valueOrValues.length > 1 && element.tagName === "PKT-SELECT") {
|
|
263
|
-
throw new Error("Multi-verdi <pkt-select> støttes ikke. Bruk <pkt-combobox> i stedet.");
|
|
264
|
-
}
|
|
265
|
-
if (element.tagName === "PKT-SELECT" || element.tagName === "PKT-COMBOBOX") {
|
|
266
|
-
const pktSelect = element;
|
|
267
|
-
const multiple = pktSelect.multiple;
|
|
268
|
-
const valueAsArray = Array.isArray(valueOrValues) ? valueOrValues : [valueOrValues];
|
|
269
|
-
const newValue = multiple && valueAsArray || valueAsArray.length == 0 && "" || valueAsArray[0];
|
|
270
|
-
testingLibrary.fireEvent.change(element, { target: { value: newValue } });
|
|
271
|
-
return Promise.resolve(true);
|
|
272
|
-
} else {
|
|
273
|
-
if (options.setValuePropForNative) {
|
|
274
|
-
if ("value" in element) {
|
|
275
|
-
element.value = valueOrValues.toString();
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
if (options.useInputEventForNative) {
|
|
279
|
-
testingLibrary.fireEvent.input(element, { target: { value: valueOrValues } });
|
|
280
|
-
}
|
|
281
|
-
if (options.useChangeEventForNative) {
|
|
282
|
-
testingLibrary.fireEvent.change(element, { target: { value: valueOrValues } });
|
|
283
|
-
}
|
|
284
|
-
return Promise.resolve(true);
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
);
|
|
288
|
-
const containsRadioInput = (element) => {
|
|
289
|
-
var _a;
|
|
290
|
-
if (element.tagName === "INPUT" && element.type === "radio") return true;
|
|
291
|
-
if ((_a = element.querySelector) == null ? void 0 : _a.call(element, 'input[type="radio"]')) return true;
|
|
292
|
-
if (element.tagName === "LABEL") {
|
|
293
|
-
const label = element;
|
|
294
|
-
const control = label.control || (label.htmlFor ? document.getElementById(label.htmlFor) : null);
|
|
295
|
-
return !!(control && control.type === "radio");
|
|
296
|
-
}
|
|
297
|
-
return false;
|
|
298
|
-
};
|
|
299
|
-
const pktClickButton = (testingLibrary) => asyncFuncWithStringIdentifierOrElement(testingLibrary)(async (element) => {
|
|
300
|
-
if (containsRadioInput(element)) {
|
|
301
|
-
throw new Error("Klikk på <pkt-radiobutton> støttes ikke - bruk setPktElementChecked i stedet");
|
|
302
|
-
}
|
|
303
|
-
return testingLibrary.fireEvent.click(element);
|
|
304
|
-
}, "text");
|
|
305
|
-
const setPktSelectedOptionsByLabel = (testingLibrary) => asyncFuncWithStringIdentifierOrElement(testingLibrary)(
|
|
306
|
-
async (pktSelect, ...desiredOptionLabels) => {
|
|
307
|
-
const availableOptions = getPktSelectOptions(testingLibrary)(pktSelect);
|
|
308
|
-
const selectedOptions = desiredOptionLabels.map((optionLabel) => availableOptions.find(([_, label]) => label === optionLabel)).filter((possibleOption) => possibleOption);
|
|
309
|
-
if (selectedOptions.length !== desiredOptionLabels.length) {
|
|
310
|
-
throw new Error(
|
|
311
|
-
"Noen av option'ene finnes ikke i denne komponenten. Du valgte " + JSON.stringify(desiredOptionLabels) + ", mens valgmulighetene er " + JSON.stringify(availableOptions.map(([, label]) => label))
|
|
312
|
-
);
|
|
313
|
-
}
|
|
314
|
-
return await setPktElementValue(testingLibrary)(
|
|
315
|
-
pktSelect,
|
|
316
|
-
selectedOptions.map(([value]) => value)
|
|
317
|
-
);
|
|
318
|
-
}
|
|
319
|
-
);
|
|
320
|
-
exports2.PKT_CUSTOM_FORMFIELDS = PKT_CUSTOM_FORMFIELDS;
|
|
321
|
-
exports2.setupPktTestingLibrary = setupPktTestingLibrary;
|
|
322
|
-
Object.defineProperty(exports2, Symbol.toStringTag, { value: "Module" });
|
|
323
|
-
}));
|
|
324
|
-
//# sourceMappingURL=punkt-testing-utils.umd.js.map
|
|
2
|
+
typeof exports === "object" && typeof module !== "undefined" ? factory(exports, require("@testing-library/dom")) : typeof define === "function" && define.amd ? define(["exports", "@testing-library/dom"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global.punktTestingUtils = {}, global["dom-testing-library"]));
|
|
3
|
+
})(this, function(exports, _testing_library_dom) {
|
|
4
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
5
|
+
//#region src/index.ts
|
|
6
|
+
var PKT_CUSTOM_FORMFIELDS = [
|
|
7
|
+
"pkt-backlink",
|
|
8
|
+
"pkt-checkbox",
|
|
9
|
+
"pkt-combobox",
|
|
10
|
+
"pkt-progressbar",
|
|
11
|
+
"pkt-radiobutton",
|
|
12
|
+
"pkt-select",
|
|
13
|
+
"pkt-textarea",
|
|
14
|
+
"pkt-textinput"
|
|
15
|
+
];
|
|
16
|
+
var PKT_CUSTOM_ELEMENTS = [
|
|
17
|
+
"pkt-input-wrapper",
|
|
18
|
+
"pkt-icon",
|
|
19
|
+
"pkt-helptext",
|
|
20
|
+
"pkt-loader",
|
|
21
|
+
"pkt-backlink",
|
|
22
|
+
...PKT_CUSTOM_FORMFIELDS
|
|
23
|
+
];
|
|
24
|
+
var setupPktTestingLibrary = (options) => {
|
|
25
|
+
const tools = {
|
|
26
|
+
fireEvent: options.fireEvent,
|
|
27
|
+
findAllByLabelText: options.findAllByLabelText,
|
|
28
|
+
findAllByDisplayValue: options.findAllByDisplayValue,
|
|
29
|
+
findAllByTestId: options.findAllByTestId,
|
|
30
|
+
findAllByText: options.findAllByText,
|
|
31
|
+
getAllByLabelText: options.getAllByLabelText,
|
|
32
|
+
getAllByDisplayValue: options.getAllByDisplayValue,
|
|
33
|
+
getAllByTestId: options.getAllByTestId,
|
|
34
|
+
getAllByText: options.getAllByText
|
|
35
|
+
};
|
|
36
|
+
return {
|
|
37
|
+
findPktElementByLabelText: withTestingLibrary(tools, findPktElementByLabel),
|
|
38
|
+
waitForPktElementsToBeDefined,
|
|
39
|
+
getPktElementByLabelText: withTestingLibrary(tools, getPktElementByLabel),
|
|
40
|
+
setPktElementChecked: withTestingLibrary(tools, setPktElementChecked),
|
|
41
|
+
isPktElementChecked: withTestingLibrary(tools, isPktElementChecked),
|
|
42
|
+
getPktSelectOptions: withTestingLibrary(tools, getPktSelectOptions),
|
|
43
|
+
getAllPktElementsByLabelText: withTestingLibrary(tools, getAllPktElementsByLabelText),
|
|
44
|
+
setPktElementValue: withTestingLibrary(tools, setPktElementValue),
|
|
45
|
+
pktClickButton: withTestingLibrary(tools, pktClickButton),
|
|
46
|
+
setPktSelectedOptionsByLabel: withTestingLibrary(tools, setPktSelectedOptionsByLabel),
|
|
47
|
+
getPktElementByDisplayValue: withTestingLibrary(tools, getPktElementByDisplayValue),
|
|
48
|
+
findPktElementByDisplayValue: withTestingLibrary(tools, findPktElementByDisplayValue)
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
var syncQueryFunctionResolver = (testingLibrary) => (queryType) => {
|
|
52
|
+
const query = {
|
|
53
|
+
text: testingLibrary.getAllByText,
|
|
54
|
+
label: testingLibrary.getAllByLabelText,
|
|
55
|
+
testid: testingLibrary.getAllByTestId,
|
|
56
|
+
displayValue: testingLibrary.getAllByDisplayValue
|
|
57
|
+
}[queryType];
|
|
58
|
+
if (query) return query;
|
|
59
|
+
else throw new Error("Unsupported query type: " + queryType);
|
|
60
|
+
};
|
|
61
|
+
var asyncQueryFunctionResolver = (testingLibrary) => (queryType) => {
|
|
62
|
+
const query = {
|
|
63
|
+
text: testingLibrary.findAllByText,
|
|
64
|
+
label: testingLibrary.findAllByLabelText,
|
|
65
|
+
testid: testingLibrary.findAllByTestId,
|
|
66
|
+
displayValue: testingLibrary.findAllByDisplayValue
|
|
67
|
+
}[queryType];
|
|
68
|
+
if (query) return query;
|
|
69
|
+
else throw new Error("Unsupported query type: " + queryType);
|
|
70
|
+
};
|
|
71
|
+
var syncResolveIdentifierOrElement = (testingLibrary) => (elementOrIdentifier, query) => typeof elementOrIdentifier === "string" || elementOrIdentifier instanceof RegExp ? getPktElementBy(testingLibrary)(query, elementOrIdentifier) : elementOrIdentifier;
|
|
72
|
+
var getByLabelElement = (possibleLabelElement) => {
|
|
73
|
+
const control = possibleLabelElement instanceof HTMLLabelElement && possibleLabelElement.control;
|
|
74
|
+
if (control) return control;
|
|
75
|
+
else throw new Error("Fant ikke noe skjema-element for labelen: " + possibleLabelElement.outerHTML);
|
|
76
|
+
};
|
|
77
|
+
var asyncResolveIdentifierOrElement = (testingLibrary) => async (elementOrIdentifier, query) => elementOrIdentifier instanceof HTMLLabelElement && getByLabelElement(elementOrIdentifier) || (typeof elementOrIdentifier === "string" || elementOrIdentifier instanceof RegExp ? await findPktElementBy(testingLibrary)(query, elementOrIdentifier) : elementOrIdentifier);
|
|
78
|
+
var asyncFuncWithStringIdentifierOrElement = (testingLibrary) => (func, query = "label") => {
|
|
79
|
+
return async (labelOrPktElement, ...restArgs) => {
|
|
80
|
+
return await func(await asyncResolveIdentifierOrElement(testingLibrary)(labelOrPktElement, query), ...restArgs);
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
var syncFuncWithStringIdentifierOrElement = (testingLibrary) => (func, query = "label") => {
|
|
84
|
+
return (labelOrPktElement, ...restArgs) => {
|
|
85
|
+
return func(syncResolveIdentifierOrElement(testingLibrary)(labelOrPktElement, query), ...restArgs);
|
|
86
|
+
};
|
|
87
|
+
};
|
|
88
|
+
var withTestingLibrary = (testingLibrary, fn) => fn(testingLibrary);
|
|
89
|
+
/**
|
|
90
|
+
* Sørger for at alle brukte custom elements fra Punkt er definerte og erstattet med implementasjoner.
|
|
91
|
+
* Bør kalles etter `render`, før noen operasjoner og antakelser gjøres.
|
|
92
|
+
*
|
|
93
|
+
* F.eks.:
|
|
94
|
+
* <code>
|
|
95
|
+
* render(<div><PktTextinput id="textinput" name={'textInput'} label="My text input" aria-label="My text input" /></div>);
|
|
96
|
+
* await waitForPktElementsToBeDefined();
|
|
97
|
+
*
|
|
98
|
+
* await setPktElementValue("My text input", "new value");
|
|
99
|
+
* </code>
|
|
100
|
+
*/
|
|
101
|
+
var waitForPktElementsToBeDefined = async () => await Promise.all(PKT_CUSTOM_ELEMENTS.map((elementName) => {
|
|
102
|
+
if (document.querySelector(elementName) !== null) return window.customElements.whenDefined(elementName);
|
|
103
|
+
}).filter((promise) => promise));
|
|
104
|
+
var findPossibleWrappingPktCustomElement = (testingLibrary) => (innerElement, isByRole, args) => {
|
|
105
|
+
const pktElement = innerElement?.closest(PKT_CUSTOM_FORMFIELDS.join(", "));
|
|
106
|
+
if (pktElement) return pktElement;
|
|
107
|
+
else return innerElement;
|
|
108
|
+
};
|
|
109
|
+
var getPktElementBy = (testingLibrary) => (query = "label", identifier, container) => {
|
|
110
|
+
try {
|
|
111
|
+
const innerElement = doElementSearch(identifier, query, syncQueryFunctionResolver(testingLibrary)(query), container)[0];
|
|
112
|
+
return findPossibleWrappingPktCustomElement(testingLibrary)(innerElement, false, []);
|
|
113
|
+
} catch (e) {
|
|
114
|
+
return fallbackSearchForPktSelectByLabel(testingLibrary)(identifier, query, container);
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
var removeElementBySelector = (ancestor, selector) => {
|
|
118
|
+
Array.from(ancestor.querySelectorAll(selector)).forEach((element) => {
|
|
119
|
+
element.parentNode?.removeChild(element);
|
|
120
|
+
});
|
|
121
|
+
};
|
|
122
|
+
function getPureLabelText(label) {
|
|
123
|
+
const clonedLabel = label.cloneNode(true);
|
|
124
|
+
removeElementBySelector(clonedLabel, "pkt-helptext");
|
|
125
|
+
removeElementBySelector(clonedLabel, ".pkt-input-suffix");
|
|
126
|
+
removeElementBySelector(clonedLabel, ".pkt-input-prefix");
|
|
127
|
+
removeElementBySelector(clonedLabel, ".pkt-input-icon");
|
|
128
|
+
removeElementBySelector(clonedLabel, ".pkt-input__counter");
|
|
129
|
+
removeElementBySelector(clonedLabel, "option");
|
|
130
|
+
removeElementBySelector(clonedLabel, "pkt-listbox");
|
|
131
|
+
removeElementBySelector(clonedLabel, ".pkt-alert--error");
|
|
132
|
+
removeElementBySelector(clonedLabel, ".pkt-tag");
|
|
133
|
+
removeElementBySelector(clonedLabel, ".pkt-input-check__input-helptext");
|
|
134
|
+
removeElementBySelector(clonedLabel, ".pkt-inputwrapper__helptext");
|
|
135
|
+
return clonedLabel.textContent?.trim() || null;
|
|
136
|
+
}
|
|
137
|
+
var getPureLabelTextForLabelOwner = (labelOwner) => {
|
|
138
|
+
const label = "labels" in labelOwner && labelOwner.labels instanceof NodeList && labelOwner.labels.length > 0 && labelOwner.labels[0] || ["input", ...PKT_CUSTOM_FORMFIELDS].includes(labelOwner.tagName.toLowerCase()) && labelOwner.querySelector("label") || null;
|
|
139
|
+
if (label) return getPureLabelText(label);
|
|
140
|
+
else return null;
|
|
141
|
+
};
|
|
142
|
+
var labelMatcher = (labelTextToMatch) => (nodeContent, element) => {
|
|
143
|
+
if (element instanceof HTMLElement) return getPureLabelTextForLabelOwner(element) === labelTextToMatch;
|
|
144
|
+
else return false;
|
|
145
|
+
};
|
|
146
|
+
var fallbackSearchForPktSelectByLabel = (testingLibrary) => (identifier, query, container = document.body) => {
|
|
147
|
+
if (typeof identifier === "string" && query === "label") {
|
|
148
|
+
const matchingLabel = Array.from(container.querySelectorAll("label")).find((labelElement) => getPureLabelText(labelElement) === identifier);
|
|
149
|
+
const labelOwner = matchingLabel?.control || matchingLabel?.closest("pkt-select") || matchingLabel?.closest("pkt-combobox");
|
|
150
|
+
if (!labelOwner) throw (0, _testing_library_dom.getElementError)(`Fant ikke noe element med label "${identifier}"`, container);
|
|
151
|
+
return labelOwner;
|
|
152
|
+
}
|
|
153
|
+
throw (0, _testing_library_dom.getElementError)(`Fant ikke noe element med ${query} "${identifier}"`, container);
|
|
154
|
+
};
|
|
155
|
+
var doElementSearch = (identifier, query, queryFunc, container = document.body) => {
|
|
156
|
+
return identifier instanceof HTMLLabelElement ? getByLabelElement(identifier) : typeof identifier === "string" && query === "label" ? queryFunc(container, labelMatcher(identifier.trim())) : queryFunc(container, identifier);
|
|
157
|
+
};
|
|
158
|
+
var findPktElementBy = (testingLibrary) => async (query = "label", identifier, container) => {
|
|
159
|
+
try {
|
|
160
|
+
if (identifier instanceof HTMLLabelElement) return Promise.resolve(getByLabelElement(identifier));
|
|
161
|
+
const innerElement = (await doElementSearch(identifier, query, asyncQueryFunctionResolver(testingLibrary)(query), container))[0];
|
|
162
|
+
return Promise.resolve(findPossibleWrappingPktCustomElement(testingLibrary)(innerElement, false, []));
|
|
163
|
+
} catch (e) {
|
|
164
|
+
return fallbackSearchForPktSelectByLabel(testingLibrary)(identifier, query, container);
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
var getPktElementByLabel = (testingLibrary) => (label, container) => label instanceof HTMLLabelElement ? getByLabelElement(label) : getPktElementBy(testingLibrary)("label", label, container);
|
|
168
|
+
var findPktElementByLabel = (testingLibrary) => async (label, container) => label instanceof HTMLLabelElement ? getByLabelElement(label) : findPktElementBy(testingLibrary)("label", label, container);
|
|
169
|
+
var setPktElementChecked = (testingLibrary) => asyncFuncWithStringIdentifierOrElement(testingLibrary)(async (element, checked) => {
|
|
170
|
+
if (!("checked" in element)) throw new Error("Bare elementer som har en \"checked\"-attributt støttes");
|
|
171
|
+
let returnValue = false;
|
|
172
|
+
if (element.tagName === "INPUT") {
|
|
173
|
+
const htmlInputElement = element;
|
|
174
|
+
if (htmlInputElement.type === "radio") if (!checked) throw new Error("Kan ikke av-velge en <input type='radio'> - prøv å velge en annen radioknapp i samme gruppe");
|
|
175
|
+
else {
|
|
176
|
+
returnValue = testingLibrary.fireEvent.click(element);
|
|
177
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
178
|
+
}
|
|
179
|
+
else if (htmlInputElement.type === "checkbox") {
|
|
180
|
+
if (htmlInputElement.checked !== checked) returnValue = testingLibrary.fireEvent.click(element);
|
|
181
|
+
}
|
|
182
|
+
} else returnValue = testingLibrary.fireEvent.change(element, { target: { checked } });
|
|
183
|
+
return Promise.resolve(returnValue);
|
|
184
|
+
});
|
|
185
|
+
var isPktElementChecked = (testingLibrary) => (target) => {
|
|
186
|
+
const element = syncResolveIdentifierOrElement(testingLibrary)(target, "label");
|
|
187
|
+
if ("checked" in element) return !!element.checked;
|
|
188
|
+
return element.querySelector("input[type=\"radio\"]:checked, input[type=\"checkbox\"]:checked") !== null;
|
|
189
|
+
};
|
|
190
|
+
var getPktElementByDisplayValue = (testingLibrary) => (text, container) => getPktElementBy(testingLibrary)("displayValue", text, container);
|
|
191
|
+
var findPktElementByDisplayValue = (testingLibrary) => (text, container) => findPktElementBy(testingLibrary)("displayValue", text, container);
|
|
192
|
+
var getPktSelectOptions = (testingLibrary) => syncFuncWithStringIdentifierOrElement(testingLibrary)((selectElement, onlySelected) => {
|
|
193
|
+
const optionElements = Array.from(selectElement.querySelectorAll("option:not(.pkt-hide), data:not(.pkt.hide)"));
|
|
194
|
+
const filter = onlySelected ? ([, , selected]) => selected : (_) => true;
|
|
195
|
+
const currentValue = selectElement.value;
|
|
196
|
+
return optionElements.map((optionElement) => [
|
|
197
|
+
optionElement.value,
|
|
198
|
+
optionElement.textContent?.trim(),
|
|
199
|
+
optionElement.value === currentValue
|
|
200
|
+
]).filter(filter);
|
|
201
|
+
});
|
|
202
|
+
var getAllPktElementsByLabelText = (testingLibrary) => (label, container) => {
|
|
203
|
+
return testingLibrary.getAllByLabelText(container || document.body, label).map((element) => findPossibleWrappingPktCustomElement(testingLibrary)(element, false, []));
|
|
204
|
+
};
|
|
205
|
+
var setPktElementValue = (testingLibrary) => asyncFuncWithStringIdentifierOrElement(testingLibrary)(async (element, valueOrValues, optionsOrUseInputEvent = false) => {
|
|
206
|
+
const options = typeof optionsOrUseInputEvent === "object" ? optionsOrUseInputEvent : optionsOrUseInputEvent && {
|
|
207
|
+
useInputEventForNative: true,
|
|
208
|
+
useChangeEventForNative: true,
|
|
209
|
+
setValuePropForNative: true
|
|
210
|
+
} || {
|
|
211
|
+
useInputEventForNative: false,
|
|
212
|
+
useChangeEventForNative: true,
|
|
213
|
+
setValuePropForNative: false
|
|
214
|
+
};
|
|
215
|
+
if (Array.isArray(valueOrValues) && valueOrValues.length > 1 && element.tagName === "PKT-SELECT") throw new Error("Multi-verdi <pkt-select> støttes ikke. Bruk <pkt-combobox> i stedet.");
|
|
216
|
+
if (element.tagName === "PKT-SELECT" || element.tagName === "PKT-COMBOBOX") {
|
|
217
|
+
const multiple = element.multiple;
|
|
218
|
+
const valueAsArray = Array.isArray(valueOrValues) ? valueOrValues : [valueOrValues];
|
|
219
|
+
const newValue = multiple && valueAsArray || valueAsArray.length == 0 && "" || valueAsArray[0];
|
|
220
|
+
testingLibrary.fireEvent.change(element, { target: { value: newValue } });
|
|
221
|
+
return Promise.resolve(true);
|
|
222
|
+
} else {
|
|
223
|
+
if (options.setValuePropForNative) {
|
|
224
|
+
if ("value" in element) element.value = valueOrValues.toString();
|
|
225
|
+
}
|
|
226
|
+
if (options.useInputEventForNative) testingLibrary.fireEvent.input(element, { target: { value: valueOrValues } });
|
|
227
|
+
if (options.useChangeEventForNative) testingLibrary.fireEvent.change(element, { target: { value: valueOrValues } });
|
|
228
|
+
return Promise.resolve(true);
|
|
229
|
+
}
|
|
230
|
+
});
|
|
231
|
+
var containsRadioInput = (element) => {
|
|
232
|
+
if (element.tagName === "INPUT" && element.type === "radio") return true;
|
|
233
|
+
if (element.querySelector?.("input[type=\"radio\"]")) return true;
|
|
234
|
+
if (element.tagName === "LABEL") {
|
|
235
|
+
const label = element;
|
|
236
|
+
const control = label.control || (label.htmlFor ? document.getElementById(label.htmlFor) : null);
|
|
237
|
+
return !!(control && control.type === "radio");
|
|
238
|
+
}
|
|
239
|
+
return false;
|
|
240
|
+
};
|
|
241
|
+
var pktClickButton = (testingLibrary) => asyncFuncWithStringIdentifierOrElement(testingLibrary)(async (element) => {
|
|
242
|
+
if (containsRadioInput(element)) throw new Error("Klikk på <pkt-radiobutton> støttes ikke - bruk setPktElementChecked i stedet");
|
|
243
|
+
return testingLibrary.fireEvent.click(element);
|
|
244
|
+
}, "text");
|
|
245
|
+
var setPktSelectedOptionsByLabel = (testingLibrary) => asyncFuncWithStringIdentifierOrElement(testingLibrary)(async (pktSelect, ...desiredOptionLabels) => {
|
|
246
|
+
const availableOptions = getPktSelectOptions(testingLibrary)(pktSelect);
|
|
247
|
+
const selectedOptions = desiredOptionLabels.map((optionLabel) => availableOptions.find(([_, label]) => label === optionLabel)).filter((possibleOption) => possibleOption);
|
|
248
|
+
if (selectedOptions.length !== desiredOptionLabels.length) throw new Error("Noen av option'ene finnes ikke i denne komponenten. Du valgte " + JSON.stringify(desiredOptionLabels) + ", mens valgmulighetene er " + JSON.stringify(availableOptions.map(([, label]) => label)));
|
|
249
|
+
return await setPktElementValue(testingLibrary)(pktSelect, selectedOptions.map(([value]) => value));
|
|
250
|
+
});
|
|
251
|
+
//#endregion
|
|
252
|
+
exports.PKT_CUSTOM_FORMFIELDS = PKT_CUSTOM_FORMFIELDS;
|
|
253
|
+
exports.setupPktTestingLibrary = setupPktTestingLibrary;
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
//# sourceMappingURL=punkt-testing-utils.umd.js.map
|