UNPKG

@enact/ui

Version:

A collection of simplified unstyled cross-platform UI components for Enact

278 lines (277 loc) 9.91 kB
"use strict"; require("@testing-library/jest-dom"); var _react = require("@testing-library/react"); var _useSkins = _interopRequireDefault(require("../useSkins")); var _jsxRuntime = require("react/jsx-runtime"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; } function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } describe('Skinnable Specs', function () { var data; var Base = function Base(props) { data = props; return null; }; function Component(_ref) { var config = _ref.config; var skins = (0, _useSkins["default"])(config); return /*#__PURE__*/(0, _jsxRuntime.jsx)(Base, _objectSpread({}, skins)); } function Parent(_ref2) { var config = _ref2.config, children = _ref2.children; var skins = (0, _useSkins["default"])(config); return skins.provideSkins(children); } test('should add a default skin class when no skin prop is specified', function () { var config = { defaultSkin: 'dark', skins: { dark: 'darkSkin', light: 'lightSkin' } }; (0, _react.render)(/*#__PURE__*/(0, _jsxRuntime.jsx)(Component, { config: config })); var expected = 'darkSkin'; expect(data).toHaveProperty('className', expected); }); test('should add the preferred skin class when the skin prop is specified', function () { var config = { defaultSkin: 'dark', skin: 'light', skins: { dark: 'darkSkin', light: 'lightSkin' } }; (0, _react.render)(/*#__PURE__*/(0, _jsxRuntime.jsx)(Component, { config: config })); var expected = 'lightSkin'; expect(data).toHaveProperty('className', expected); }); test('should ignore the preferred skin prop if it\'s not one of the available skins', function () { var config = { defaultSkin: 'dark', skin: 'potato', skins: { dark: 'darkSkin', light: 'lightSkin' } }; (0, _react.render)(/*#__PURE__*/(0, _jsxRuntime.jsx)(Component, { config: config })); var expected = ''; expect(data).toHaveProperty('className', expected); }); test('should ignore the skinVariants prop if there are no defined allowedVariants', function () { var config = { defaultSkin: 'dark', skinVariants: 'potato', skins: { dark: 'darkSkin', light: 'lightSkin' } }; (0, _react.render)(/*#__PURE__*/(0, _jsxRuntime.jsx)(Component, { config: config })); var expected = 'darkSkin'; expect(data).toHaveProperty('className', expected); }); test('should only apply allowed variants assigned by the skinVariants prop', function () { var config = { defaultSkin: 'dark', skinVariants: 'normal potato unicase', skins: { dark: 'darkSkin', light: 'lightSkin' }, variants: ['normal', 'smallCaps', 'unicase'] }; (0, _react.render)(/*#__PURE__*/(0, _jsxRuntime.jsx)(Component, { config: config })); var expected = 'darkSkin normal unicase'; expect(data).toHaveProperty('className', expected); }); test('should apply default variants even if the skinVariants prop is explicitly empty', function () { var config = { defaultSkin: 'dark', defaultVariants: 'normal', skinVariants: '', skins: { dark: 'darkSkin', light: 'lightSkin' }, variants: ['normal', 'smallCaps', 'unicase'] }; (0, _react.render)(/*#__PURE__*/(0, _jsxRuntime.jsx)(Component, { config: config })); var expected = 'darkSkin normal'; expect(data).toHaveProperty('className', expected); }); test('should apply default variants and the skinVariants if both are defined', function () { var config = { defaultSkin: 'dark', defaultVariants: 'normal', skinVariants: 'unicase', skins: { dark: 'darkSkin', light: 'lightSkin' }, variants: ['normal', 'smallCaps', 'unicase'] }; (0, _react.render)(/*#__PURE__*/(0, _jsxRuntime.jsx)(Component, { config: config })); var expected = 'darkSkin normal unicase'; expect(data).toHaveProperty('className', expected); }); test('should apply variants supplied via an array or a string in the same way', function () { var config = { defaultSkin: 'dark', defaultVariants: 'normal', skins: { dark: 'darkSkin', light: 'lightSkin' }, variants: ['normal', 'smallCaps', 'unicase'] }; (0, _react.render)(/*#__PURE__*/(0, _jsxRuntime.jsx)(Component, { config: _objectSpread(_objectSpread({}, config), {}, { skinVariants: 'normal unicase' }) })); var component1Class = data.className; (0, _react.render)(/*#__PURE__*/(0, _jsxRuntime.jsx)(Component, { config: _objectSpread(_objectSpread({}, config), {}, { skinVariants: ['normal', 'unicase'] }) })); var component2Class = data.className; expect(component1Class).toBe(component2Class); }); test('should allow opting out of the default variants if an object is supplied to skinVariants with false as variant-key values', function () { var config = { defaultSkin: 'dark', defaultVariants: 'normal', skinVariants: { normal: false, unicase: true }, skins: { dark: 'darkSkin', light: 'lightSkin' }, variants: ['normal', 'smallCaps', 'unicase'] }; (0, _react.render)(/*#__PURE__*/(0, _jsxRuntime.jsx)(Component, { config: config })); var expected = 'darkSkin unicase'; expect(data).toHaveProperty('className', expected); }); test('should ignore variants, sent by an object, equaling null, undefined, or empty string', function () { var config = { defaultSkin: 'dark', defaultVariants: 'normal', skinVariants: { normal: null, smallCaps: void 0, unicase: '' }, skins: { dark: 'darkSkin', light: 'lightSkin' }, variants: ['normal', 'smallCaps', 'unicase'] }; (0, _react.render)(/*#__PURE__*/(0, _jsxRuntime.jsx)(Component, { config: config })); var expected = 'darkSkin normal'; expect(data).toHaveProperty('className', expected); }); test('should apply parent variants', function () { var config = { defaultSkin: 'dark', defaultVariants: 'normal', skins: { dark: 'darkSkin', light: 'lightSkin' }, variants: ['normal', 'smallCaps', 'unicase'] }; (0, _react.render)(/*#__PURE__*/(0, _jsxRuntime.jsx)(Parent, { config: _objectSpread(_objectSpread({}, config), {}, { skinVariants: 'unicase' }), children: /*#__PURE__*/(0, _jsxRuntime.jsx)(Component, { config: _objectSpread(_objectSpread({}, config), {}, { skinVariants: 'smallCaps' }) }) })); var expected = 'darkSkin normal unicase smallCaps'; expect(data).toHaveProperty('className', expected); }); test('should be able to override a parent\'s variants by assigning a false skinVariant', function () { var config = { defaultSkin: 'dark', defaultVariants: 'normal', skins: { dark: 'darkSkin', light: 'lightSkin' }, variants: ['normal', 'smallCaps', 'unicase'] }; (0, _react.render)(/*#__PURE__*/(0, _jsxRuntime.jsx)(Parent, { config: _objectSpread(_objectSpread({}, config), {}, { skinVariants: 'smallCaps unicase' }), children: /*#__PURE__*/(0, _jsxRuntime.jsx)(Component, { config: _objectSpread(_objectSpread({}, config), {}, { skinVariants: { unicase: false } }) }) })); var expected = 'darkSkin normal smallCaps'; expect(data).toHaveProperty('className', expected); }); test('should inherit an overridden default variant', function () { var config = { defaultSkin: 'dark', defaultVariants: 'normal', skins: { dark: 'darkSkin', light: 'lightSkin' }, variants: ['normal', 'smallCaps', 'unicase'] }; (0, _react.render)(/*#__PURE__*/(0, _jsxRuntime.jsx)(Parent, { config: config, children: /*#__PURE__*/(0, _jsxRuntime.jsx)(Parent, { config: _objectSpread(_objectSpread({}, config), {}, { skinVariants: { normal: false } }), children: /*#__PURE__*/(0, _jsxRuntime.jsx)(Component, { config: config }) }) })); var expected = 'darkSkin'; expect(data).toHaveProperty('className', expected); }); });