@primer/components 0.0.0-20219421304 → 0.0.0-202194215436
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/CHANGELOG.md +4 -8
- package/dist/browser.esm.js +768 -714
- package/dist/browser.esm.js.map +1 -1
- package/dist/browser.umd.js +409 -355
- package/dist/browser.umd.js.map +1 -1
- package/lib/ActionList/List.d.ts +1 -1
- package/lib/Token/LabelToken.d.ts +14 -0
- package/lib/Token/LabelToken.js +141 -0
- package/lib/Token/Token.d.ts +14 -0
- package/lib/Token/Token.js +76 -0
- package/lib/Token/TokenBase.d.ts +16 -0
- package/lib/Token/TokenBase.js +91 -0
- package/lib/Token/TokenProfile.d.ts +7 -0
- package/lib/Token/TokenProfile.js +50 -0
- package/lib/Token/_RemoveTokenButton.d.ts +9 -0
- package/lib/Token/_RemoveTokenButton.js +73 -0
- package/lib/Token/index.d.ts +3 -0
- package/lib/Token/index.js +31 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +20 -0
- package/lib/sx.js +15 -0
- package/lib/theme-preval.js +5677 -0
- package/lib/theme.js +11 -0
- package/lib/utils/deprecate.js +100 -0
- package/lib/utils/isNumeric.js +11 -0
- package/lib/utils/iterateFocusableElements.js +113 -0
- package/lib/utils/ssr.js +19 -0
- package/lib/utils/test-deprecations.js +23 -0
- package/lib/utils/test-helpers.js +9 -0
- package/lib/utils/test-matchers.js +119 -0
- package/lib/utils/testing.js +273 -0
- package/lib/utils/theme.js +68 -0
- package/lib/utils/types.js +1 -0
- package/lib/utils/uniqueId.js +12 -0
- package/lib/utils/userAgent.js +15 -0
- package/lib-esm/ActionList/List.d.ts +1 -1
- package/lib-esm/Token/LabelToken.d.ts +14 -0
- package/lib-esm/Token/LabelToken.js +121 -0
- package/lib-esm/Token/Token.d.ts +14 -0
- package/lib-esm/Token/Token.js +57 -0
- package/lib-esm/Token/TokenBase.d.ts +16 -0
- package/lib-esm/Token/TokenBase.js +71 -0
- package/lib-esm/Token/TokenProfile.d.ts +7 -0
- package/lib-esm/Token/TokenProfile.js +29 -0
- package/lib-esm/Token/_RemoveTokenButton.d.ts +9 -0
- package/lib-esm/Token/_RemoveTokenButton.js +55 -0
- package/lib-esm/Token/index.d.ts +3 -0
- package/lib-esm/Token/index.js +3 -0
- package/lib-esm/index.d.ts +1 -0
- package/lib-esm/index.js +1 -0
- package/lib-esm/sx.js +5 -0
- package/lib-esm/theme-preval.js +5677 -0
- package/lib-esm/theme.js +2 -0
- package/lib-esm/utils/deprecate.js +91 -0
- package/lib-esm/utils/isNumeric.js +4 -0
- package/lib-esm/utils/iterateFocusableElements.js +102 -0
- package/lib-esm/utils/ssr.js +1 -0
- package/lib-esm/utils/test-deprecations.js +17 -0
- package/lib-esm/utils/test-helpers.js +7 -0
- package/lib-esm/utils/test-matchers.js +110 -0
- package/lib-esm/utils/testing.js +222 -0
- package/lib-esm/utils/theme.js +66 -0
- package/lib-esm/utils/types.js +1 -0
- package/lib-esm/utils/uniqueId.js +5 -0
- package/lib-esm/utils/userAgent.js +8 -0
- package/package.json +4 -2
package/lib/theme.js
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.Deprecations = exports.useDeprecation = exports.deprecate = void 0;
|
7
|
+
|
8
|
+
var _react = require("react");
|
9
|
+
|
10
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
11
|
+
const noop = () => {}; // eslint-disable-next-line import/no-mutable-exports
|
12
|
+
|
13
|
+
|
14
|
+
let deprecate = noop;
|
15
|
+
exports.deprecate = deprecate;
|
16
|
+
|
17
|
+
if (process.env.NODE_ENV !== 'production') {
|
18
|
+
exports.deprecate = deprecate = ({
|
19
|
+
name,
|
20
|
+
message,
|
21
|
+
version
|
22
|
+
}) => {
|
23
|
+
Deprecations.deprecate({
|
24
|
+
name,
|
25
|
+
message,
|
26
|
+
version
|
27
|
+
});
|
28
|
+
};
|
29
|
+
}
|
30
|
+
|
31
|
+
// eslint-disable-next-line import/no-mutable-exports
|
32
|
+
let useDeprecation = null;
|
33
|
+
exports.useDeprecation = useDeprecation;
|
34
|
+
|
35
|
+
if (process.env.NODE_ENV !== 'production') {
|
36
|
+
exports.useDeprecation = useDeprecation = ({
|
37
|
+
name,
|
38
|
+
message,
|
39
|
+
version
|
40
|
+
}) => {
|
41
|
+
const ref = (0, _react.useRef)(false);
|
42
|
+
const logDeprecation = (0, _react.useCallback)(() => {
|
43
|
+
if (!ref.current) {
|
44
|
+
ref.current = true;
|
45
|
+
deprecate({
|
46
|
+
name,
|
47
|
+
message,
|
48
|
+
version
|
49
|
+
});
|
50
|
+
}
|
51
|
+
}, [name, message, version]);
|
52
|
+
return logDeprecation;
|
53
|
+
};
|
54
|
+
} else {
|
55
|
+
exports.useDeprecation = useDeprecation = () => {
|
56
|
+
return noop;
|
57
|
+
};
|
58
|
+
}
|
59
|
+
|
60
|
+
class Deprecations {
|
61
|
+
static instance = null;
|
62
|
+
|
63
|
+
static get() {
|
64
|
+
if (!Deprecations.instance) {
|
65
|
+
Deprecations.instance = new Deprecations();
|
66
|
+
}
|
67
|
+
|
68
|
+
return Deprecations.instance;
|
69
|
+
}
|
70
|
+
|
71
|
+
constructor() {
|
72
|
+
this.deprecations = [];
|
73
|
+
}
|
74
|
+
|
75
|
+
static deprecate({
|
76
|
+
name,
|
77
|
+
message,
|
78
|
+
version
|
79
|
+
}) {
|
80
|
+
const msg = `WARNING! ${name} is deprecated and will be removed in version ${version}. ${message}`; // eslint-disable-next-line no-console
|
81
|
+
|
82
|
+
console.warn(msg);
|
83
|
+
this.get().deprecations.push({
|
84
|
+
name,
|
85
|
+
message,
|
86
|
+
version
|
87
|
+
});
|
88
|
+
}
|
89
|
+
|
90
|
+
static getDeprecations() {
|
91
|
+
return this.get().deprecations;
|
92
|
+
}
|
93
|
+
|
94
|
+
static clearDeprecations() {
|
95
|
+
this.get().deprecations.length = 0;
|
96
|
+
}
|
97
|
+
|
98
|
+
}
|
99
|
+
|
100
|
+
exports.Deprecations = Deprecations;
|
@@ -0,0 +1,11 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.default = isNumeric;
|
7
|
+
|
8
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
9
|
+
function isNumeric(n) {
|
10
|
+
return !isNaN(parseFloat(n)) && isFinite(n);
|
11
|
+
}
|
@@ -0,0 +1,113 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.iterateFocusableElements = iterateFocusableElements;
|
7
|
+
exports.isFocusable = isFocusable;
|
8
|
+
exports.isTabbable = isTabbable;
|
9
|
+
|
10
|
+
/**
|
11
|
+
* Options to the focusable elements iterator
|
12
|
+
*/
|
13
|
+
|
14
|
+
/**
|
15
|
+
* Returns an iterator over all of the focusable elements within `container`.
|
16
|
+
* Note: If `container` is itself focusable it will be included in the results.
|
17
|
+
* @param container The container over which to find focusable elements.
|
18
|
+
* @param reverse If true, iterate backwards through focusable elements.
|
19
|
+
*/
|
20
|
+
function* iterateFocusableElements(container, options = {}) {
|
21
|
+
var _options$strict, _options$onlyTabbable;
|
22
|
+
|
23
|
+
const strict = (_options$strict = options.strict) !== null && _options$strict !== void 0 ? _options$strict : false;
|
24
|
+
const acceptFn = ((_options$onlyTabbable = options.onlyTabbable) !== null && _options$onlyTabbable !== void 0 ? _options$onlyTabbable : false) ? isTabbable : isFocusable;
|
25
|
+
const walker = document.createTreeWalker(container, NodeFilter.SHOW_ELEMENT, {
|
26
|
+
acceptNode: node => node instanceof HTMLElement && acceptFn(node, strict) ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP
|
27
|
+
});
|
28
|
+
let nextNode = null; // Allow the container to participate
|
29
|
+
|
30
|
+
if (!options.reverse && acceptFn(container, strict)) {
|
31
|
+
yield container;
|
32
|
+
} // If iterating in reverse, continue traversing down into the last child until we reach
|
33
|
+
// a leaf DOM node
|
34
|
+
|
35
|
+
|
36
|
+
if (options.reverse) {
|
37
|
+
let lastChild = walker.lastChild();
|
38
|
+
|
39
|
+
while (lastChild) {
|
40
|
+
nextNode = lastChild;
|
41
|
+
lastChild = walker.lastChild();
|
42
|
+
}
|
43
|
+
} else {
|
44
|
+
nextNode = walker.firstChild();
|
45
|
+
}
|
46
|
+
|
47
|
+
while (nextNode instanceof HTMLElement) {
|
48
|
+
yield nextNode;
|
49
|
+
nextNode = options.reverse ? walker.previousNode() : walker.nextNode();
|
50
|
+
} // Allow the container to participate (in reverse)
|
51
|
+
|
52
|
+
|
53
|
+
if (options.reverse && acceptFn(container, strict)) {
|
54
|
+
yield container;
|
55
|
+
}
|
56
|
+
|
57
|
+
return undefined;
|
58
|
+
}
|
59
|
+
/**
|
60
|
+
* Determines whether the given element is focusable. If `strict` is true, we may
|
61
|
+
* perform additional checks that require a reflow (less performant).
|
62
|
+
* @param elem
|
63
|
+
* @param strict
|
64
|
+
*/
|
65
|
+
|
66
|
+
|
67
|
+
function isFocusable(elem, strict = false) {
|
68
|
+
// Certain conditions cause an element to never be focusable, even if they have tabindex="0"
|
69
|
+
const disabledAttrInert = ['BUTTON', 'INPUT', 'SELECT', 'TEXTAREA', 'OPTGROUP', 'OPTION', 'FIELDSET'].includes(elem.tagName) && elem.disabled;
|
70
|
+
const hiddenInert = elem.hidden;
|
71
|
+
const hiddenInputInert = elem instanceof HTMLInputElement && elem.type === 'hidden';
|
72
|
+
|
73
|
+
if (disabledAttrInert || hiddenInert || hiddenInputInert) {
|
74
|
+
return false;
|
75
|
+
} // Each of the conditions checked below require a reflow, thus are gated by the `strict`
|
76
|
+
// argument. If any are true, the element is not focusable, even if tabindex is set.
|
77
|
+
|
78
|
+
|
79
|
+
if (strict) {
|
80
|
+
const sizeInert = elem.offsetWidth === 0 || elem.offsetHeight === 0;
|
81
|
+
const visibilityInert = ['hidden', 'collapse'].includes(getComputedStyle(elem).visibility);
|
82
|
+
const clientRectsInert = elem.getClientRects().length === 0;
|
83
|
+
|
84
|
+
if (sizeInert || visibilityInert || clientRectsInert) {
|
85
|
+
return false;
|
86
|
+
}
|
87
|
+
} // Any element with `tabindex` explicitly set can be focusable, even if it's set to "-1"
|
88
|
+
|
89
|
+
|
90
|
+
if (elem.getAttribute('tabindex') != null) {
|
91
|
+
return true;
|
92
|
+
} // One last way `elem.tabIndex` can be wrong.
|
93
|
+
|
94
|
+
|
95
|
+
if (elem instanceof HTMLAnchorElement && elem.getAttribute('href') == null) {
|
96
|
+
return false;
|
97
|
+
}
|
98
|
+
|
99
|
+
return elem.tabIndex !== -1;
|
100
|
+
}
|
101
|
+
/**
|
102
|
+
* Determines whether the given element is tabbable. If `strict` is true, we may
|
103
|
+
* perform additional checks that require a reflow (less performant). This check
|
104
|
+
* ensures that the element is focusable and that its tabindex is not explicitly
|
105
|
+
* set to "-1" (which makes it focusable, but removes it from the tab order).
|
106
|
+
* @param elem
|
107
|
+
* @param strict
|
108
|
+
*/
|
109
|
+
|
110
|
+
|
111
|
+
function isTabbable(elem, strict = false) {
|
112
|
+
return isFocusable(elem, strict) && elem.getAttribute('tabindex') !== '-1';
|
113
|
+
}
|
package/lib/utils/ssr.js
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
Object.defineProperty(exports, "SSRProvider", {
|
7
|
+
enumerable: true,
|
8
|
+
get: function () {
|
9
|
+
return _ssr.SSRProvider;
|
10
|
+
}
|
11
|
+
});
|
12
|
+
Object.defineProperty(exports, "useSSRSafeId", {
|
13
|
+
enumerable: true,
|
14
|
+
get: function () {
|
15
|
+
return _ssr.useSSRSafeId;
|
16
|
+
}
|
17
|
+
});
|
18
|
+
|
19
|
+
var _ssr = require("@react-aria/ssr");
|
@@ -0,0 +1,23 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
var _semver = _interopRequireDefault(require("semver"));
|
4
|
+
|
5
|
+
var _deprecate = require("./deprecate");
|
6
|
+
|
7
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
8
|
+
|
9
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
10
|
+
const ourVersion = require('../../package.json').version;
|
11
|
+
|
12
|
+
beforeEach(() => {
|
13
|
+
_deprecate.Deprecations.clearDeprecations();
|
14
|
+
});
|
15
|
+
afterEach(() => {
|
16
|
+
const deprecations = _deprecate.Deprecations.getDeprecations();
|
17
|
+
|
18
|
+
for (const dep of deprecations) {
|
19
|
+
if (_semver.default.gte(ourVersion, dep.version)) {
|
20
|
+
throw new Error(`Found a deprecation that should be removed in ${dep.version}`);
|
21
|
+
}
|
22
|
+
}
|
23
|
+
});
|
@@ -0,0 +1,119 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
require("jest-styled-components");
|
4
|
+
|
5
|
+
var _serializer = require("jest-styled-components/serializer");
|
6
|
+
|
7
|
+
var _react = _interopRequireDefault(require("react"));
|
8
|
+
|
9
|
+
var _testing = require("./testing");
|
10
|
+
|
11
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
12
|
+
|
13
|
+
expect.addSnapshotSerializer(_serializer.styleSheetSerializer);
|
14
|
+
|
15
|
+
const stringify = d => JSON.stringify(d, null, ' ');
|
16
|
+
|
17
|
+
expect.extend({
|
18
|
+
toMatchKeys(obj, values) {
|
19
|
+
return {
|
20
|
+
pass: Object.keys(values).every(key => this.equals(obj[key], values[key])),
|
21
|
+
message: () => `Expected ${stringify(obj)} to have matching keys: ${stringify(values)}`
|
22
|
+
};
|
23
|
+
},
|
24
|
+
|
25
|
+
toHaveClass(node, klass) {
|
26
|
+
const classes = (0, _testing.getClasses)(node);
|
27
|
+
const pass = classes.includes(klass);
|
28
|
+
return {
|
29
|
+
pass,
|
30
|
+
message: () => `expected ${stringify(classes)} to include: ${stringify(klass)}`
|
31
|
+
};
|
32
|
+
},
|
33
|
+
|
34
|
+
toHaveClasses(node, klasses, only = false) {
|
35
|
+
const classes = (0, _testing.getClasses)(node);
|
36
|
+
const pass = only ? this.equals(classes.sort(), klasses.sort()) : klasses.every(klass => classes.includes(klass));
|
37
|
+
return {
|
38
|
+
pass,
|
39
|
+
message: () => `expected ${stringify(classes)} to include: ${stringify(klasses)}`
|
40
|
+
};
|
41
|
+
},
|
42
|
+
|
43
|
+
toImplementSxBehavior(element) {
|
44
|
+
const mediaKey = '@media (max-width:123px)';
|
45
|
+
const sxPropValue = {
|
46
|
+
[mediaKey]: {
|
47
|
+
color: 'red.5'
|
48
|
+
}
|
49
|
+
};
|
50
|
+
|
51
|
+
const elem = /*#__PURE__*/_react.default.cloneElement(element, {
|
52
|
+
sx: sxPropValue
|
53
|
+
});
|
54
|
+
|
55
|
+
function checkStylesDeep(rendered) {
|
56
|
+
const className = rendered.props.className;
|
57
|
+
const styles = (0, _testing.getComputedStyles)(className);
|
58
|
+
const mediaStyles = styles[mediaKey];
|
59
|
+
|
60
|
+
if (mediaStyles && mediaStyles.color) {
|
61
|
+
return true;
|
62
|
+
} else if (rendered.children) {
|
63
|
+
return rendered.children.some(child => checkStylesDeep(child));
|
64
|
+
} else {
|
65
|
+
return false;
|
66
|
+
}
|
67
|
+
}
|
68
|
+
|
69
|
+
return {
|
70
|
+
pass: checkStylesDeep((0, _testing.render)(elem)),
|
71
|
+
message: () => 'sx prop values did not change styles of component nor of any sub-components'
|
72
|
+
};
|
73
|
+
},
|
74
|
+
|
75
|
+
toSetExports(mod, expectedExports) {
|
76
|
+
if (!Object.keys(expectedExports).includes('default')) {
|
77
|
+
return {
|
78
|
+
pass: false,
|
79
|
+
message: () => "You must specify the module's default export"
|
80
|
+
};
|
81
|
+
}
|
82
|
+
|
83
|
+
const seen = new Set();
|
84
|
+
|
85
|
+
for (const exp of Object.keys(expectedExports)) {
|
86
|
+
seen.add(exp);
|
87
|
+
|
88
|
+
if (mod[exp] !== expectedExports[exp]) {
|
89
|
+
if (!mod[exp] && !expectedExports[exp]) {
|
90
|
+
continue;
|
91
|
+
}
|
92
|
+
|
93
|
+
return {
|
94
|
+
pass: false,
|
95
|
+
message: () => `Module exported a different value from key '${exp}' than expected`
|
96
|
+
};
|
97
|
+
}
|
98
|
+
}
|
99
|
+
|
100
|
+
for (const exp of Object.keys(mod)) {
|
101
|
+
if (seen.has(exp)) {
|
102
|
+
continue;
|
103
|
+
}
|
104
|
+
|
105
|
+
if (mod[exp] !== expectedExports[exp]) {
|
106
|
+
return {
|
107
|
+
pass: false,
|
108
|
+
message: () => `Module exported an unexpected value from key '${exp}'`
|
109
|
+
};
|
110
|
+
}
|
111
|
+
}
|
112
|
+
|
113
|
+
return {
|
114
|
+
pass: true,
|
115
|
+
message: () => ''
|
116
|
+
};
|
117
|
+
}
|
118
|
+
|
119
|
+
});
|
@@ -0,0 +1,273 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.mount = mount;
|
7
|
+
exports.render = render;
|
8
|
+
exports.renderRoot = renderRoot;
|
9
|
+
exports.renderClasses = renderClasses;
|
10
|
+
exports.rendersClass = rendersClass;
|
11
|
+
exports.px = px;
|
12
|
+
exports.percent = percent;
|
13
|
+
exports.renderStyles = renderStyles;
|
14
|
+
exports.getComputedStyles = getComputedStyles;
|
15
|
+
exports.getProps = getProps;
|
16
|
+
exports.getClassName = getClassName;
|
17
|
+
exports.getClasses = getClasses;
|
18
|
+
exports.loadCSS = loadCSS;
|
19
|
+
exports.unloadCSS = unloadCSS;
|
20
|
+
exports.behavesAsComponent = behavesAsComponent;
|
21
|
+
exports.checkExports = checkExports;
|
22
|
+
exports.COMPONENT_DISPLAY_NAME_REGEX = void 0;
|
23
|
+
|
24
|
+
var _react = _interopRequireDefault(require("react"));
|
25
|
+
|
26
|
+
var _util = require("util");
|
27
|
+
|
28
|
+
var _reactTestRenderer = _interopRequireDefault(require("react-test-renderer"));
|
29
|
+
|
30
|
+
var _enzyme = _interopRequireDefault(require("enzyme"));
|
31
|
+
|
32
|
+
var _enzymeAdapterReact = _interopRequireDefault(require("@wojtekmaj/enzyme-adapter-react-17"));
|
33
|
+
|
34
|
+
var _ = require("..");
|
35
|
+
|
36
|
+
var _theme = _interopRequireDefault(require("../theme"));
|
37
|
+
|
38
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
39
|
+
|
40
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
41
|
+
const readFile = (0, _util.promisify)(require('fs').readFile);
|
42
|
+
const COMPONENT_DISPLAY_NAME_REGEX = /^[A-Z][A-Za-z]+(\.[A-Z][A-Za-z]+)*$/;
|
43
|
+
exports.COMPONENT_DISPLAY_NAME_REGEX = COMPONENT_DISPLAY_NAME_REGEX;
|
44
|
+
|
45
|
+
_enzyme.default.configure({
|
46
|
+
adapter: new _enzymeAdapterReact.default()
|
47
|
+
});
|
48
|
+
|
49
|
+
function mount(component) {
|
50
|
+
return _enzyme.default.mount(component);
|
51
|
+
}
|
52
|
+
|
53
|
+
/**
|
54
|
+
* Render the component (a React.createElement() or JSX expression)
|
55
|
+
* into its intermediate object representation with 'type',
|
56
|
+
* 'props', and 'children' keys
|
57
|
+
*
|
58
|
+
* The returned object can be matched with expect().toEqual(), e.g.
|
59
|
+
*
|
60
|
+
* ```js
|
61
|
+
* expect(render(<Foo />)).toEqual(render(<div foo='bar' />))
|
62
|
+
* ```
|
63
|
+
*/
|
64
|
+
function render(component, theme = _theme.default) {
|
65
|
+
return _reactTestRenderer.default.create( /*#__PURE__*/_react.default.createElement(_.ThemeProvider, {
|
66
|
+
theme: theme
|
67
|
+
}, component)).toJSON();
|
68
|
+
}
|
69
|
+
/**
|
70
|
+
* Render the component (a React.createElement() or JSX expression)
|
71
|
+
* using react-test-renderer and return the root node
|
72
|
+
* ```
|
73
|
+
*/
|
74
|
+
|
75
|
+
|
76
|
+
function renderRoot(component) {
|
77
|
+
return _reactTestRenderer.default.create(component).root;
|
78
|
+
}
|
79
|
+
/**
|
80
|
+
* Get the HTML class names rendered by the component instance
|
81
|
+
* as an array.
|
82
|
+
*
|
83
|
+
* ```js
|
84
|
+
* expect(renderClasses(<div className='a b' />))
|
85
|
+
* .toEqual(['a', 'b'])
|
86
|
+
* ```
|
87
|
+
*/
|
88
|
+
|
89
|
+
|
90
|
+
function renderClasses(component) {
|
91
|
+
const {
|
92
|
+
props: {
|
93
|
+
className
|
94
|
+
}
|
95
|
+
} = render(component);
|
96
|
+
return className ? className.trim().split(' ') : [];
|
97
|
+
}
|
98
|
+
/**
|
99
|
+
* Returns true if a node renders with a single class.
|
100
|
+
*/
|
101
|
+
|
102
|
+
|
103
|
+
function rendersClass(node, klass) {
|
104
|
+
return renderClasses(node).includes(klass);
|
105
|
+
}
|
106
|
+
|
107
|
+
function px(value) {
|
108
|
+
return typeof value === 'number' ? `${value}px` : value;
|
109
|
+
}
|
110
|
+
|
111
|
+
function percent(value) {
|
112
|
+
return typeof value === 'number' ? `${value}%` : value;
|
113
|
+
}
|
114
|
+
|
115
|
+
function renderStyles(node) {
|
116
|
+
const {
|
117
|
+
props: {
|
118
|
+
className
|
119
|
+
}
|
120
|
+
} = render(node);
|
121
|
+
return getComputedStyles(className);
|
122
|
+
}
|
123
|
+
|
124
|
+
function getComputedStyles(className) {
|
125
|
+
const div = document.createElement('div');
|
126
|
+
div.className = className;
|
127
|
+
const computed = {};
|
128
|
+
|
129
|
+
for (const sheet of document.styleSheets) {
|
130
|
+
// CSSRulesLists assumes every rule is a CSSRule, not a CSSStyleRule
|
131
|
+
for (const rule of sheet.cssRules) {
|
132
|
+
if (rule instanceof CSSMediaRule) {
|
133
|
+
readMedia(rule);
|
134
|
+
} else if (rule instanceof CSSStyleRule) {
|
135
|
+
readRule(rule, computed);
|
136
|
+
} else {// console.warn('rule.type =', rule.type)
|
137
|
+
}
|
138
|
+
}
|
139
|
+
}
|
140
|
+
|
141
|
+
return computed;
|
142
|
+
|
143
|
+
function matchesSafe(node, selector) {
|
144
|
+
if (!selector) {
|
145
|
+
return false;
|
146
|
+
}
|
147
|
+
|
148
|
+
try {
|
149
|
+
return node.matches(selector);
|
150
|
+
} catch (error) {
|
151
|
+
return false;
|
152
|
+
}
|
153
|
+
}
|
154
|
+
|
155
|
+
function readRule(rule, dest) {
|
156
|
+
if (matchesSafe(div, rule.selectorText)) {
|
157
|
+
const {
|
158
|
+
style
|
159
|
+
} = rule;
|
160
|
+
|
161
|
+
for (let i = 0; i < style.length; i++) {
|
162
|
+
const prop = style[i];
|
163
|
+
dest[prop] = style.getPropertyValue(prop);
|
164
|
+
}
|
165
|
+
} else {// console.warn('no match:', rule.selectorText)
|
166
|
+
}
|
167
|
+
}
|
168
|
+
|
169
|
+
function readMedia(mediaRule) {
|
170
|
+
const key = `@media ${mediaRule.media[0]}`; // const dest = computed[key] || (computed[key] = {})
|
171
|
+
|
172
|
+
const dest = {};
|
173
|
+
|
174
|
+
for (const rule of mediaRule.cssRules) {
|
175
|
+
if (rule instanceof CSSStyleRule) {
|
176
|
+
readRule(rule, dest);
|
177
|
+
}
|
178
|
+
} // Don't add media rule to computed styles
|
179
|
+
// if no styles were actually applied
|
180
|
+
|
181
|
+
|
182
|
+
if (Object.keys(dest).length > 0) {
|
183
|
+
computed[key] = dest;
|
184
|
+
}
|
185
|
+
}
|
186
|
+
}
|
187
|
+
/**
|
188
|
+
* This provides a layer of compatibility between the render() function from
|
189
|
+
* react-test-renderer and Enzyme's mount()
|
190
|
+
*/
|
191
|
+
|
192
|
+
|
193
|
+
function getProps(node) {
|
194
|
+
return typeof node.props === 'function' ? node.props() : node.props;
|
195
|
+
}
|
196
|
+
|
197
|
+
function getClassName(node) {
|
198
|
+
return getProps(node).className;
|
199
|
+
}
|
200
|
+
|
201
|
+
function getClasses(node) {
|
202
|
+
const className = getClassName(node);
|
203
|
+
return className ? className.trim().split(/ +/) : [];
|
204
|
+
}
|
205
|
+
|
206
|
+
async function loadCSS(path) {
|
207
|
+
const css = await readFile(require.resolve(path), 'utf8');
|
208
|
+
const style = document.createElement('style');
|
209
|
+
style.setAttribute('data-path', path);
|
210
|
+
style.textContent = css;
|
211
|
+
document.head.appendChild(style);
|
212
|
+
return style;
|
213
|
+
}
|
214
|
+
|
215
|
+
function unloadCSS(path) {
|
216
|
+
const style = document.querySelector(`style[data-path="${path}"]`);
|
217
|
+
|
218
|
+
if (style) {
|
219
|
+
style.remove();
|
220
|
+
return true;
|
221
|
+
}
|
222
|
+
} // If a component requires certain props or other conditions in order
|
223
|
+
// to render without errors, you can pass a `toRender` function that
|
224
|
+
// returns an element ready to be rendered.
|
225
|
+
|
226
|
+
|
227
|
+
function behavesAsComponent({
|
228
|
+
Component,
|
229
|
+
toRender,
|
230
|
+
options
|
231
|
+
}) {
|
232
|
+
options = options || {};
|
233
|
+
|
234
|
+
const getElement = () => toRender ? toRender() : /*#__PURE__*/_react.default.createElement(Component, null);
|
235
|
+
|
236
|
+
if (!options.skipSx) {
|
237
|
+
it('implements sx prop behavior', () => {
|
238
|
+
expect(getElement()).toImplementSxBehavior();
|
239
|
+
});
|
240
|
+
}
|
241
|
+
|
242
|
+
if (!options.skipAs) {
|
243
|
+
it('respects the as prop', () => {
|
244
|
+
const As = /*#__PURE__*/_react.default.forwardRef((_props, ref) => /*#__PURE__*/_react.default.createElement("div", {
|
245
|
+
className: "as-component",
|
246
|
+
ref: ref
|
247
|
+
}));
|
248
|
+
|
249
|
+
const elem = /*#__PURE__*/_react.default.cloneElement(getElement(), {
|
250
|
+
as: As
|
251
|
+
});
|
252
|
+
|
253
|
+
expect(render(elem)).toEqual(render( /*#__PURE__*/_react.default.createElement(As, null)));
|
254
|
+
});
|
255
|
+
}
|
256
|
+
|
257
|
+
it('sets a valid displayName', () => {
|
258
|
+
expect(Component.displayName).toMatch(COMPONENT_DISPLAY_NAME_REGEX);
|
259
|
+
});
|
260
|
+
it('renders consistently', () => {
|
261
|
+
expect(render(getElement())).toMatchSnapshot();
|
262
|
+
});
|
263
|
+
} // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
264
|
+
|
265
|
+
|
266
|
+
function checkExports(path, exports) {
|
267
|
+
it('has declared exports', () => {
|
268
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
269
|
+
const mod = require(`../${path}`);
|
270
|
+
|
271
|
+
expect(mod).toSetExports(exports);
|
272
|
+
});
|
273
|
+
}
|