@enact/sandstone
Version:
Large-screen/TV support library for Enact, containing a variety of UI components.
142 lines (141 loc) • 6.95 kB
JavaScript
;
var _FloatingLayer = require("@enact/ui/FloatingLayer");
require("@testing-library/jest-dom");
var _react = require("@testing-library/react");
var _react2 = require("react");
var _Item = _interopRequireDefault(require("../../Item"));
var _Panels = require("../../Panels");
var _FixedPopupPanels = _interopRequireDefault(require("../FixedPopupPanels"));
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
var keyDown = function keyDown(keyCode) {
return function (elm) {
return _react.fireEvent.keyDown(elm, {
keyCode: keyCode
});
};
};
var leftKeyDown = keyDown(37);
var FloatingLayerController = (0, _FloatingLayer.FloatingLayerDecorator)('div');
var CustomFixedPopupPanels = function CustomFixedPopupPanels(_ref) {
var defaultIndex = _ref.defaultIndex;
var _useState = (0, _react2.useState)(defaultIndex),
_useState2 = _slicedToArray(_useState, 2),
index = _useState2[0],
setIndex = _useState2[1];
return /*#__PURE__*/(0, _jsxRuntime.jsx)(FloatingLayerController, {
children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_FixedPopupPanels["default"], {
index: index,
open: true,
rtl: false,
children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)(_Panels.Panel, {
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_Panels.Header, {
children: /*#__PURE__*/(0, _jsxRuntime.jsx)("title", {
children: "This is the first panel"
})
}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_Item["default"], {
children: "Example Item 1"
}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_Item["default"], {
children: "Example Item 2"
})]
}), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_Panels.Panel, {
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_Panels.Header, {
children: /*#__PURE__*/(0, _jsxRuntime.jsx)("title", {
children: "This is the second panel"
})
}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_Item["default"], {
onKeyDown: function onKeyDown() {
return setIndex(index - 1);
},
children: "Example Item 1 on Panel 2"
})]
})]
})
});
};
describe('FixedPopupPanels', function () {
test('should have the default width when nothing is assigned', function () {
(0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(FloatingLayerController, {
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_FixedPopupPanels["default"], {
"data-testid": "panels-id",
open: true
})
}));
var expected = 'narrow';
var actual = _react.screen.getByTestId('panels-id').parentElement.parentElement;
expect(actual).toHaveClass(expected);
});
test('should have narrow width applied when width="narrow"', function () {
(0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(FloatingLayerController, {
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_FixedPopupPanels["default"], {
"data-testid": "panels-id",
open: true,
width: "narrow"
})
}));
var expected = 'narrow';
var actual = _react.screen.getByTestId('panels-id').parentElement.parentElement;
expect(actual).toHaveClass(expected);
});
test('should have half width applied when width="half"', function () {
(0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(FloatingLayerController, {
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_FixedPopupPanels["default"], {
"data-testid": "panels-id",
open: true,
width: "half"
})
}));
var expected = 'half';
var actual = _react.screen.getByTestId('panels-id').parentElement.parentElement;
expect(actual).toHaveClass(expected);
});
test('should correctly assign the fullHeight class', function () {
(0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(FloatingLayerController, {
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_FixedPopupPanels["default"], {
"data-testid": "panels-id",
fullHeight: true,
open: true
})
}));
var expected = 'fullHeight';
var actual = _react.screen.getByTestId('panels-id').parentElement.parentElement;
expect(actual).toHaveClass(expected);
});
test('should close on back key', function () {
var map = {};
window.addEventListener = jest.fn(function (event, cb) {
map[event] = cb;
});
var handleClose = jest.fn();
(0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(FloatingLayerController, {
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_FixedPopupPanels["default"], {
onClose: handleClose,
open: true
})
}));
map.keyup({
type: 'keyup',
currentTarget: window,
keyCode: 27
});
var expectedEvent = {
type: 'onClose'
};
var actualEvent = handleClose.mock.calls.length && handleClose.mock.calls[0][0];
expect(handleClose).toHaveBeenCalled();
expect(actualEvent).toMatchObject(expectedEvent);
});
test('should navigate to the first panel on arrow left key', function () {
(0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(CustomFixedPopupPanels, {
defaultIndex: 1
}));
leftKeyDown(_react.screen.getByText('Example Item 1 on Panel 2'));
expect(_react.screen.getByText('This is the first panel')).toBeInTheDocument();
});
});