UNPKG

zarm

Version:

基于 React 的移动端UI库

218 lines (186 loc) 8.95 kB
import _defineProperty from "@babel/runtime/helpers/defineProperty"; 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; } import React, { useRef } from 'react'; import { fireEvent, render, screen } from '@testing-library/react'; import { mocked } from 'ts-jest/utils'; import { CloseCircle } from '@zarm-design/icons'; import { createFCRefMock } from '../../../tests/utils'; import NoticeBar from '../index'; import ConfigProvider from '../../config-provider'; jest.mock('react', function () { return _objectSpread(_objectSpread({}, jest.requireActual('react')), {}, { useRef: jest.fn().mockReturnValue({ current: { getBoundingClientRect: jest.fn() } }) }); }); var useMockRef = mocked(useRef); describe('NoticeBar', function () { afterEach(function () { jest.restoreAllMocks(); jest.clearAllMocks(); }); test('should render default notice bar', function () { var _render = render( /*#__PURE__*/React.createElement(NoticeBar, null, "\u666E\u901A")), container = _render.container; expect(screen.queryByText(/普通/)).toBeTruthy(); expect(container).toMatchSnapshot(); }); test('should render notice bar with custom theme', function () { var _render2 = render( /*#__PURE__*/React.createElement(NoticeBar, { theme: "danger" }, "\u81EA\u5B9A\u4E49\u4E3B\u9898")), container = _render2.container; expect(screen.queryByText(/自定义主题/)).toBeTruthy(); expect(container).toMatchSnapshot(); }); test('should render default notice bar', function () { var _render3 = render( /*#__PURE__*/React.createElement(NoticeBar, { icon: /*#__PURE__*/React.createElement(CloseCircle, { "data-testid": "notice-bar-icon" }) }, "\u81EA\u5B9A\u4E49\u56FE\u6807")), container = _render3.container; expect(screen.queryByText(/自定义图标/)).toBeTruthy(); expect(screen.queryByTestId('notice-bar-icon')).toBeTruthy(); expect(container).toMatchSnapshot(); }); test('should add animation style to the content element and append a style tag into document with keyframe animation', function () { expect.assertions(3); var mWrapperRef = createFCRefMock('getBoundingClientRect', { width: 100 }); var mContentRef = createFCRefMock('getBoundingClientRect', { width: 200 }); useMockRef.mockReturnValueOnce(mWrapperRef).mockReturnValueOnce(mContentRef); render( /*#__PURE__*/React.createElement(NoticeBar, null, "\u666E\u901A")); var noticeBarContent = document.querySelector('.za-notice-bar__content'); if (noticeBarContent) { expect(window.getComputedStyle(noticeBarContent).getPropertyValue('animation')).toEqual('za-notice-bar-scrolling 8000ms linear infinite'); } var keyframeStyleElement = document.querySelector('#za-notice-bar-scrolling'); if (keyframeStyleElement) { expect(keyframeStyleElement).toBeTruthy(); expect(keyframeStyleElement.innerHTML).toMatchSnapshot(); } }); test('should use prefixCls from context', function () { expect.assertions(3); render( /*#__PURE__*/React.createElement(ConfigProvider, { prefixCls: "zarm" }, /*#__PURE__*/React.createElement(NoticeBar, { "data-testid": "message" }, "\u666E\u901A"))); expect(screen.queryByText(/普通/)).toBeTruthy(); var messageElement = screen.queryByTestId('message'); if (messageElement) { if (messageElement.parentElement) { expect(messageElement.parentElement.className).toEqual('zarm-notice-bar'); } expect(messageElement.className).toEqual('zarm-message zarm-message--warning'); } }); test('should forward ref from parent', function () { expect.assertions(2); var ref = /*#__PURE__*/React.createRef(); render( /*#__PURE__*/React.createElement(NoticeBar, { ref: ref }, "\u666E\u901A")); if (ref.current) { expect(ref.current.nodeName.toLowerCase()).toEqual('div'); expect(ref.current.className).toEqual('za-notice-bar'); } }); test('should recalculate keyframe of the animation when new value of speed prop comes', function () { expect.assertions(4); var mWrapperRef = createFCRefMock('getBoundingClientRect', { width: 100 }); var mContentRef = createFCRefMock('getBoundingClientRect', { width: 200 }); useMockRef.mockReturnValueOnce(mWrapperRef).mockReturnValueOnce(mContentRef); var _render4 = render( /*#__PURE__*/React.createElement(NoticeBar, null, "\u666E\u901A")), rerender = _render4.rerender; var noticeBarContent = document.querySelector('.za-notice-bar__content'); if (noticeBarContent) { expect(window.getComputedStyle(noticeBarContent).getPropertyValue('animation')).toEqual('za-notice-bar-scrolling 8000ms linear infinite'); } useMockRef.mockReturnValueOnce(mWrapperRef).mockReturnValueOnce(mContentRef); rerender( /*#__PURE__*/React.createElement(NoticeBar, { speed: 100 }, "\u666E\u901A")); noticeBarContent = document.querySelector('.za-notice-bar__content'); if (noticeBarContent) { expect(window.getComputedStyle(noticeBarContent).getPropertyValue('animation')).toEqual('za-notice-bar-scrolling 6000ms linear infinite'); } var keyframeStyleElement = document.querySelector('#za-notice-bar-scrolling'); if (keyframeStyleElement) { expect(keyframeStyleElement).toBeTruthy(); expect(keyframeStyleElement.innerHTML).toMatchSnapshot(); } }); test('should recalculate keyframe of the animation when new value of delay prop comes', function () { expect.assertions(2); var mWrapperRef = createFCRefMock('getBoundingClientRect', { width: 100 }); var mContentRef = createFCRefMock('getBoundingClientRect', { width: 200 }); useMockRef.mockReturnValueOnce(mWrapperRef).mockReturnValueOnce(mContentRef); var _render5 = render( /*#__PURE__*/React.createElement(NoticeBar, null, "\u666E\u901A")), rerender = _render5.rerender; var noticeBarContent = document.querySelector('.za-notice-bar__content'); if (noticeBarContent) { expect(window.getComputedStyle(noticeBarContent).getPropertyValue('animation')).toEqual('za-notice-bar-scrolling 8000ms linear infinite'); } useMockRef.mockReturnValueOnce(mWrapperRef).mockReturnValueOnce(mContentRef); rerender( /*#__PURE__*/React.createElement(NoticeBar, { delay: 4000 }, "\u666E\u901A")); noticeBarContent = document.querySelector('.za-notice-bar__content'); if (noticeBarContent) { expect(window.getComputedStyle(noticeBarContent).getPropertyValue('animation')).toEqual('za-notice-bar-scrolling 12000ms linear infinite'); } }); test('should be closable(remove from the document when user click the close icon)', function () { var _screen$queryByTestId, _screen$queryByTestId2; expect.assertions(4); var onClose = jest.fn(); var _render6 = render( /*#__PURE__*/React.createElement(NoticeBar, { closable: true, onClose: onClose, "data-testid": "root" }, "\u666E\u901A")), container = _render6.container; expect(container).toMatchSnapshot(); var closeIcon = (_screen$queryByTestId = screen.queryByTestId('root')) === null || _screen$queryByTestId === void 0 ? void 0 : (_screen$queryByTestId2 = _screen$queryByTestId.querySelector('.za-message__footer')) === null || _screen$queryByTestId2 === void 0 ? void 0 : _screen$queryByTestId2.firstChild; if (closeIcon) { fireEvent.click(closeIcon); expect(screen.queryByTestId('root')).toBeNull(); } expect(document.querySelector('.za-notice-bar')).toBeNull(); expect(onClose).toBeCalledTimes(1); }); test('should respond to user click events when notice bar has arrow prop', function () { expect.assertions(2); var onClick = jest.fn(); var _render7 = render( /*#__PURE__*/React.createElement(NoticeBar, { hasArrow: true, "data-testid": "root", onClick: onClick }, "\u666E\u901A")), container = _render7.container; expect(container).toMatchSnapshot(); var message = screen.queryByTestId('root'); if (message) { fireEvent.click(message); expect(onClick).toBeCalledTimes(1); } }); });