UNPKG

@enact/core

Version:

Enact is an open source JavaScript framework containing everything you need to create a fast, scalable mobile or web application.

196 lines (194 loc) 10.6 kB
"use strict"; var _react = require("react"); require("@testing-library/jest-dom"); var _react2 = require("@testing-library/react"); var _userEvent = _interopRequireDefault(require("@testing-library/user-event")); var _Registry = _interopRequireDefault(require("../Registry")); var _jsxRuntime = require("react/jsx-runtime"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; } function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); } function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; } 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 _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), 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); } function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _possibleConstructorReturn(t, e) { if (e && ("object" == typeof e || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); } function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); } function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); } function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); } var SomeContext = /*#__PURE__*/(0, _react.createContext)(); describe('Registry', function () { var NotifiesTree = /*#__PURE__*/function (_Component) { function NotifiesTree() { var _this; _classCallCheck(this, NotifiesTree); _this = _callSuper(this, NotifiesTree); _this.handleNotify = function (_ref) { var action = _ref.action; if (action === 'update') { _this.registry.parent = _this.context; } }; _this.handleClick = function () { _this.registry.notify({ action: 'notify' }); }; _this.registry = _Registry["default"].create(_this.handleNotify); return _this; } _inherits(NotifiesTree, _Component); return _createClass(NotifiesTree, [{ key: "componentDidMount", value: function componentDidMount() { this.registry.parent = this.context; } }, { key: "render", value: function render() { return /*#__PURE__*/(0, _jsxRuntime.jsxs)(SomeContext, { value: this.registry.register, children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("button", _objectSpread(_objectSpread({}, this.props), {}, { onClick: this.handleClick, children: "Notify!" })), this.props.children] }); } }]); }(_react.Component); NotifiesTree.contextType = SomeContext; var HandlesNotification = /*#__PURE__*/function (_Component2) { function HandlesNotification() { var _this2; _classCallCheck(this, HandlesNotification); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this2 = _callSuper(this, HandlesNotification, [].concat(args)); _this2.state = { number: 0 }; _this2.handleResize = function () { _this2.setState(function (prevState) { var number = prevState.number + 1; _this2.registry.notify({ action: 'update' }); return { number: number }; }); }; return _this2; } _inherits(HandlesNotification, _Component2); return _createClass(HandlesNotification, [{ key: "componentDidMount", value: function componentDidMount() { if (this.context && typeof this.context === 'function') { this.registry = this.context(this.handleResize); } } }, { key: "componentWillUnmount", value: function componentWillUnmount() { if (this.registry) { this.registry.unregister(); } } }, { key: "render", value: function render() { return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", _objectSpread(_objectSpread({}, this.props), {}, { children: this.state.number })); } }]); }(_react.Component); HandlesNotification.contextType = SomeContext; test('should increment child on click', /*#__PURE__*/_asyncToGenerator(function* () { var user = _userEvent["default"].setup(); (0, _react2.render)(/*#__PURE__*/(0, _jsxRuntime.jsx)(NotifiesTree, { "data-testid": "a-btn", children: /*#__PURE__*/(0, _jsxRuntime.jsx)(HandlesNotification, { "data-testid": "a" }) })); yield user.click(_react2.screen.getByTestId('a-btn')); var expected = '1'; var child = _react2.screen.getByTestId('a'); expect(child).toHaveTextContent(expected); })); test('should increment both children on top click', /*#__PURE__*/_asyncToGenerator(function* () { var user = _userEvent["default"].setup(); (0, _react2.render)(/*#__PURE__*/(0, _jsxRuntime.jsxs)(NotifiesTree, { "data-testid": "a-btn", children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(HandlesNotification, { "data-testid": "a" }), /*#__PURE__*/(0, _jsxRuntime.jsx)(NotifiesTree, { "data-testid": "b-btn", children: /*#__PURE__*/(0, _jsxRuntime.jsx)(HandlesNotification, { "data-testid": "b" }) })] })); yield user.click(_react2.screen.getByTestId('a-btn')); var expected = '1'; var childA = _react2.screen.getByTestId('a'); var childB = _react2.screen.getByTestId('b'); expect(childA).toHaveTextContent(expected); expect(childB).toHaveTextContent(expected); })); test('should increment the deepest child when we click child button', /*#__PURE__*/_asyncToGenerator(function* () { var user = _userEvent["default"].setup(); (0, _react2.render)(/*#__PURE__*/(0, _jsxRuntime.jsxs)(NotifiesTree, { "data-testid": "a-btn", children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(HandlesNotification, { "data-testid": "a" }), /*#__PURE__*/(0, _jsxRuntime.jsx)(NotifiesTree, { "data-testid": "b-btn", children: /*#__PURE__*/(0, _jsxRuntime.jsx)(HandlesNotification, { "data-testid": "b" }) })] })); yield user.click(_react2.screen.getByTestId('b-btn')); var expectedA = '0'; var expectedB = '1'; var childA = _react2.screen.getByTestId('a'); var childB = _react2.screen.getByTestId('b'); expect(childA).toHaveTextContent(expectedA); expect(childB).toHaveTextContent(expectedB); })); test('should support removing children without error', /*#__PURE__*/_asyncToGenerator(function* () { var user = _userEvent["default"].setup(); var _render = (0, _react2.render)(/*#__PURE__*/(0, _jsxRuntime.jsxs)(NotifiesTree, { "data-testid": "a-btn", children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(HandlesNotification, { "data-testid": "a" }), /*#__PURE__*/(0, _jsxRuntime.jsx)(HandlesNotification, { "data-testid": "b" })] })), rerender = _render.rerender; yield user.click(_react2.screen.getByTestId('a-btn')); // changing children should be safe and not throw errors when notifying instances rerender(/*#__PURE__*/(0, _jsxRuntime.jsx)(NotifiesTree, { "data-testid": "a-btn", children: /*#__PURE__*/(0, _jsxRuntime.jsx)(HandlesNotification, { "data-testid": "c" }) })); yield user.click(_react2.screen.getByTestId('a-btn')); var expectedC = '1'; var childC = _react2.screen.getByTestId('c'); expect(childC).toHaveTextContent(expectedC); })); });