@magnit-ce/code-tests 0.0.4 → 0.0.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.
- package/dist/code-tests.cjs +70 -10
- package/dist/code-tests.d.cts +11 -2
- package/dist/code-tests.d.ts +11 -2
- package/dist/code-tests.js +68 -9
- package/dist/code-tests.min.js +21 -8
- package/package.json +1 -1
package/dist/code-tests.cjs
CHANGED
|
@@ -27,7 +27,8 @@ __export(code_tests_exports, {
|
|
|
27
27
|
CodeTestEventType: () => CodeTestEventType,
|
|
28
28
|
CodeTests: () => CodeTests,
|
|
29
29
|
CodeTestsElement: () => CodeTestsElement,
|
|
30
|
-
expect: () => expect
|
|
30
|
+
expect: () => expect,
|
|
31
|
+
prompt: () => prompt
|
|
31
32
|
});
|
|
32
33
|
module.exports = __toCommonJS(code_tests_exports);
|
|
33
34
|
|
|
@@ -393,7 +394,7 @@ pre
|
|
|
393
394
|
}`;
|
|
394
395
|
|
|
395
396
|
// src/code-tests.html?raw
|
|
396
|
-
var code_tests_default2 = '<slot name="header">\n <header id="header">\n <span id="title"><slot name="title"><span id="title-text">Tests</span></slot></span>\n <slot name="play-button">\n <button type="button" class="run" data-all>\n <slot name="play-button-label">\n <span id="play-button-label" class="button-label label icon">Run Tests</span>\n </slot>\n </button>\n </slot>\n <slot name="details"></slot>\n </header>\n</slot>\n<details id="before-all-details" class="hook">\n <summary id="before-all-summary">\n <span id="before-all-result-icon" class="result-icon"></span>\n <span id="before-all-description" class="description">Results from Before All Hook</span>\n </summary>\n <div id="before-all-results" class="results"></div>\n</details>\n<ul id="tests"></ul>\n<details id="after-all-details" class="hook">\n <summary id="after-all-summary">\n <span id="after-all-result-icon" class="result-icon"></span>\n <span id="after-all-description" class="description">Results from After All Hook</span>\n </summary>\n <div id="after-all-results" class="results"></div>\n</details>';
|
|
397
|
+
var code_tests_default2 = '<slot name="header">\n <header id="header">\n <span id="title"><slot name="title"><span id="title-text">Tests</span></slot></span>\n <slot name="play-button">\n <button type="button" class="run" data-all>\n <slot name="play-button-label">\n <span id="play-button-label" class="button-label label icon">Run Tests</span>\n </slot>\n </button>\n </slot>\n <slot name="details"></slot>\n </header>\n</slot>\n<details id="before-all-details" class="hook">\n <summary id="before-all-summary">\n <span id="before-all-result-icon" class="result-icon"></span>\n <span id="before-all-description" class="description">Results from Before All Hook</span>\n </summary>\n <div id="before-all-results" class="results"></div>\n</details>\n<ul id="tests"></ul>\n<details id="after-all-details" class="hook">\n <summary id="after-all-summary">\n <span id="after-all-result-icon" class="result-icon"></span>\n <span id="after-all-description" class="description">Results from After All Hook</span>\n </summary>\n <div id="after-all-results" class="results"></div>\n</details>\n\n<template id="prompt-template">\n <div class="prompt" part="prompt">\n <div class="prompt-display">\n <span class="icon prompt-icon"></span>\n <span class="label prompt-label"></span>\n </div>\n <div class="prompt-actions">\n <button class="prompt-button accept" type="button">Accept</button>\n <button class="prompt-button reject" type="button">Reject</button>\n </div>\n </div>\n</template>';
|
|
397
398
|
|
|
398
399
|
// src/api.ts
|
|
399
400
|
var TestPromise = class extends Promise {
|
|
@@ -429,7 +430,7 @@ var BEFOREALL = Symbol("beforeAll");
|
|
|
429
430
|
var BEFOREEACH = Symbol("beforeEach");
|
|
430
431
|
var AFTERALL = Symbol("afterAll");
|
|
431
432
|
var AFTEREACH = Symbol("afterEach");
|
|
432
|
-
var CodeTests = class {
|
|
433
|
+
var CodeTests = class _CodeTests {
|
|
433
434
|
static timeoutMS = 500;
|
|
434
435
|
static #expectInterval;
|
|
435
436
|
static #expectPromise;
|
|
@@ -466,10 +467,61 @@ var CodeTests = class {
|
|
|
466
467
|
});
|
|
467
468
|
return promise;
|
|
468
469
|
}
|
|
470
|
+
static async prompt(host, parent, message, options) {
|
|
471
|
+
return new Promise((resolve, reject) => {
|
|
472
|
+
const template = host.findElement("prompt-template");
|
|
473
|
+
const promptElement = _CodeTests.createElementFromTemplate(template);
|
|
474
|
+
promptElement.querySelector(".label").textContent = message;
|
|
475
|
+
const clickHandler = (event) => {
|
|
476
|
+
const composedPath = event.composedPath();
|
|
477
|
+
const acceptButton = composedPath.find((item) => item instanceof HTMLButtonElement && item.classList.contains("accept"));
|
|
478
|
+
if (acceptButton != null) {
|
|
479
|
+
const result = options?.onAccept?.() ?? true;
|
|
480
|
+
promptElement.removeEventListener("click", clickHandler);
|
|
481
|
+
resolve(result);
|
|
482
|
+
return;
|
|
483
|
+
}
|
|
484
|
+
const rejectButton = composedPath.find((item) => item instanceof HTMLButtonElement && item.classList.contains("reject"));
|
|
485
|
+
if (rejectButton != null) {
|
|
486
|
+
const result = options?.onReject?.() ?? false;
|
|
487
|
+
promptElement.removeEventListener("click", clickHandler);
|
|
488
|
+
resolve(result);
|
|
489
|
+
return;
|
|
490
|
+
}
|
|
491
|
+
};
|
|
492
|
+
promptElement.addEventListener("click", clickHandler);
|
|
493
|
+
if (options?.acceptLabel != null) {
|
|
494
|
+
promptElement.querySelector(".accept").textContent = options.acceptLabel;
|
|
495
|
+
}
|
|
496
|
+
if (options?.rejectLabel != null) {
|
|
497
|
+
promptElement.querySelector(".reject").textContent = options.rejectLabel;
|
|
498
|
+
}
|
|
499
|
+
const details = parent instanceof HTMLDetailsElement ? parent : parent.querySelector(".test-details");
|
|
500
|
+
if (details != null) {
|
|
501
|
+
details.open = true;
|
|
502
|
+
}
|
|
503
|
+
parent.querySelector(".result")?.append(promptElement);
|
|
504
|
+
});
|
|
505
|
+
}
|
|
506
|
+
static createElementFromTemplate(target, parent) {
|
|
507
|
+
const templateNode = target instanceof HTMLTemplateElement ? target : document.querySelector(target);
|
|
508
|
+
if (templateNode == null) {
|
|
509
|
+
throw new Error(`Unable to find template element from selector: ${target}`);
|
|
510
|
+
}
|
|
511
|
+
const firstChild = templateNode.content.cloneNode(true).querySelector("*");
|
|
512
|
+
if (firstChild == null) {
|
|
513
|
+
throw new Error(`Unable to find first child of template element`);
|
|
514
|
+
}
|
|
515
|
+
parent?.append(firstChild);
|
|
516
|
+
return firstChild;
|
|
517
|
+
}
|
|
469
518
|
};
|
|
470
519
|
function expect(value) {
|
|
471
520
|
return CodeTests.expect(value);
|
|
472
521
|
}
|
|
522
|
+
function prompt(host, parent, message, options) {
|
|
523
|
+
return CodeTests.prompt(host, parent, message, options);
|
|
524
|
+
}
|
|
473
525
|
|
|
474
526
|
// node_modules/.pnpm/ce-part-utils@0.0.0/node_modules/ce-part-utils/dist/ce-part-utils.js
|
|
475
527
|
var DEFAULT_ELEMENT_SELECTOR = ":not(slot,defs,g,rect,path,circle,ellipse,line,polygon,text,tspan,use,svg image,svg title,desc,template,template *)";
|
|
@@ -564,7 +616,14 @@ var CodeTestsElement = class extends HTMLElement {
|
|
|
564
616
|
}
|
|
565
617
|
this.#runTest(testId, test);
|
|
566
618
|
}
|
|
567
|
-
|
|
619
|
+
#getCurrentTestsPath() {
|
|
620
|
+
return this.getAttribute("src") ?? this.getAttribute("test") ?? this.getAttribute("tests") ?? this.getAttribute("run") ?? this.getAttribute("path");
|
|
621
|
+
}
|
|
622
|
+
async loadTests(testsPath) {
|
|
623
|
+
const path = testsPath ?? this.#getCurrentTestsPath();
|
|
624
|
+
if (path == null) {
|
|
625
|
+
return;
|
|
626
|
+
}
|
|
568
627
|
try {
|
|
569
628
|
this.getElement("tests").innerHTML = "";
|
|
570
629
|
this.#tests.clear();
|
|
@@ -680,7 +739,7 @@ var CodeTestsElement = class extends HTMLElement {
|
|
|
680
739
|
beforeAllHookElement.classList.add("running");
|
|
681
740
|
beforeAllHookElement.part.add("running");
|
|
682
741
|
for (const [hook, ids] of beforeHooks) {
|
|
683
|
-
hookResult = await hook();
|
|
742
|
+
hookResult = await hook(this, beforeAllHookElement);
|
|
684
743
|
this.#handleHookResult(hookResult, true, "before");
|
|
685
744
|
}
|
|
686
745
|
beforeAllHookElement.part.remove("running");
|
|
@@ -723,7 +782,7 @@ var CodeTestsElement = class extends HTMLElement {
|
|
|
723
782
|
afterAllHookElement.classList.add("running");
|
|
724
783
|
afterAllHookElement.part.add("running");
|
|
725
784
|
for (const [hook, ids] of afterHooks) {
|
|
726
|
-
hookResult = await hook();
|
|
785
|
+
hookResult = await hook(this, afterAllHookElement);
|
|
727
786
|
this.#handleHookResult(hookResult, true, "after");
|
|
728
787
|
}
|
|
729
788
|
afterAllHookElement.part.remove("running");
|
|
@@ -799,17 +858,17 @@ var CodeTestsElement = class extends HTMLElement {
|
|
|
799
858
|
if (beforeHooks != null) {
|
|
800
859
|
for (const [hook, ids] of beforeHooks) {
|
|
801
860
|
if (ids.has(testId)) {
|
|
802
|
-
beforeResult = await hook();
|
|
861
|
+
beforeResult = await hook(this, testElement);
|
|
803
862
|
break;
|
|
804
863
|
}
|
|
805
864
|
}
|
|
806
865
|
}
|
|
807
|
-
testResult = await test();
|
|
866
|
+
testResult = await test(this, testElement);
|
|
808
867
|
const afterHooks = this.#hooks.get(AFTEREACH);
|
|
809
868
|
if (afterHooks != null) {
|
|
810
869
|
for (const [hook, ids] of afterHooks) {
|
|
811
870
|
if (ids.has(testId)) {
|
|
812
|
-
afterResult = await hook();
|
|
871
|
+
afterResult = await hook(this, testElement);
|
|
813
872
|
break;
|
|
814
873
|
}
|
|
815
874
|
}
|
|
@@ -1075,5 +1134,6 @@ if (customElements.get(COMPONENT_TAG_NAME) == null) {
|
|
|
1075
1134
|
CodeTestEventType,
|
|
1076
1135
|
CodeTests,
|
|
1077
1136
|
CodeTestsElement,
|
|
1078
|
-
expect
|
|
1137
|
+
expect,
|
|
1138
|
+
prompt
|
|
1079
1139
|
});
|
package/dist/code-tests.d.cts
CHANGED
|
@@ -14,8 +14,17 @@ declare class CodeTests {
|
|
|
14
14
|
static expect<T>(value: T): TestPromise<T>;
|
|
15
15
|
static expectSync<T>(value: T): TestPromise<T>;
|
|
16
16
|
static expectBefore<T>(value: T): TestPromise<T>;
|
|
17
|
+
static prompt(host: CodeTestsElement, parent: HTMLElement, message: string, options?: PromptOptions): Promise<boolean>;
|
|
18
|
+
static createElementFromTemplate(target: string | HTMLTemplateElement, parent?: HTMLElement): HTMLElement;
|
|
17
19
|
}
|
|
18
20
|
declare function expect(value: any): TestPromise<any>;
|
|
21
|
+
type PromptOptions = {
|
|
22
|
+
acceptLabel?: string;
|
|
23
|
+
rejectLabel?: string;
|
|
24
|
+
onAccept?: () => void;
|
|
25
|
+
onReject?: () => void;
|
|
26
|
+
};
|
|
27
|
+
declare function prompt(host: CodeTestsElement, parent: HTMLElement, message: string, options?: PromptOptions): Promise<boolean>;
|
|
19
28
|
|
|
20
29
|
type CodeTestsProperties = {};
|
|
21
30
|
declare enum CodeTestEventType {
|
|
@@ -32,11 +41,11 @@ declare class CodeTestsElement extends HTMLElement {
|
|
|
32
41
|
constructor();
|
|
33
42
|
connectedCallback(): void;
|
|
34
43
|
disconnectedCallback(): void;
|
|
35
|
-
loadTests(
|
|
44
|
+
loadTests(testsPath?: string): Promise<void>;
|
|
36
45
|
runTests(): Promise<void>;
|
|
37
46
|
static create(properties: CodeTestsProperties): HTMLElement;
|
|
38
47
|
static observedAttributes: string[];
|
|
39
48
|
attributeChangedCallback(attributeName: string, oldValue: string, newValue: string): void;
|
|
40
49
|
}
|
|
41
50
|
|
|
42
|
-
export { AFTERALL, AFTEREACH, BEFOREALL, BEFOREEACH, CodeTestEventType, CodeTests, CodeTestsElement, type CodeTestsProperties, expect };
|
|
51
|
+
export { AFTERALL, AFTEREACH, BEFOREALL, BEFOREEACH, CodeTestEventType, CodeTests, CodeTestsElement, type CodeTestsProperties, expect, prompt };
|
package/dist/code-tests.d.ts
CHANGED
|
@@ -14,8 +14,17 @@ declare class CodeTests {
|
|
|
14
14
|
static expect<T>(value: T): TestPromise<T>;
|
|
15
15
|
static expectSync<T>(value: T): TestPromise<T>;
|
|
16
16
|
static expectBefore<T>(value: T): TestPromise<T>;
|
|
17
|
+
static prompt(host: CodeTestsElement, parent: HTMLElement, message: string, options?: PromptOptions): Promise<boolean>;
|
|
18
|
+
static createElementFromTemplate(target: string | HTMLTemplateElement, parent?: HTMLElement): HTMLElement;
|
|
17
19
|
}
|
|
18
20
|
declare function expect(value: any): TestPromise<any>;
|
|
21
|
+
type PromptOptions = {
|
|
22
|
+
acceptLabel?: string;
|
|
23
|
+
rejectLabel?: string;
|
|
24
|
+
onAccept?: () => void;
|
|
25
|
+
onReject?: () => void;
|
|
26
|
+
};
|
|
27
|
+
declare function prompt(host: CodeTestsElement, parent: HTMLElement, message: string, options?: PromptOptions): Promise<boolean>;
|
|
19
28
|
|
|
20
29
|
type CodeTestsProperties = {};
|
|
21
30
|
declare enum CodeTestEventType {
|
|
@@ -32,11 +41,11 @@ declare class CodeTestsElement extends HTMLElement {
|
|
|
32
41
|
constructor();
|
|
33
42
|
connectedCallback(): void;
|
|
34
43
|
disconnectedCallback(): void;
|
|
35
|
-
loadTests(
|
|
44
|
+
loadTests(testsPath?: string): Promise<void>;
|
|
36
45
|
runTests(): Promise<void>;
|
|
37
46
|
static create(properties: CodeTestsProperties): HTMLElement;
|
|
38
47
|
static observedAttributes: string[];
|
|
39
48
|
attributeChangedCallback(attributeName: string, oldValue: string, newValue: string): void;
|
|
40
49
|
}
|
|
41
50
|
|
|
42
|
-
export { AFTERALL, AFTEREACH, BEFOREALL, BEFOREEACH, CodeTestEventType, CodeTests, CodeTestsElement, type CodeTestsProperties, expect };
|
|
51
|
+
export { AFTERALL, AFTEREACH, BEFOREALL, BEFOREEACH, CodeTestEventType, CodeTests, CodeTestsElement, type CodeTestsProperties, expect, prompt };
|
package/dist/code-tests.js
CHANGED
|
@@ -360,7 +360,7 @@ pre
|
|
|
360
360
|
}`;
|
|
361
361
|
|
|
362
362
|
// src/code-tests.html?raw
|
|
363
|
-
var code_tests_default2 = '<slot name="header">\n <header id="header">\n <span id="title"><slot name="title"><span id="title-text">Tests</span></slot></span>\n <slot name="play-button">\n <button type="button" class="run" data-all>\n <slot name="play-button-label">\n <span id="play-button-label" class="button-label label icon">Run Tests</span>\n </slot>\n </button>\n </slot>\n <slot name="details"></slot>\n </header>\n</slot>\n<details id="before-all-details" class="hook">\n <summary id="before-all-summary">\n <span id="before-all-result-icon" class="result-icon"></span>\n <span id="before-all-description" class="description">Results from Before All Hook</span>\n </summary>\n <div id="before-all-results" class="results"></div>\n</details>\n<ul id="tests"></ul>\n<details id="after-all-details" class="hook">\n <summary id="after-all-summary">\n <span id="after-all-result-icon" class="result-icon"></span>\n <span id="after-all-description" class="description">Results from After All Hook</span>\n </summary>\n <div id="after-all-results" class="results"></div>\n</details>';
|
|
363
|
+
var code_tests_default2 = '<slot name="header">\n <header id="header">\n <span id="title"><slot name="title"><span id="title-text">Tests</span></slot></span>\n <slot name="play-button">\n <button type="button" class="run" data-all>\n <slot name="play-button-label">\n <span id="play-button-label" class="button-label label icon">Run Tests</span>\n </slot>\n </button>\n </slot>\n <slot name="details"></slot>\n </header>\n</slot>\n<details id="before-all-details" class="hook">\n <summary id="before-all-summary">\n <span id="before-all-result-icon" class="result-icon"></span>\n <span id="before-all-description" class="description">Results from Before All Hook</span>\n </summary>\n <div id="before-all-results" class="results"></div>\n</details>\n<ul id="tests"></ul>\n<details id="after-all-details" class="hook">\n <summary id="after-all-summary">\n <span id="after-all-result-icon" class="result-icon"></span>\n <span id="after-all-description" class="description">Results from After All Hook</span>\n </summary>\n <div id="after-all-results" class="results"></div>\n</details>\n\n<template id="prompt-template">\n <div class="prompt" part="prompt">\n <div class="prompt-display">\n <span class="icon prompt-icon"></span>\n <span class="label prompt-label"></span>\n </div>\n <div class="prompt-actions">\n <button class="prompt-button accept" type="button">Accept</button>\n <button class="prompt-button reject" type="button">Reject</button>\n </div>\n </div>\n</template>';
|
|
364
364
|
|
|
365
365
|
// src/api.ts
|
|
366
366
|
var TestPromise = class extends Promise {
|
|
@@ -396,7 +396,7 @@ var BEFOREALL = Symbol("beforeAll");
|
|
|
396
396
|
var BEFOREEACH = Symbol("beforeEach");
|
|
397
397
|
var AFTERALL = Symbol("afterAll");
|
|
398
398
|
var AFTEREACH = Symbol("afterEach");
|
|
399
|
-
var CodeTests = class {
|
|
399
|
+
var CodeTests = class _CodeTests {
|
|
400
400
|
static timeoutMS = 500;
|
|
401
401
|
static #expectInterval;
|
|
402
402
|
static #expectPromise;
|
|
@@ -433,10 +433,61 @@ var CodeTests = class {
|
|
|
433
433
|
});
|
|
434
434
|
return promise;
|
|
435
435
|
}
|
|
436
|
+
static async prompt(host, parent, message, options) {
|
|
437
|
+
return new Promise((resolve, reject) => {
|
|
438
|
+
const template = host.findElement("prompt-template");
|
|
439
|
+
const promptElement = _CodeTests.createElementFromTemplate(template);
|
|
440
|
+
promptElement.querySelector(".label").textContent = message;
|
|
441
|
+
const clickHandler = (event) => {
|
|
442
|
+
const composedPath = event.composedPath();
|
|
443
|
+
const acceptButton = composedPath.find((item) => item instanceof HTMLButtonElement && item.classList.contains("accept"));
|
|
444
|
+
if (acceptButton != null) {
|
|
445
|
+
const result = options?.onAccept?.() ?? true;
|
|
446
|
+
promptElement.removeEventListener("click", clickHandler);
|
|
447
|
+
resolve(result);
|
|
448
|
+
return;
|
|
449
|
+
}
|
|
450
|
+
const rejectButton = composedPath.find((item) => item instanceof HTMLButtonElement && item.classList.contains("reject"));
|
|
451
|
+
if (rejectButton != null) {
|
|
452
|
+
const result = options?.onReject?.() ?? false;
|
|
453
|
+
promptElement.removeEventListener("click", clickHandler);
|
|
454
|
+
resolve(result);
|
|
455
|
+
return;
|
|
456
|
+
}
|
|
457
|
+
};
|
|
458
|
+
promptElement.addEventListener("click", clickHandler);
|
|
459
|
+
if (options?.acceptLabel != null) {
|
|
460
|
+
promptElement.querySelector(".accept").textContent = options.acceptLabel;
|
|
461
|
+
}
|
|
462
|
+
if (options?.rejectLabel != null) {
|
|
463
|
+
promptElement.querySelector(".reject").textContent = options.rejectLabel;
|
|
464
|
+
}
|
|
465
|
+
const details = parent instanceof HTMLDetailsElement ? parent : parent.querySelector(".test-details");
|
|
466
|
+
if (details != null) {
|
|
467
|
+
details.open = true;
|
|
468
|
+
}
|
|
469
|
+
parent.querySelector(".result")?.append(promptElement);
|
|
470
|
+
});
|
|
471
|
+
}
|
|
472
|
+
static createElementFromTemplate(target, parent) {
|
|
473
|
+
const templateNode = target instanceof HTMLTemplateElement ? target : document.querySelector(target);
|
|
474
|
+
if (templateNode == null) {
|
|
475
|
+
throw new Error(`Unable to find template element from selector: ${target}`);
|
|
476
|
+
}
|
|
477
|
+
const firstChild = templateNode.content.cloneNode(true).querySelector("*");
|
|
478
|
+
if (firstChild == null) {
|
|
479
|
+
throw new Error(`Unable to find first child of template element`);
|
|
480
|
+
}
|
|
481
|
+
parent?.append(firstChild);
|
|
482
|
+
return firstChild;
|
|
483
|
+
}
|
|
436
484
|
};
|
|
437
485
|
function expect(value) {
|
|
438
486
|
return CodeTests.expect(value);
|
|
439
487
|
}
|
|
488
|
+
function prompt(host, parent, message, options) {
|
|
489
|
+
return CodeTests.prompt(host, parent, message, options);
|
|
490
|
+
}
|
|
440
491
|
|
|
441
492
|
// node_modules/.pnpm/ce-part-utils@0.0.0/node_modules/ce-part-utils/dist/ce-part-utils.js
|
|
442
493
|
var DEFAULT_ELEMENT_SELECTOR = ":not(slot,defs,g,rect,path,circle,ellipse,line,polygon,text,tspan,use,svg image,svg title,desc,template,template *)";
|
|
@@ -531,7 +582,14 @@ var CodeTestsElement = class extends HTMLElement {
|
|
|
531
582
|
}
|
|
532
583
|
this.#runTest(testId, test);
|
|
533
584
|
}
|
|
534
|
-
|
|
585
|
+
#getCurrentTestsPath() {
|
|
586
|
+
return this.getAttribute("src") ?? this.getAttribute("test") ?? this.getAttribute("tests") ?? this.getAttribute("run") ?? this.getAttribute("path");
|
|
587
|
+
}
|
|
588
|
+
async loadTests(testsPath) {
|
|
589
|
+
const path = testsPath ?? this.#getCurrentTestsPath();
|
|
590
|
+
if (path == null) {
|
|
591
|
+
return;
|
|
592
|
+
}
|
|
535
593
|
try {
|
|
536
594
|
this.getElement("tests").innerHTML = "";
|
|
537
595
|
this.#tests.clear();
|
|
@@ -647,7 +705,7 @@ var CodeTestsElement = class extends HTMLElement {
|
|
|
647
705
|
beforeAllHookElement.classList.add("running");
|
|
648
706
|
beforeAllHookElement.part.add("running");
|
|
649
707
|
for (const [hook, ids] of beforeHooks) {
|
|
650
|
-
hookResult = await hook();
|
|
708
|
+
hookResult = await hook(this, beforeAllHookElement);
|
|
651
709
|
this.#handleHookResult(hookResult, true, "before");
|
|
652
710
|
}
|
|
653
711
|
beforeAllHookElement.part.remove("running");
|
|
@@ -690,7 +748,7 @@ var CodeTestsElement = class extends HTMLElement {
|
|
|
690
748
|
afterAllHookElement.classList.add("running");
|
|
691
749
|
afterAllHookElement.part.add("running");
|
|
692
750
|
for (const [hook, ids] of afterHooks) {
|
|
693
|
-
hookResult = await hook();
|
|
751
|
+
hookResult = await hook(this, afterAllHookElement);
|
|
694
752
|
this.#handleHookResult(hookResult, true, "after");
|
|
695
753
|
}
|
|
696
754
|
afterAllHookElement.part.remove("running");
|
|
@@ -766,17 +824,17 @@ var CodeTestsElement = class extends HTMLElement {
|
|
|
766
824
|
if (beforeHooks != null) {
|
|
767
825
|
for (const [hook, ids] of beforeHooks) {
|
|
768
826
|
if (ids.has(testId)) {
|
|
769
|
-
beforeResult = await hook();
|
|
827
|
+
beforeResult = await hook(this, testElement);
|
|
770
828
|
break;
|
|
771
829
|
}
|
|
772
830
|
}
|
|
773
831
|
}
|
|
774
|
-
testResult = await test();
|
|
832
|
+
testResult = await test(this, testElement);
|
|
775
833
|
const afterHooks = this.#hooks.get(AFTEREACH);
|
|
776
834
|
if (afterHooks != null) {
|
|
777
835
|
for (const [hook, ids] of afterHooks) {
|
|
778
836
|
if (ids.has(testId)) {
|
|
779
|
-
afterResult = await hook();
|
|
837
|
+
afterResult = await hook(this, testElement);
|
|
780
838
|
break;
|
|
781
839
|
}
|
|
782
840
|
}
|
|
@@ -1041,5 +1099,6 @@ export {
|
|
|
1041
1099
|
CodeTestEventType,
|
|
1042
1100
|
CodeTests,
|
|
1043
1101
|
CodeTestsElement,
|
|
1044
|
-
expect
|
|
1102
|
+
expect,
|
|
1103
|
+
prompt
|
|
1045
1104
|
};
|
package/dist/code-tests.min.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var
|
|
1
|
+
var R=`:host
|
|
2
2
|
{
|
|
3
3
|
/*** gray ***/
|
|
4
4
|
--uchu-light-gray-raw: 95.57% 0.003 286.35;
|
|
@@ -356,7 +356,7 @@ pre
|
|
|
356
356
|
{
|
|
357
357
|
from { transform: rotate(0deg); }
|
|
358
358
|
to { transform: rotate(360deg); }
|
|
359
|
-
}`;var
|
|
359
|
+
}`;var F=`<slot name="header">
|
|
360
360
|
<header id="header">
|
|
361
361
|
<span id="title"><slot name="title"><span id="title-text">Tests</span></slot></span>
|
|
362
362
|
<slot name="play-button">
|
|
@@ -383,15 +383,28 @@ pre
|
|
|
383
383
|
<span id="after-all-description" class="description">Results from After All Hook</span>
|
|
384
384
|
</summary>
|
|
385
385
|
<div id="after-all-results" class="results"></div>
|
|
386
|
-
</details
|
|
386
|
+
</details>
|
|
387
|
+
|
|
388
|
+
<template id="prompt-template">
|
|
389
|
+
<div class="prompt" part="prompt">
|
|
390
|
+
<div class="prompt-display">
|
|
391
|
+
<span class="icon prompt-icon"></span>
|
|
392
|
+
<span class="label prompt-label"></span>
|
|
393
|
+
</div>
|
|
394
|
+
<div class="prompt-actions">
|
|
395
|
+
<button class="prompt-button accept" type="button">Accept</button>
|
|
396
|
+
<button class="prompt-button reject" type="button">Reject</button>
|
|
397
|
+
</div>
|
|
398
|
+
</div>
|
|
399
|
+
</template>`;var T=class extends Promise{async toBeDefined(t){if(await this==null)throw new Error(`${t??"Value"} is undefined`)}async toBe(t,s=!1){let e=await this;if((s==!0?e===t:e==t)==!1)throw new Error(` Value is not equal.
|
|
387
400
|
Expected: ${t}
|
|
388
|
-
Result: ${e}`)}async toContainText(t){let s=await this}async toHaveAttribute(t){let s=await this;if(!(s instanceof HTMLElement))throw new Error("Unable to check for attribute on non-HTMLElement target");if(s.getAttribute(t))throw new Error("Taret does not have attribute")}},y=Symbol("beforeAll"),v=Symbol("beforeEach"),k=Symbol("afterAll"),x=Symbol("afterEach"),
|
|
401
|
+
Result: ${e}`)}async toContainText(t){let s=await this}async toHaveAttribute(t){let s=await this;if(!(s instanceof HTMLElement))throw new Error("Unable to check for attribute on non-HTMLElement target");if(s.getAttribute(t))throw new Error("Taret does not have attribute")}},y=Symbol("beforeAll"),v=Symbol("beforeEach"),k=Symbol("afterAll"),x=Symbol("afterEach"),L=class d{static timeoutMS=500;static#e;static#p;static expect(t){return new T(async(e,r)=>{if(t instanceof Promise){let a=await t;e(a);return}e(t)})}static expectSync(t){return new T(async(e,r)=>{if(t instanceof Promise){let a=await t;e(a);return}e(t)})}static expectBefore(t){return new T(async(e,r)=>{if(t instanceof Promise){let a=await t;e(a);return}e(t)})}static async prompt(t,s,e,r){return new Promise((a,n)=>{let o=t.findElement("prompt-template"),l=d.createElementFromTemplate(o);l.querySelector(".label").textContent=e;let u=h=>{let f=h.composedPath();if(f.find(i=>i instanceof HTMLButtonElement&&i.classList.contains("accept"))!=null){let i=r?.onAccept?.()??!0;l.removeEventListener("click",u),a(i);return}if(f.find(i=>i instanceof HTMLButtonElement&&i.classList.contains("reject"))!=null){let i=r?.onReject?.()??!1;l.removeEventListener("click",u),a(i);return}};l.addEventListener("click",u),r?.acceptLabel!=null&&(l.querySelector(".accept").textContent=r.acceptLabel),r?.rejectLabel!=null&&(l.querySelector(".reject").textContent=r.rejectLabel);let c=s instanceof HTMLDetailsElement?s:s.querySelector(".test-details");c!=null&&(c.open=!0),s.querySelector(".result")?.append(l)})}static createElementFromTemplate(t,s){let e=t instanceof HTMLTemplateElement?t:document.querySelector(t);if(e==null)throw new Error(`Unable to find template element from selector: ${t}`);let r=e.content.cloneNode(!0).querySelector("*");if(r==null)throw new Error("Unable to find first child of template element");return s?.append(r),r}};function I(d){return L.expect(d)}function U(d,t,s,e){return L.prompt(d,t,s,e)}var $=":not(slot,defs,g,rect,path,circle,ellipse,line,polygon,text,tspan,use,svg image,svg title,desc,template,template *)";function P(d){let t=[...d.querySelectorAll(`${$}[id]`)];for(let e=0;e<t.length;e++)t[e].part.add(t[e].id);let s=[...d.querySelectorAll(`${$}[class]`)];for(let e=0;e<s.length;e++)s[e].part.add(...s[e].classList)}var N=(r=>(r.BeforeAll="beforeall",r.AfterAll="afterall",r.BeforeTest="beforetest",r.AfterTest="aftertest",r))(N||{}),H=Symbol("No Test Defined"),D=new CSSStyleSheet;D.replaceSync(R);var B="code-tests",S=class extends HTMLElement{componentParts=new Map;getElement(t){if(this.componentParts.get(t)==null){let s=this.findElement(t);s!=null&&this.componentParts.set(t,s)}return this.componentParts.get(t)}findElement(t){return this.shadowRoot.getElementById(t)}#e=new Map;#p={[y]:C(),[v]:C(),[x]:C(),[k]:C()};#t=!0;constructor(){super(),this.attachShadow({mode:"open"}),this.shadowRoot.innerHTML=F,this.shadowRoot.adoptedStyleSheets.push(D),this.#c=this.#h.bind(this)}connectedCallback(){if(P(this.shadowRoot),this.addEventListener("click",this.#c),this.getAttribute("auto")=="false")return;let t=this.getAttribute("src")??this.getAttribute("test")??this.getAttribute("tests")??this.getAttribute("run")??this.getAttribute("path");t!=null&&this.loadTests(t)}disconnectedCallback(){this.removeEventListener("click",this.#c)}#c;#h(t){let s=t.composedPath().find(n=>n instanceof HTMLButtonElement&&n.classList.contains("run"));if(s==null)return;let e=s.closest("li");if(e==null){s.hasAttribute("data-all")==!0&&this.runTests();return}let r=e.dataset.testId;if(r==null)return;let a=this.#s.get(r);a!=null&&this.#u(r,a)}#f(){return this.getAttribute("src")??this.getAttribute("test")??this.getAttribute("tests")??this.getAttribute("run")??this.getAttribute("path")}async loadTests(t){let s=t??this.#f();if(s!=null)try{this.getElement("tests").innerHTML="",this.#s.clear(),this.classList.remove("has-before-hook"),this.classList.remove("has-after-hook");let e=window.location.href.lastIndexOf("/"),a=window.location.href.substring(e).indexOf(".")!=-1==!0?window.location.href.substring(0,e+1):window.location.href,n=a+s.substring(0,s.lastIndexOf("/")+1),o=a+s,l=await(await fetch(o)).text();l=l.replaceAll(/['"`](((\.\/)|(\.\.\/))+(.*))['"`]/g,`'${n}$1'`);let u=new File([l],s.substring(s.lastIndexOf("/")),{type:"text/javascript"}),h=await import(URL.createObjectURL(u)),f=h.tests??h.default;if(f==null)throw new Error(`Unable to find tests definition in file at path: ${s}`);let E=f[y];if(E!=null){if(this.#e.get(y)==null){let p=new Map;p.set(E,new Set),this.#e.set(y,p)}this.classList.add("has-before-hook")}let b=f[v];if(b!=null&&this.#e.get(v)==null){let p=new Map;p.set(b,new Set),this.#e.set(v,p)}let i=f[k];if(i!=null){if(this.#e.get(k)==null){let p=new Map;p.set(i,new Set),this.#e.set(k,p)}this.classList.add("has-after-hook")}let A=f[x];if(A!=null&&this.#e.get(x)==null){let p=new Map;p.set(A,new Set),this.#e.set(x,p)}for(let[w,p]of Object.entries(f)){let M=this.#g(w,p);if(E!=null){let m=this.#e.get(y);if(m!=null){let g=m.get(E);g?.add(M)}}if(b!=null){let m=this.#e.get(v);if(m!=null){let g=m.get(b);g?.add(M)}}if(i!=null){let m=this.#e.get(k);if(m!=null){let g=m.get(i);g?.add(M)}}if(A!=null){let m=this.#e.get(x);if(m!=null){let g=m.get(A);g?.add(M)}}}}catch(e){this.#i("An error occurred while loading the tasks:",e)}}async runTests(){this.dispatchEvent(new CustomEvent("beforeall",{bubbles:!0,composed:!0})),this.#t=!0,this.classList.add("running"),this.toggleAttribute("success",!1),this.#m();let t=this.hasAttribute("in-order"),s=this.#e.get(y);if(s!=null){let a;try{let n=this.getElement("before-all-details");n.classList.add("running"),n.part.add("running");for(let[o,l]of s)a=await o(this,n),this.#n(a,!0,"before");n.part.remove("running"),n.classList.remove("running")}catch(n){this.#n(a,!1,"before",n),console.error(n),this.#t=!1,this.classList.remove("running"),this.part.remove("running"),this.dispatchEvent(new CustomEvent("afterall",{bubbles:!0,composed:!0}));return}}if(t==!1){let a=[];for(let[n,o]of this.#s)a.push(this.#u(n,o));await Promise.all(a)}else for(let[a,n]of this.#s){if(this.#t==!1)break;await this.#u(a,n)}if(this.#t==!1){this.classList.remove("running"),this.part.remove("running"),this.dispatchEvent(new CustomEvent("afterall",{bubbles:!0,composed:!0}));return}let e=this.#e.get(k);if(e!=null){let a;try{let n=this.getElement("after-all-details");n.classList.add("running"),n.part.add("running");for(let[o,l]of e)a=await o(this,n),this.#n(a,!0,"after");n.part.remove("running"),n.classList.remove("running")}catch(n){this.#n(a,!1,"after",n),console.error(n),this.#t=!1,this.classList.remove("running"),this.part.remove("running"),this.dispatchEvent(new CustomEvent("afterall",{bubbles:!0,composed:!0}));return}}let r=this.shadowRoot.querySelectorAll('[success="false"]');this.setAttribute("success",r.length==0?"true":"false"),this.classList.remove("running"),this.part.remove("running"),this.dispatchEvent(new CustomEvent("afterall",{bubbles:!0,composed:!0}))}#m(){for(let[e,r]of this.#s){let a=this.getElement("tests").querySelector(`[data-test-id="${e}"]`);if(a==null){this.#i(`Unable to find test element for test: ${e}`);return}a.toggleAttribute("success",!1),a.classList.remove("success","fail"),a.part.remove("success","fail")}let t=this.getElement("before-all-details");t.toggleAttribute("success",!1),t.classList.remove("success","fail"),t.part.remove("success","fail");let s=this.getElement("after-all-details");s.toggleAttribute("success",!1),s.classList.remove("success","fail"),s.part.remove("success","fail")}async#u(t,s){let e=this.getElement("tests").querySelector(`[data-test-id="${t}"]`);if(e==null){this.#i(`Unable to find test element for test: ${t}`);return}e.toggleAttribute("success",!1),e.classList.add("running"),e.part.add("running"),e.classList.remove("success","fail"),e.part.remove("success","fail");let r=e.querySelector(".result-icon");r?.classList.remove("success","fail"),r?.part.remove("success","fail"),r?.classList.add("running"),r?.part.add("running");let a=e.querySelector(".error-message");a!=null&&(a.textContent="");let n=e.querySelector("details");n!=null&&(n.open=!1);let o=H,l,u=H,c;try{if(this.dispatchEvent(new CustomEvent("beforetest",{bubbles:!0,cancelable:!0,composed:!0,detail:{testElement:e}}))==!0){let f=this.#e.get(v);if(f!=null){for(let[b,i]of f)if(i.has(t)){o=await b(this,e);break}}l=await s(this,e);let E=this.#e.get(x);if(E!=null){for(let[b,i]of E)if(i.has(t)){u=await b(this,e);break}}c="before",o!=H&&this.#a(e,o,!0,void 0,c),c=void 0,this.#a(e,l,!0,void 0,c),c="after",u!=H&&this.#a(e,u,!0,void 0,c)}}catch(h){this.#a(e,l,!1,h,c),console.error(h),this.#t=!1}finally{e?.classList.remove("running"),e?.part.remove("running"),r?.classList.remove("running"),r?.part.remove("running"),this.dispatchEvent(new CustomEvent("aftertest",{bubbles:!0,cancelable:!0,composed:!0,detail:{testElement:e}}))}}#a(t,s,e,r,a){if(s instanceof HTMLElement)this.#o(t,s,e,a);else if(s==null){let o=a==null?"Passed":"Hook Ran Successfully",l=this.#r(e==!0?`${o}`:`Failed${r!=null?`:
|
|
389
402
|
${r.message}`:""}`,e,a);this.#o(t,l,e,a)}else if(typeof s=="string"){let o=this.#r(`${s}${r==null?"":`:
|
|
390
|
-
${r.message}`}`,e,a);this.#o(t,o,e,a)}else if(typeof s=="object"){let o=s;if(o.success!=null&&o.expected!=null&&o.value!=null){let l=a==null?"Passed":"Success",
|
|
403
|
+
${r.message}`}`,e,a);this.#o(t,o,e,a)}else if(typeof s=="object"){let o=s;if(o.success!=null&&o.expected!=null&&o.value!=null){let l=a==null?"Passed":"Success",u=a==null?"Failed":"Fail",c=this.#r(`${o.success==!0?`${l}:`:`${u}:`}
|
|
391
404
|
Expected:${o.expected}
|
|
392
|
-
Result:${o.value}`,o.success,a);this.#o(t,
|
|
405
|
+
Result:${o.value}`,o.success,a);this.#o(t,c,e,a)}}let n=t.querySelector("details");n!=null&&(n.open=!0)}#n(t,s,e,r){if(t instanceof HTMLElement)this.#l(t,s,e);else{let n;if(t==null)n=this.#r(s==!0?"Hook Ran Successfully":`Failed${r!=null?`:
|
|
393
406
|
${r.message}`:""}`,s),this.#l(n,s,e);else if(typeof t=="string")n=this.#r(`${t}${r==null?"":`:
|
|
394
407
|
${r.message}`}`,s),this.#l(n,s,e);else if(typeof t=="object"){let o=t;o.success!=null&&o.expected!=null&&o.value!=null&&(n=this.#r(`${o.success==!0?"Success:":"Fail:"}
|
|
395
408
|
Expected:${o.expected}
|
|
396
|
-
Result:${o.value}`,o.success),this.#l(n,s,e))}}let a=this.getElement(`${e}-all-details`);a!=null&&(a.open=!0)}static create(t){let s=document.createElement("code-tests");return console.log(t),s}#s=new Map;#g(t,s){let e=
|
|
397
|
-
${s.message}`,console.error(s));let e=document.createElement("li");e.classList.add("error","process-error"),e.part.add("error","process-error");let r=document.createElement("code");r.classList.add("code","process-error-code"),r.part.add("code","process-error-code");let a=document.createElement("pre");a.classList.add("pre","process-error-pre"),a.part.add("pre","process-error-pre"),a.textContent=t,r.append(a),e.append(r),this.getElement("tests").append(e)}#d(t){if(t=="ordered"){let s=this.shadowRoot.querySelector("ul");if(s==null)return;let e=this.shadowRoot?.querySelectorAll("li"),r=document.createElement("ol");e!=null&&r.append(...e),r.id="tests",s.replaceWith(r)}else{let s=this.shadowRoot.querySelector("ol");if(s==null)return;let e=this.shadowRoot?.querySelectorAll("li"),r=document.createElement("ul");r.id="tests",e!=null&&r.append(...e),s.replaceWith(r)}}static observedAttributes=["in-order"];attributeChangedCallback(t,s,e){t=="in-order"&&(e==null?this.#d("unordered"):this.#d("ordered"))}};function
|
|
409
|
+
Result:${o.value}`,o.success),this.#l(n,s,e))}}let a=this.getElement(`${e}-all-details`);a!=null&&(a.open=!0)}static create(t){let s=document.createElement("code-tests");return console.log(t),s}#s=new Map;#g(t,s){let e=C();this.#s.set(e,s);let r=this.#b(e,t);return this.getElement("tests").append(r),e}#b(t,s){let e=document.createElement("li");e.dataset.testId=t,e.classList.add("test"),e.part.add("test");let r=document.createElement("details");r.classList.add("test-details"),r.part.add("test-details");let a=document.createElement("summary");a.classList.add("test-summary"),a.part.add("test-summary");let n=document.createElement("div");n.classList.add("result-icon"),n.part.add("result-icon"),a.append(n);let o=document.createElement("span");o.classList.add("description","test-description"),o.textContent=s,a.append(o);let l=document.createElement("button");l.classList.add("run","test-run"),l.part.add("run","test-run"),l.textContent="Run Test",l.title="Run Test",a.append(l);let u=document.createElement("div");u.classList.add("before-result","test-before-result"),u.part.add("before-result","test-before-result");let c=document.createElement("div");c.classList.add("result","test-result"),c.part.add("result","test-result");let h=document.createElement("div");return h.classList.add("after-result","test-after-result"),h.part.add("after-result","test-after-result"),r.append(a),r.append(u),r.append(c),r.append(h),e.append(r),e}#o(t,s,e,r){t.setAttribute("success",e==!0?"true":"false"),t.classList.toggle("success",e),t.part.toggle("success",e),t.classList.toggle("fail",!e),t.part.toggle("fail",!e);let a=t.querySelector(".result-icon");a?.classList.toggle("success",e),a?.part.toggle("success",e),a?.classList.toggle("fail",!e),a?.part.toggle("fail",!e);let n=t.querySelector(`.${r==null?"result":r=="before"?"before-result":"after-result"}`);if(n==null){this.#i("Unable to find result element");return}n.innerHTML="",n.appendChild(s)}#r(t,s,e){let r=document.createElement("code");r.classList.add("code"),r.part.add("code");let a=document.createElement("pre");a.textContent=t;let n=s==!0?"success-message":"error-message";return a.classList.add("pre",n),a.part.add("pre",n),r.appendChild(a),r}#l(t,s,e){let r=this.getElement(`${e}-all-details`),a=this.getElement(`${e}-all-results`);r.setAttribute("success",s==!0?"true":"false"),r.classList.toggle("success",s),r.part.toggle("success",s),r.classList.toggle("fail",!s),r.part.toggle("fail",!s),a.innerHTML="",a.appendChild(t)}#i(t,s){s instanceof Error&&(t+=`
|
|
410
|
+
${s.message}`,console.error(s));let e=document.createElement("li");e.classList.add("error","process-error"),e.part.add("error","process-error");let r=document.createElement("code");r.classList.add("code","process-error-code"),r.part.add("code","process-error-code");let a=document.createElement("pre");a.classList.add("pre","process-error-pre"),a.part.add("pre","process-error-pre"),a.textContent=t,r.append(a),e.append(r),this.getElement("tests").append(e)}#d(t){if(t=="ordered"){let s=this.shadowRoot.querySelector("ul");if(s==null)return;let e=this.shadowRoot?.querySelectorAll("li"),r=document.createElement("ol");e!=null&&r.append(...e),r.id="tests",s.replaceWith(r)}else{let s=this.shadowRoot.querySelector("ol");if(s==null)return;let e=this.shadowRoot?.querySelectorAll("li"),r=document.createElement("ul");r.id="tests",e!=null&&r.append(...e),s.replaceWith(r)}}static observedAttributes=["in-order"];attributeChangedCallback(t,s,e){t=="in-order"&&(e==null?this.#d("unordered"):this.#d("ordered"))}};function C(){let d=new Uint8Array(20);crypto.getRandomValues(d);let t=[].slice.apply(d).map(function(e){return String.fromCharCode(e)}).join("");return btoa(t).replace(/\//g,"_").replace(/\+/g,"-").replace(/=/g,"")}customElements.get(B)==null&&customElements.define(B,S);export{k as AFTERALL,x as AFTEREACH,y as BEFOREALL,v as BEFOREEACH,N as CodeTestEventType,L as CodeTests,S as CodeTestsElement,I as expect,U as prompt};
|