@testspectra/react 1.3.3 → 1.3.5
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.
- package/dist/index.js +44 -8
- package/dist/setup.js +33 -33
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -208,8 +208,9 @@ function createMockHandle(rule, release) {
|
|
|
208
208
|
respondWith: async (newFixture, newOptions = {}) => {
|
|
209
209
|
const newObj = newFixture;
|
|
210
210
|
rule.response = newObj && newObj.body !== void 0 ? newObj.body : newFixture;
|
|
211
|
-
|
|
212
|
-
|
|
211
|
+
const envelopeStatus = readMockEnvelope(newObj)?.statusCode;
|
|
212
|
+
if (newOptions.statusCode || newObj && newObj.statusCode || envelopeStatus) {
|
|
213
|
+
rule.statusCode = newOptions.statusCode || newObj?.statusCode || envelopeStatus || 200;
|
|
213
214
|
}
|
|
214
215
|
if (newOptions.headers || newObj && newObj.headers) {
|
|
215
216
|
rule.headers = newOptions.headers || newObj?.headers;
|
|
@@ -222,7 +223,7 @@ function createMockHandle(rule, release) {
|
|
|
222
223
|
respondOnce: async (newFixture, newOptions = {}) => {
|
|
223
224
|
const newObj = newFixture;
|
|
224
225
|
const resp = newObj && newObj.body !== void 0 ? newObj.body : newFixture;
|
|
225
|
-
const statusCode = newOptions.statusCode || newObj &&
|
|
226
|
+
const statusCode = newOptions.statusCode || newObj && newObj.statusCode || readMockEnvelope(newObj)?.statusCode || rule.statusCode || 200;
|
|
226
227
|
const headers = newOptions.headers || newObj && newObj.headers || rule.headers;
|
|
227
228
|
const delayMs = newOptions.delayMs ?? (newObj && newObj.delayMs) ?? rule.delayMs;
|
|
228
229
|
rule.respondOnceQueue.push({ response: resp, statusCode, headers, delayMs });
|
|
@@ -349,6 +350,28 @@ function recordMockCall(rule, request) {
|
|
|
349
350
|
}
|
|
350
351
|
return request;
|
|
351
352
|
}
|
|
353
|
+
var ENVELOPE_KEYS = /* @__PURE__ */ new Set(["status", "statusCode", "headers", "body", "delayMs"]);
|
|
354
|
+
function readMockEnvelope(value) {
|
|
355
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return null;
|
|
356
|
+
const record = value;
|
|
357
|
+
const keys = Object.keys(record);
|
|
358
|
+
if (keys.length === 0 || !keys.every((key) => ENVELOPE_KEYS.has(key))) return null;
|
|
359
|
+
const status = typeof record.status === "number" ? record.status : record.statusCode;
|
|
360
|
+
if (!("body" in record) && typeof status !== "number") return null;
|
|
361
|
+
return { body: record.body ?? "", statusCode: status, headers: record.headers, delayMs: record.delayMs };
|
|
362
|
+
}
|
|
363
|
+
async function resolveMockResponse(rule, request) {
|
|
364
|
+
const consumed = consumeMockResponse(rule);
|
|
365
|
+
const value = typeof consumed.body === "function" ? await consumed.body(request) : consumed.body;
|
|
366
|
+
const envelope = readMockEnvelope(value);
|
|
367
|
+
if (!envelope) return { ...consumed, body: value };
|
|
368
|
+
return {
|
|
369
|
+
body: envelope.body,
|
|
370
|
+
statusCode: envelope.statusCode ?? consumed.statusCode,
|
|
371
|
+
headers: { ...consumed.headers, ...envelope.headers },
|
|
372
|
+
delayMs: envelope.delayMs ?? consumed.delayMs
|
|
373
|
+
};
|
|
374
|
+
}
|
|
352
375
|
function consumeMockResponse(rule) {
|
|
353
376
|
if (rule.respondOnceQueue.length > 0) {
|
|
354
377
|
const queued = rule.respondOnceQueue.shift();
|
|
@@ -509,7 +532,7 @@ async function patchedFetch(originalFetch, input, init) {
|
|
|
509
532
|
if (rule.aborted) {
|
|
510
533
|
throw new TypeError(`Spectra.intercept() aborted ${method} ${url} (${rule.abortReason || "Failed"})`);
|
|
511
534
|
}
|
|
512
|
-
const consumed =
|
|
535
|
+
const consumed = await resolveMockResponse(rule, intercepted);
|
|
513
536
|
if (consumed.delayMs && consumed.delayMs > 0) await sleep(consumed.delayMs);
|
|
514
537
|
return buildMockResponse(consumed);
|
|
515
538
|
}
|
|
@@ -545,8 +568,7 @@ function patchXMLHttpRequest() {
|
|
|
545
568
|
setTimeout(fireError, 0);
|
|
546
569
|
return;
|
|
547
570
|
}
|
|
548
|
-
const
|
|
549
|
-
const respond2 = () => {
|
|
571
|
+
const respond2 = (consumed) => {
|
|
550
572
|
const bodyText = mockResponseBodyText(consumed.body);
|
|
551
573
|
Object.defineProperties(xhr, {
|
|
552
574
|
status: { value: consumed.statusCode, configurable: true },
|
|
@@ -562,8 +584,9 @@ function patchXMLHttpRequest() {
|
|
|
562
584
|
xhr.dispatchEvent(new Event("load"));
|
|
563
585
|
xhr.dispatchEvent(new Event("loadend"));
|
|
564
586
|
};
|
|
565
|
-
|
|
566
|
-
|
|
587
|
+
void resolveMockResponse(rule, intercepted).then((consumed) => {
|
|
588
|
+
setTimeout(() => respond2(consumed), consumed.delayMs && consumed.delayMs > 0 ? consumed.delayMs : 0);
|
|
589
|
+
});
|
|
567
590
|
};
|
|
568
591
|
return xhr;
|
|
569
592
|
}
|
|
@@ -763,6 +786,19 @@ var DomDriverBridge = class {
|
|
|
763
786
|
}
|
|
764
787
|
}
|
|
765
788
|
}
|
|
789
|
+
/** Types `value` at the end of the field's current text (`type()` without `clearFirst`). */
|
|
790
|
+
async typeText(selector, value, index = null) {
|
|
791
|
+
const el = await this.waitForScopedEl(selector, index);
|
|
792
|
+
if (!isFormEl(el)) throw new Error("Element is not a form field: " + selectorText(selector));
|
|
793
|
+
const text = String(value ?? "");
|
|
794
|
+
if (text.length === 0) return;
|
|
795
|
+
const before = readTypedValue(el);
|
|
796
|
+
await getUser().type(el, text);
|
|
797
|
+
const landed = readTypedValue(el);
|
|
798
|
+
if (!typedTextLanded((before ?? "") + text, landed, before)) {
|
|
799
|
+
throw typedTextError(selectorText(selector), text, landed ?? "");
|
|
800
|
+
}
|
|
801
|
+
}
|
|
766
802
|
async upload(selector, files, index = null) {
|
|
767
803
|
const el = await this.waitForScopedEl(selector, index);
|
|
768
804
|
const fileList = Array.isArray(files) ? files : [files];
|