@enact/sandstone
Version:
Large-screen/TV support library for Enact, containing a variety of UI components.
280 lines (277 loc) • 16 kB
JavaScript
;
require("@testing-library/jest-dom");
var _react = require("@testing-library/react");
var _react2 = require("react");
var _SharedStateDecorator = _interopRequireWildcard(require("../SharedStateDecorator"));
var _jsxRuntime = require("react/jsx-runtime");
var _excluded = ["children"],
_excluded2 = ["children", "value"];
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
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(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : String(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); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
describe('SharedStateDecorator Specs', function () {
test('should provide a set method via context', function () {
var fn = jest.fn();
var ComponentSet = (0, _SharedStateDecorator["default"])(function () {
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_SharedStateDecorator.SharedState.Consumer, {
children: function children(value) {
fn(value.set);
return null;
}
});
});
(0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(ComponentSet, {}));
var expected = 'function';
var actual = typeof fn.mock.calls[0][0];
expect(actual).toBe(expected);
});
test('should provide a get method via context', function () {
var fn = jest.fn();
var ComponentGet = (0, _SharedStateDecorator["default"])(function () {
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_SharedStateDecorator.SharedState.Consumer, {
children: function children(value) {
fn(value.get);
return null;
}
});
});
(0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(ComponentGet, {}));
var expected = 'function';
var actual = typeof fn.mock.calls[0][0];
expect(actual).toBe(expected);
});
test('should provide a delete method via context', function () {
var fn = jest.fn();
var ComponentDelete = (0, _SharedStateDecorator["default"])(function () {
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_SharedStateDecorator.SharedState.Consumer, {
children: function children(value) {
fn(value["delete"]);
return null;
}
});
});
(0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(ComponentDelete, {}));
var expected = 'function';
var actual = typeof fn.mock.calls[0][0];
expect(actual).toBe(expected);
});
test('should supporting setting and getting a value by key when {id} is set', function () {
var Component = (0, _SharedStateDecorator["default"])(function () {
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_SharedStateDecorator.SharedState.Consumer, {
children: function children(value) {
value.set('key', 'value');
return value.get('key');
}
});
});
(0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(Component, {
id: "outer"
}));
var actual = _react.screen.getByText('value');
expect(actual).toBeInTheDocument();
});
test('should supporting setting and getting a value by key when {id} is set to a non-zero value', function () {
var Component = (0, _SharedStateDecorator["default"])(function () {
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_SharedStateDecorator.SharedState.Consumer, {
children: function children(value) {
value.set('key', 'value');
return value.get('key');
}
});
});
(0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(Component, {
id: -1
}));
var actual = _react.screen.getByText('value');
expect(actual).toBeInTheDocument();
});
test('should supporting setting and getting a value by key when {id} is set to zero', function () {
var Component = (0, _SharedStateDecorator["default"])(function () {
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_SharedStateDecorator.SharedState.Consumer, {
children: function children(value) {
value.set('key', 'value');
return value.get('key');
}
});
});
(0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(Component, {
id: 0
}));
var actual = _react.screen.getByText('value');
expect(actual).toBeInTheDocument();
});
test('should not set or return values when {id} is not set', function () {
var Component = (0, _SharedStateDecorator["default"])(function () {
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_SharedStateDecorator.SharedState.Consumer, {
children: function children(value) {
value.set('key', 'value');
return value.get('key');
}
});
});
(0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(Component, {}));
var actual = _react.screen.queryByText('value');
expect(actual).toBeNull();
});
test('should not set or return values when {id} is set to an empty string', function () {
var Component = (0, _SharedStateDecorator["default"])(function () {
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_SharedStateDecorator.SharedState.Consumer, {
children: function children(value) {
value.set('key', 'value');
return value.get('key');
}
});
});
(0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(Component, {
id: ""
}));
var actual = _react.screen.queryByText('value');
expect(actual).toBeNull();
});
test('should not set or return values when {id} is set to null', function () {
var Component = (0, _SharedStateDecorator["default"])(function () {
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_SharedStateDecorator.SharedState.Consumer, {
children: function children(value) {
value.set('key', 'value');
return value.get('key');
}
});
});
(0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(Component, {
id: null
}));
var actual = _react.screen.queryByText('value');
expect(actual).toBeNull();
});
test('should not set or return values when {id} and {noSharedState} are set', function () {
var Component = (0, _SharedStateDecorator["default"])(function () {
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_SharedStateDecorator.SharedState.Consumer, {
children: function children(value) {
value.set('key', 'value');
return value.get('key');
}
});
});
(0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(Component, {
id: "outer",
noSharedState: true
}));
var actual = _react.screen.queryByText('value');
expect(actual).toBeNull();
});
test('should supporting deleting a value by key when {id} is set', function () {
var Component = (0, _SharedStateDecorator["default"])(function () {
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_SharedStateDecorator.SharedState.Consumer, {
children: function children(value) {
value.set('key', 'value');
value["delete"]('key');
return value.get('key');
}
});
});
(0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(Component, {
id: "outer"
}));
var actual = _react.screen.queryByText('value');
expect(actual).toBeNull();
});
test('should share data upstream when inside another SharedStateDecorator', function () {
var Component = (0, _SharedStateDecorator["default"])(function (_ref) {
var _children = _ref.children,
rest = _objectWithoutProperties(_ref, _excluded);
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_SharedStateDecorator.SharedState.Consumer, {
children: function children(value) {
value.set('key', 'value');
return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", _objectSpread(_objectSpread({}, rest), {}, {
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
children: value.get('key')
}), _children]
}));
}
});
});
(0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(Component, {
id: "outer",
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(Component, {
id: "inner"
})
}));
var expected = '<div id="outer"><span>value</span><div id="inner"><span>value</span></div></div>';
var actual = _react.screen.getAllByText('value')[0].parentElement.parentElement.innerHTML;
expect(actual).toEqual(expected);
});
test('should restore shared state from ancestor', function () {
var Base = /*#__PURE__*/function (_ReactComponent) {
_inherits(Base, _ReactComponent);
var _super = _createSuper(Base);
function Base() {
_classCallCheck(this, Base);
return _super.apply(this, arguments);
}
_createClass(Base, [{
key: "render",
value: function render() {
var _this$props = this.props,
children = _this$props.children,
propValue = _this$props.value,
rest = _objectWithoutProperties(_this$props, _excluded2);
if (propValue) {
this.context.set('key', propValue);
}
return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", _objectSpread(_objectSpread({}, rest), {}, {
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
children: this.context.get('key')
}), children]
}));
}
}]);
return Base;
}(_react2.Component);
Base.contextType = _SharedStateDecorator.SharedState;
var Component = (0, _SharedStateDecorator["default"])({
updateOnMount: true
}, Base);
var _render = (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(Component, {
id: "outer",
value: "value",
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(Component, {
id: "inner",
value: "from-prop"
})
})),
rerender = _render.rerender;
// remove the children which drops inner's shared state
rerender( /*#__PURE__*/(0, _jsxRuntime.jsx)(Component, {
id: "outer",
value: "value"
}));
// recreate it with the same id but no initial value to validate the previous state is
// restored. updateOnMount is used above to coerce a re-render on mount since the shared
// state value is used in the render method and isn't available on first render otherwise.
rerender( /*#__PURE__*/(0, _jsxRuntime.jsx)(Component, {
id: "outer",
value: "value",
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(Component, {
id: "inner"
})
}));
var actual = _react.screen.getByText('from-prop');
expect(actual).toBeInTheDocument();
});
});