UNPKG

@enact/sandstone

Version:

Large-screen/TV support library for Enact, containing a variety of UI components.

192 lines (191 loc) 8.11 kB
"use strict"; require("@testing-library/jest-dom"); var _react = require("@testing-library/react"); var _userEvent = _interopRequireDefault(require("@testing-library/user-event")); var _ = require("../"); var _jsxRuntime = require("react/jsx-runtime"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } describe('PageViews Specs', function () { test('should show next button and hide previous button on the first page', function () { (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsxs)(_.PageViews, { index: 0, children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_.Page, { children: "I gots contents" }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_.Page, { children: "I gots contents" })] })); var nextButton = _react.screen.getByLabelText('Next'); var prevButton = _react.screen.queryByLabelText('Previous'); expect(nextButton).toBeInTheDocument(); expect(prevButton).toBeNull(); }); test('should show next button and hide previous button on the first page when pageIndicatorType is `number`', function () { (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsxs)(_.PageViews, { pageIndicatorType: "number", index: 0, children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_.Page, { children: "I gots contents" }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_.Page, { children: "I gots contents" })] })); var nextButton = _react.screen.getByLabelText('Next'); var prevButton = _react.screen.queryByLabelText('Previous'); expect(nextButton).toBeInTheDocument(); expect(prevButton).toBeNull(); }); test('should show previous button and hide next button on the last page', function () { (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsxs)(_.PageViews, { index: 1, children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_.Page, { children: "I gots contents" }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_.Page, { children: "I gots contents" })] })); var prevButton = _react.screen.getByLabelText('Previous'); var nextButton = _react.screen.queryByLabelText('Next'); expect(prevButton).toBeInTheDocument(); expect(nextButton).toBeNull(); }); test('should show previous button and hide next button on the last page when pageIndicatorType is `number`', function () { (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsxs)(_.PageViews, { pageIndicatorType: "number", index: 1, children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_.Page, { children: "I gots contents" }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_.Page, { children: "I gots contents" })] })); var prevButton = _react.screen.getByLabelText('Previous'); var nextButton = _react.screen.queryByLabelText('Next'); expect(prevButton).toBeInTheDocument(); expect(nextButton).toBeNull(); }); test('should fire onTransition with target index and type', /*#__PURE__*/_asyncToGenerator(function* () { var spy = jest.fn(); var index = 0; var _render = (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsxs)(_.PageViews, { index: index, onTransition: spy, noAnimation: true, children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_.Page, { children: "I gots contents" }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_.Page, { children: "I gots contents2" })] })), rerender = _render.rerender; spy.mockClear(); index++; rerender( /*#__PURE__*/(0, _jsxRuntime.jsxs)(_.PageViews, { index: index, onTransition: spy, noAnimation: true, children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_.Page, { children: "I gots contents" }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_.Page, { children: "I gots contents2" })] })); var expected = { index: index, type: 'onTransition' }; var actual = spy.mock.calls.length && spy.mock.calls[0][0]; yield (0, _react.waitFor)(function () { expect(actual).toMatchObject(expected); }); })); test('should fire onWillTransition with target index and type', /*#__PURE__*/_asyncToGenerator(function* () { var spy = jest.fn(); var index = 0; var _render2 = (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsxs)(_.PageViews, { index: index, onWillTransition: spy, noAnimation: true, children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_.Page, { children: "I gots contents" }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_.Page, { children: "I gots contents2" })] })), rerender = _render2.rerender; spy.mockClear(); index++; rerender( /*#__PURE__*/(0, _jsxRuntime.jsxs)(_.PageViews, { index: index, onWillTransition: spy, noAnimation: true, children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_.Page, { children: "I gots contents" }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_.Page, { children: "I gots contents2" })] })); var expected = { index: index, type: 'onWillTransition' }; var actual = spy.mock.calls.length && spy.mock.calls[0][0]; yield (0, _react.waitFor)(function () { expect(actual).toMatchObject(expected); }); })); test('should fire `onNextClick` and `onChange` with type when go to the next page', /*#__PURE__*/_asyncToGenerator(function* () { var handleChange = jest.fn(); var handleNextClick = jest.fn(); var user = _userEvent["default"].setup(); (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsxs)(_.PageViews, { index: 1, onChange: handleChange, onNextClick: handleNextClick, children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_.Page, {}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_.Page, {}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_.Page, {})] })); var nextButton = _react.screen.getByLabelText('Next'); var expected = { type: 'onNextClick' }; yield user.click(nextButton); yield (0, _react.waitFor)(function () { expect(handleChange).toBeCalledWith({ index: 2, type: 'onChange' }); }); yield (0, _react.waitFor)(function () { var actual = handleNextClick.mock.calls.length && handleNextClick.mock.calls[0][0]; expect(actual).toMatchObject(expected); }); })); test('should fire `onPrevClick` and `onChange` with type when go to the previous page', /*#__PURE__*/_asyncToGenerator(function* () { var handleChange = jest.fn(); var handlePrevClick = jest.fn(); var user = _userEvent["default"].setup(); (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsxs)(_.PageViews, { index: 2, onChange: handleChange, onPrevClick: handlePrevClick, children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_.Page, {}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_.Page, {}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_.Page, {})] })); var prevButton = _react.screen.getByLabelText('Previous'); var expected = { type: 'onPrevClick' }; yield user.click(prevButton); yield (0, _react.waitFor)(function () { expect(handleChange).toBeCalledWith({ index: 1, type: 'onChange' }); }); yield (0, _react.waitFor)(function () { var actual = handlePrevClick.mock.calls.length && handlePrevClick.mock.calls[0][0]; expect(actual).toMatchObject(expected); }); })); });