@cronn/element-snapshot 0.6.0 → 0.8.0

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.d.ts CHANGED
@@ -18,6 +18,10 @@ interface ButtonAttributes extends DisableableAttributes {
18
18
  }
19
19
  type PressedValue = true | "mixed";
20
20
 
21
+ interface DiscribableAttributes {
22
+ description?: string;
23
+ }
24
+
21
25
  interface InputSnapshot extends GenericElementSnapshot<CommonInputRole, InputAttributes> {
22
26
  }
23
27
  type InputRole = "button" | "combobox" | CommonInputRole;
@@ -26,8 +30,7 @@ interface InputAttributes extends CommonInputAttributes {
26
30
  value?: string;
27
31
  checked?: boolean;
28
32
  }
29
- interface CommonInputAttributes extends InputStateAttributes {
30
- description?: string;
33
+ interface CommonInputAttributes extends InputStateAttributes, DiscribableAttributes {
31
34
  placeholder?: string;
32
35
  }
33
36
 
@@ -46,13 +49,16 @@ interface OptionAttributes {
46
49
  interface DialogSnapshot extends GenericElementSnapshot<DialogRole, DialogAttributes> {
47
50
  }
48
51
  type DialogRole = "dialog" | "alertdialog";
49
- interface DialogAttributes {
50
- description?: string;
52
+ interface DialogAttributes extends DiscribableAttributes {
51
53
  modal?: boolean;
52
54
  }
53
55
 
54
56
  interface GroupSnapshot extends GenericElementSnapshot<"group"> {
55
57
  }
58
+ interface RadiogroupSnapshot extends GenericElementSnapshot<"radiogroup", RadiogroupAttributes> {
59
+ }
60
+ interface RadiogroupAttributes extends DiscribableAttributes {
61
+ }
56
62
 
57
63
  interface HeadingSnapshot extends GenericElementSnapshot<"heading", HeadingAttributes> {
58
64
  }
@@ -71,6 +77,12 @@ interface MenuitemSnapshot extends GenericElementSnapshot<"menuitem", MenuitemAt
71
77
  interface MenuitemAttributes extends LinkAttributes, DisableableAttributes {
72
78
  }
73
79
 
80
+ interface ProgressbarSnapshot extends GenericElementSnapshot<"progressbar", ProgressbarAttributes> {
81
+ }
82
+ interface ProgressbarAttributes {
83
+ value?: number;
84
+ }
85
+
74
86
  interface TabSnapshot extends GenericElementSnapshot<"tab", TabAttributes> {
75
87
  }
76
88
  interface TabAttributes extends DisableableAttributes, SelectableAttributes {
@@ -91,9 +103,9 @@ interface TextSnapshot {
91
103
 
92
104
  type SnapshotTargetElement = HTMLElement | SVGElement;
93
105
  type NodeRole = ElementRole | "text";
94
- type ElementRole = "heading" | "link" | "button" | "option" | "tab" | "menuitem" | "columnheader" | "group" | ContainerRole | InputRole | DialogRole;
106
+ type ElementRole = "heading" | "link" | "button" | "option" | "tab" | "menuitem" | "columnheader" | "group" | "radiogroup" | "progressbar" | ContainerRole | InputRole | DialogRole;
95
107
  type NodeSnapshot = ElementSnapshot | TextSnapshot;
96
- type ElementSnapshot = ContainerSnapshot | HeadingSnapshot | LinkSnapshot | ButtonSnapshot | InputSnapshot | ComboboxSnapshot | OptionSnapshot | DialogSnapshot | TabSnapshot | MenuitemSnapshot | ColumnheaderSnapshot | GroupSnapshot;
108
+ type ElementSnapshot = ContainerSnapshot | HeadingSnapshot | LinkSnapshot | ButtonSnapshot | InputSnapshot | ComboboxSnapshot | OptionSnapshot | DialogSnapshot | TabSnapshot | MenuitemSnapshot | ColumnheaderSnapshot | GroupSnapshot | RadiogroupSnapshot | ProgressbarSnapshot;
97
109
  interface GenericElementSnapshot<TRole extends NodeRole = NodeRole, TAttributes = unknown> {
98
110
  role: TRole;
99
111
  name?: string;
@@ -126,6 +126,11 @@
126
126
  }
127
127
 
128
128
  // src/snapshots/description.ts
129
+ function discribableAttributes(element) {
130
+ return {
131
+ description: resolveDescription(element)
132
+ };
133
+ }
129
134
  function resolveDescription(element) {
130
135
  const describedByElement = resolveElementReference(
131
136
  element,
@@ -236,7 +241,8 @@
236
241
  tr: "row",
237
242
  th: resolveTableHeaderCellRole,
238
243
  td: "cell",
239
- fieldset: "group"
244
+ fieldset: "group",
245
+ progress: "progressbar"
240
246
  };
241
247
  var CONTEXT_DEPENDENT_ROLES = {
242
248
  banner: isTopLevelBanner,
@@ -427,13 +433,14 @@
427
433
  }
428
434
  function snapshotCommonInputAttributes(element, isEmpty) {
429
435
  return {
430
- description: resolveDescription(element),
436
+ ...discribableAttributes(element),
431
437
  placeholder: isEmpty ? resolvePlaceholder(element) : void 0,
432
438
  ...inputStateAttributes(element)
433
439
  };
434
440
  }
441
+ var CHECKED_INPUT_TYPES = /* @__PURE__ */ new Set(["checkbox", "radio"]);
435
442
  function resolveInputValue(element) {
436
- if (element instanceof HTMLInputElement && element.type === "checkbox") {
443
+ if (element instanceof HTMLInputElement && CHECKED_INPUT_TYPES.has(element.type)) {
437
444
  return void 0;
438
445
  }
439
446
  return stringAttribute(element.value);
@@ -476,7 +483,7 @@
476
483
  return null;
477
484
  }
478
485
  const options = snapshotOptions(element);
479
- const value = resolveValue(element, options);
486
+ const value = resolveValue(element);
480
487
  return {
481
488
  role: "combobox",
482
489
  name: resolveInputLabel(element),
@@ -496,15 +503,23 @@
496
503
  }
497
504
  return false;
498
505
  }
499
- function resolveValue(element, options) {
500
- const selectedOptions = options.filter((option) => option.attributes?.selected === true).map((option) => option.name).filter((optionName) => optionName !== void 0);
501
- if (selectedOptions.length === 0) {
502
- return element instanceof HTMLInputElement ? resolveInputValue(element) : void 0;
506
+ function resolveValue(element) {
507
+ if (element instanceof HTMLButtonElement) {
508
+ return snapshotTextContent(element);
503
509
  }
504
- if (selectedOptions.length === 1) {
505
- return selectedOptions.at(0);
510
+ if (element instanceof HTMLInputElement) {
511
+ return resolveInputValue(element);
506
512
  }
507
- return selectedOptions;
513
+ const selectedLabels = Array.from(element.selectedOptions).map(
514
+ (option) => option.label
515
+ );
516
+ if (selectedLabels.length === 0) {
517
+ return void 0;
518
+ }
519
+ if (selectedLabels.length === 1) {
520
+ return selectedLabels.at(0);
521
+ }
522
+ return selectedLabels;
508
523
  }
509
524
  function snapshotOptions(element) {
510
525
  return resolveOptions(element).map(snapshotOption).filter((childSnapshot) => childSnapshot !== null);
@@ -513,10 +528,6 @@
513
528
  if (element instanceof HTMLSelectElement) {
514
529
  return Array.from(element.options);
515
530
  }
516
- const ariaExpanded = element.ariaExpanded === "true";
517
- if (!ariaExpanded) {
518
- return [];
519
- }
520
531
  const controlledElement = resolveElementReference(element, "aria-controls");
521
532
  if (controlledElement === null) {
522
533
  return [];
@@ -544,7 +555,7 @@
544
555
  role,
545
556
  name: resolveAccessibleName(element, false),
546
557
  attributes: {
547
- description: resolveDescription(element),
558
+ ...discribableAttributes(element),
548
559
  modal: booleanAttribute(element.ariaModal)
549
560
  },
550
561
  children: snapshotChildren(element)
@@ -566,6 +577,14 @@
566
577
  }
567
578
  return resolveAccessibleName(element, false);
568
579
  }
580
+ function snapshotRadiogroup(element) {
581
+ return {
582
+ role: "radiogroup",
583
+ name: resolveAccessibleName(element, false),
584
+ attributes: discribableAttributes(element),
585
+ children: snapshotChildren(element)
586
+ };
587
+ }
569
588
 
570
589
  // src/snapshots/heading.ts
571
590
  var LEVEL_REGEXP = /^H([1-6])$/;
@@ -613,6 +632,24 @@
613
632
  };
614
633
  }
615
634
 
635
+ // src/snapshots/progressbar.ts
636
+ function snapshotProgressbar(element) {
637
+ return {
638
+ role: "progressbar",
639
+ name: resolveAccessibleName(element, false),
640
+ attributes: {
641
+ value: resolveValue2(element)
642
+ },
643
+ children: snapshotChildren(element)
644
+ };
645
+ }
646
+ function resolveValue2(element) {
647
+ if (element instanceof HTMLProgressElement) {
648
+ return element.value;
649
+ }
650
+ return numericAttribute(element.ariaValueNow);
651
+ }
652
+
616
653
  // src/snapshots/tab.ts
617
654
  function snapshotTab(element) {
618
655
  return {
@@ -657,7 +694,9 @@
657
694
  tab: snapshotTab,
658
695
  menuitem: snapshotMenuitem,
659
696
  columnheader: snapshotColumnheader,
660
- group: snapshotGroup
697
+ group: snapshotGroup,
698
+ radiogroup: snapshotRadiogroup,
699
+ progressbar: snapshotProgressbar
661
700
  };
662
701
  function snapshotElement(element) {
663
702
  return snapshotNodeRecursive(element);
package/dist/types.d.ts CHANGED
@@ -18,6 +18,10 @@ interface ButtonAttributes extends DisableableAttributes {
18
18
  }
19
19
  type PressedValue = true | "mixed";
20
20
 
21
+ interface DiscribableAttributes {
22
+ description?: string;
23
+ }
24
+
21
25
  interface InputSnapshot extends GenericElementSnapshot<CommonInputRole, InputAttributes> {
22
26
  }
23
27
  type InputRole = "button" | "combobox" | CommonInputRole;
@@ -26,8 +30,7 @@ interface InputAttributes extends CommonInputAttributes {
26
30
  value?: string;
27
31
  checked?: boolean;
28
32
  }
29
- interface CommonInputAttributes extends InputStateAttributes {
30
- description?: string;
33
+ interface CommonInputAttributes extends InputStateAttributes, DiscribableAttributes {
31
34
  placeholder?: string;
32
35
  }
33
36
 
@@ -51,13 +54,16 @@ type ContainerRole = SetValues<typeof CONTAINER_ROLES>;
51
54
  interface DialogSnapshot extends GenericElementSnapshot<DialogRole, DialogAttributes> {
52
55
  }
53
56
  type DialogRole = "dialog" | "alertdialog";
54
- interface DialogAttributes {
55
- description?: string;
57
+ interface DialogAttributes extends DiscribableAttributes {
56
58
  modal?: boolean;
57
59
  }
58
60
 
59
61
  interface GroupSnapshot extends GenericElementSnapshot<"group"> {
60
62
  }
63
+ interface RadiogroupSnapshot extends GenericElementSnapshot<"radiogroup", RadiogroupAttributes> {
64
+ }
65
+ interface RadiogroupAttributes extends DiscribableAttributes {
66
+ }
61
67
 
62
68
  interface HeadingSnapshot extends GenericElementSnapshot<"heading", HeadingAttributes> {
63
69
  }
@@ -76,6 +82,12 @@ interface MenuitemSnapshot extends GenericElementSnapshot<"menuitem", MenuitemAt
76
82
  interface MenuitemAttributes extends LinkAttributes, DisableableAttributes {
77
83
  }
78
84
 
85
+ interface ProgressbarSnapshot extends GenericElementSnapshot<"progressbar", ProgressbarAttributes> {
86
+ }
87
+ interface ProgressbarAttributes {
88
+ value?: number;
89
+ }
90
+
79
91
  interface TabSnapshot extends GenericElementSnapshot<"tab", TabAttributes> {
80
92
  }
81
93
  interface TabAttributes extends DisableableAttributes, SelectableAttributes {
@@ -95,9 +107,9 @@ interface TextSnapshot {
95
107
  }
96
108
 
97
109
  type NodeRole = ElementRole | "text";
98
- type ElementRole = "heading" | "link" | "button" | "option" | "tab" | "menuitem" | "columnheader" | "group" | ContainerRole | InputRole | DialogRole;
110
+ type ElementRole = "heading" | "link" | "button" | "option" | "tab" | "menuitem" | "columnheader" | "group" | "radiogroup" | "progressbar" | ContainerRole | InputRole | DialogRole;
99
111
  type NodeSnapshot = ElementSnapshot | TextSnapshot;
100
- type ElementSnapshot = ContainerSnapshot | HeadingSnapshot | LinkSnapshot | ButtonSnapshot | InputSnapshot | ComboboxSnapshot | OptionSnapshot | DialogSnapshot | TabSnapshot | MenuitemSnapshot | ColumnheaderSnapshot | GroupSnapshot;
112
+ type ElementSnapshot = ContainerSnapshot | HeadingSnapshot | LinkSnapshot | ButtonSnapshot | InputSnapshot | ComboboxSnapshot | OptionSnapshot | DialogSnapshot | TabSnapshot | MenuitemSnapshot | ColumnheaderSnapshot | GroupSnapshot | RadiogroupSnapshot | ProgressbarSnapshot;
101
113
  interface GenericElementSnapshot<TRole extends NodeRole = NodeRole, TAttributes = unknown> {
102
114
  role: TRole;
103
115
  name?: string;
@@ -106,4 +118,4 @@ interface GenericElementSnapshot<TRole extends NodeRole = NodeRole, TAttributes
106
118
  }
107
119
  type SetValues<TSet> = TSet extends Set<infer TValue> ? TValue : never;
108
120
 
109
- export type { ButtonSnapshot, ColumnheaderSnapshot, ComboboxSnapshot, ContainerSnapshot, DialogSnapshot, ElementRole, ElementSnapshot, GroupSnapshot, HeadingSnapshot, InputSnapshot, LinkSnapshot, MenuitemSnapshot, NodeRole, NodeSnapshot, OptionSnapshot, TabSnapshot, TextSnapshot };
121
+ export type { ButtonSnapshot, ColumnheaderSnapshot, ComboboxSnapshot, ContainerSnapshot, DialogSnapshot, ElementRole, ElementSnapshot, GroupSnapshot, HeadingSnapshot, InputSnapshot, LinkSnapshot, MenuitemSnapshot, NodeRole, NodeSnapshot, OptionSnapshot, ProgressbarSnapshot, TabSnapshot, TextSnapshot };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cronn/element-snapshot",
3
- "version": "0.6.0",
3
+ "version": "0.8.0",
4
4
  "description": "Element snapshots for Playwright",
5
5
  "author": "cronn",
6
6
  "license": "Apache-2.0",