@atlaskit/top-layer 0.2.0 → 0.4.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.
@@ -0,0 +1,92 @@
1
+ "use strict";
2
+
3
+ var _matchers = require("@testing-library/jest-dom/matchers");
4
+ /* eslint-disable compat/compat */
5
+ /* global expect */
6
+ /**
7
+ * Patches `toBeVisible()` to understand popover and dialog visibility.
8
+ *
9
+ * jest-dom's `toBeVisible()` does not know about the Popover API. In real
10
+ * browsers the UA stylesheet hides `[popover]` with `display: none`, but
11
+ * jsdom has no such stylesheet — so closed popovers incorrectly appear visible.
12
+ *
13
+ * Raw jest-dom results in jsdom:
14
+ *
15
+ * closed popover / child → true (WRONG — should be false)
16
+ * open popover / child → true (correct)
17
+ * closed dialog / child → false (correct — jsdom natively hides <dialog>)
18
+ * open dialog / child → true (correct)
19
+ *
20
+ * We correct both false positives and false negatives:
21
+ * 1. Delegate to the original first.
22
+ * 2. If it says "visible" → check for closed top-layer hosts (false positive).
23
+ * 3. If it says "not visible" → check for open top-layer hosts (false negative).
24
+ *
25
+ * Must load via `setupFilesAfterEnv` (after jest-dom registers its matchers).
26
+ * Polyfills live in `./polyfill` (loaded via `setupFiles`).
27
+ */
28
+
29
+ // Delegating to jest-dom's matcher; types come from @testing-library/jest-dom.
30
+ // eslint-disable-next-line import/no-extraneous-dependencies
31
+
32
+ /**
33
+ * Finds the nearest popover or dialog ancestor (including the element itself)
34
+ * and returns its state: 'open', 'closed', or null if no top-layer ancestor exists.
35
+ *
36
+ * Checking the *nearest* ancestor handles nesting correctly:
37
+ * a closed popover inside an open popover → nearest is closed → not visible.
38
+ */
39
+ function nearestTopLayerState(element) {
40
+ if (!element) {
41
+ return null;
42
+ }
43
+ if (element.hasAttribute('popover')) {
44
+ return element.hasAttribute('data-popover-open') ? 'open' : 'closed';
45
+ }
46
+ if (element.nodeName === 'DIALOG') {
47
+ return element.hasAttribute('open') ? 'open' : 'closed';
48
+ }
49
+ return nearestTopLayerState(element.parentElement);
50
+ }
51
+ function getNearestTopLayerType(element) {
52
+ if (!element.closest) {
53
+ return 'top-layer element';
54
+ }
55
+ if (element.closest('[popover]')) {
56
+ return 'popover';
57
+ }
58
+ return 'dialog';
59
+ }
60
+
61
+ // `expect` is registered globally by jest before this file runs.
62
+
63
+ expect.extend({
64
+ toBeVisible: function toBeVisible(element) {
65
+ var originalResult = _matchers.toBeVisible.call(this, element);
66
+ var topLayerState = nearestTopLayerState(element);
67
+ if (originalResult.pass) {
68
+ // Original says visible — override if nearest top-layer host is closed.
69
+ if (topLayerState === 'closed') {
70
+ return {
71
+ pass: false,
72
+ message: function message() {
73
+ return "expected element to be visible, but it is inside a closed ".concat(getNearestTopLayerType(element));
74
+ }
75
+ };
76
+ }
77
+ return originalResult;
78
+ }
79
+
80
+ // Original says not visible — override if nearest top-layer host is open.
81
+ // jsdom may hide [popover] elements even when they are logically open.
82
+ if (topLayerState === 'open') {
83
+ return {
84
+ pass: true,
85
+ message: function message() {
86
+ return "expected element not to be visible, but it is inside an open ".concat(getNearestTopLayerType(element));
87
+ }
88
+ };
89
+ }
90
+ return originalResult;
91
+ }
92
+ });