@geezee/react-ui
Version:
Modern and minimalist React UI library.
566 lines (507 loc) • 20.7 kB
JavaScript
import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import React from 'react';
import { mount } from 'enzyme';
import { Button, Tooltip, CfxProvider } from 'components';
import { nativeEvent, updateWrapper } from 'tests/utils';
import { act } from 'react-dom/test-utils';
var expectTooltipIsShow = function expectTooltipIsShow(wrapper) {
expect(wrapper.find('.tooltip-content').length).not.toBe(0);
};
var expectTooltipIsHidden = function expectTooltipIsHidden(wrapper) {
expect(wrapper.find('.tooltip-content').length).toBe(0);
};
var simulateNativeClick = function simulateNativeClick(el) {
el.dispatchEvent(new MouseEvent('click', {
view: window,
bubbles: true,
cancelable: true
}));
};
var typeWrapper = mount( /*#__PURE__*/React.createElement("div", null));
var clickAway = /*#__PURE__*/function () {
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(wrapper) {
return _regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
_context2.next = 2;
return act( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
return _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
simulateNativeClick(document.body);
case 1:
case "end":
return _context.stop();
}
}
}, _callee);
})));
case 2:
_context2.next = 4;
return updateWrapper(wrapper, 150);
case 4:
case "end":
return _context2.stop();
}
}
}, _callee2);
}));
return function clickAway(_x) {
return _ref.apply(this, arguments);
};
}();
describe('Tooltip', function () {
it('should render correctly', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3() {
var wrapper;
return _regeneratorRuntime.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
wrapper = mount( /*#__PURE__*/React.createElement(CfxProvider, {
theme: {
type: 'dark'
}
}, /*#__PURE__*/React.createElement(Tooltip, {
text: /*#__PURE__*/React.createElement("p", {
id: "test"
}, "custom-content")
}, "some tips")));
expectTooltipIsHidden(wrapper);
wrapper.find('.tooltip').simulate('mouseEnter', nativeEvent);
_context3.next = 5;
return updateWrapper(wrapper, 150);
case 5:
wrapper.find('#test').simulate('click', nativeEvent);
expectTooltipIsShow(wrapper);
_context3.next = 9;
return updateWrapper(wrapper, 150);
case 9:
wrapper.find('.tooltip').simulate('mouseLeave', nativeEvent);
_context3.next = 12;
return updateWrapper(wrapper, 400);
case 12:
expectTooltipIsHidden(wrapper);
expect(function () {
return wrapper.unmount();
}).not.toThrow();
case 14:
case "end":
return _context3.stop();
}
}
}, _callee3);
})));
it('should call custom mouse event handler when is controlled', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4() {
var onMouseEnter, onMouseLeave, onClickAway, onVisibleChange, wrapper;
return _regeneratorRuntime.wrap(function _callee4$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
onMouseEnter = jest.fn();
onMouseLeave = jest.fn();
onClickAway = jest.fn();
onVisibleChange = jest.fn();
wrapper = mount( /*#__PURE__*/React.createElement(CfxProvider, {
theme: {
type: 'dark'
}
}, /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("div", {
id: "click-away"
}, "click away"), /*#__PURE__*/React.createElement(Tooltip, {
visible: true,
text: /*#__PURE__*/React.createElement("p", {
id: "test"
}, "custom-content"),
onVisibleChange: onVisibleChange,
onMouseEnter: onMouseEnter,
onMouseLeave: onMouseLeave,
onClickAway: onClickAway
}, "some tips"))));
wrapper.find('.tooltip').simulate('mouseEnter', nativeEvent);
expect(onMouseEnter).toBeCalledTimes(1);
expect(onVisibleChange).toBeCalledTimes(1);
wrapper.find('.tooltip').simulate('mouseLeave', nativeEvent);
expect(onMouseLeave).toBeCalledTimes(1);
_context4.next = 12;
return clickAway(wrapper);
case 12:
expect(onClickAway).toBeCalledTimes(1);
expect(function () {
return wrapper.unmount();
}).not.toThrow();
case 14:
case "end":
return _context4.stop();
}
}
}, _callee4);
})));
it('should render text when hover it', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5() {
var wrapper;
return _regeneratorRuntime.wrap(function _callee5$(_context5) {
while (1) {
switch (_context5.prev = _context5.next) {
case 0:
wrapper = mount( /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(Tooltip, {
text: "some text"
}, "some tips")));
wrapper.find('.tooltip').simulate('mouseEnter', nativeEvent);
_context5.next = 4;
return updateWrapper(wrapper, 150);
case 4:
expectTooltipIsShow(wrapper);
wrapper.find('.tooltip').simulate('mouseLeave', nativeEvent);
_context5.next = 8;
return updateWrapper(wrapper, 400);
case 8:
expectTooltipIsHidden(wrapper);
expect(function () {
return wrapper.unmount();
}).not.toThrow();
case 10:
case "end":
return _context5.stop();
}
}
}, _callee5);
})));
it('should render react-node when click it', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee6() {
var wrapper, testNode;
return _regeneratorRuntime.wrap(function _callee6$(_context6) {
while (1) {
switch (_context6.prev = _context6.next) {
case 0:
wrapper = mount( /*#__PURE__*/React.createElement(Tooltip, {
text: /*#__PURE__*/React.createElement("p", {
id: "test"
}, "custom-content"),
trigger: "click"
}, /*#__PURE__*/React.createElement("span", null, "click me")));
wrapper.find('.tooltip').simulate('click', nativeEvent);
_context6.next = 4;
return updateWrapper(wrapper, 150);
case 4:
expectTooltipIsShow(wrapper);
testNode = wrapper.find('#test');
expect(testNode.length).not.toBe(0);
expect(testNode.text()).toContain('custom-content');
act(function () {
document.body.dispatchEvent(new MouseEvent('click', {
view: window,
bubbles: true,
cancelable: true
}));
});
_context6.next = 11;
return updateWrapper(wrapper, 400);
case 11:
expectTooltipIsHidden(wrapper);
expect(function () {
return wrapper.unmount();
}).not.toThrow();
case 13:
case "end":
return _context6.stop();
}
}
}, _callee6);
})));
it('should render inner components', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee7() {
var wrapper;
return _regeneratorRuntime.wrap(function _callee7$(_context7) {
while (1) {
switch (_context7.prev = _context7.next) {
case 0:
wrapper = mount( /*#__PURE__*/React.createElement(Tooltip, {
text: "some text",
color: "dark"
}, /*#__PURE__*/React.createElement(Button, {
auto: true,
size: "small",
id: "test"
}, "button")));
expect(wrapper.find('#test').length).not.toBe(0);
expect(function () {
return wrapper.unmount();
}).not.toThrow();
case 3:
case "end":
return _context7.stop();
}
}
}, _callee7);
})));
it('should render correctly by visible', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee8() {
var wrapper;
return _regeneratorRuntime.wrap(function _callee8$(_context8) {
while (1) {
switch (_context8.prev = _context8.next) {
case 0:
wrapper = mount( /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(Tooltip, {
text: /*#__PURE__*/React.createElement("p", {
id: "visible"
}, "custom-content"),
visible: true,
placement: "right-end"
}, "some tips")));
_context8.next = 3;
return updateWrapper(wrapper, 150);
case 3:
expect(wrapper.find('#visible').length).toBe(1);
expect(function () {
return wrapper.unmount();
}).not.toThrow();
case 5:
case "end":
return _context8.stop();
}
}
}, _callee8);
})));
it('should render correctly by using wrong placement', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee9() {
var wrapper;
return _regeneratorRuntime.wrap(function _callee9$(_context9) {
while (1) {
switch (_context9.prev = _context9.next) {
case 0:
wrapper = mount( /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(Tooltip, {
text: /*#__PURE__*/React.createElement("p", {
id: "default-visible"
}, "custom-content"),
defaultVisible: true,
placement: 'test'
}, "some tips")));
expect(wrapper.find('#default-visible').length).toBe(1);
expect(function () {
return wrapper.unmount();
}).not.toThrow();
case 3:
case "end":
return _context9.stop();
}
}
}, _callee9);
})));
it('should have the hoverable effect', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee20() {
var onVisibleChange, onMouseLeave, wrapper;
return _regeneratorRuntime.wrap(function _callee20$(_context20) {
while (1) {
switch (_context20.prev = _context20.next) {
case 0:
onVisibleChange = jest.fn();
onMouseLeave = jest.fn();
wrapper = mount( /*#__PURE__*/React.createElement(Tooltip, {
placement: "top",
text: "some text",
hoverable: true,
hoverableTimeout: 200,
offset: [0, 0],
onMouseLeave: onMouseLeave
}, "tooltip")); // normal show hide
// on
_context20.next = 5;
return act( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee10() {
return _regeneratorRuntime.wrap(function _callee10$(_context10) {
while (1) {
switch (_context10.prev = _context10.next) {
case 0:
wrapper.find('.tooltip').simulate('mouseEnter', nativeEvent);
_context10.next = 3;
return updateWrapper(wrapper, 0);
case 3:
case "end":
return _context10.stop();
}
}
}, _callee10);
})));
case 5:
expectTooltipIsShow(wrapper); // off
_context20.next = 8;
return act( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee11() {
return _regeneratorRuntime.wrap(function _callee11$(_context11) {
while (1) {
switch (_context11.prev = _context11.next) {
case 0:
wrapper.find('.tooltip').simulate('mouseLeave', nativeEvent);
_context11.next = 3;
return updateWrapper(wrapper, 0);
case 3:
case "end":
return _context11.stop();
}
}
}, _callee11);
})));
case 8:
expectTooltipIsShow(wrapper);
_context20.next = 11;
return updateWrapper(wrapper, 250);
case 11:
expectTooltipIsHidden(wrapper);
wrapper.setProps({
onVisibleChange: onVisibleChange
}); // hover tooltip -> leave t and hover content -> leave content
// on
_context20.next = 15;
return act( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee12() {
return _regeneratorRuntime.wrap(function _callee12$(_context12) {
while (1) {
switch (_context12.prev = _context12.next) {
case 0:
wrapper.find('.tooltip').simulate('mouseEnter', nativeEvent);
_context12.next = 3;
return updateWrapper(wrapper, 0);
case 3:
case "end":
return _context12.stop();
}
}
}, _callee12);
})));
case 15:
expectTooltipIsShow(wrapper); // off
_context20.next = 18;
return act( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee13() {
return _regeneratorRuntime.wrap(function _callee13$(_context13) {
while (1) {
switch (_context13.prev = _context13.next) {
case 0:
wrapper.find('.tooltip').simulate('mouseLeave', nativeEvent);
_context13.next = 3;
return updateWrapper(wrapper, 0);
case 3:
case "end":
return _context13.stop();
}
}
}, _callee13);
})));
case 18:
expectTooltipIsShow(wrapper); // on content
_context20.next = 21;
return act( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee14() {
return _regeneratorRuntime.wrap(function _callee14$(_context14) {
while (1) {
switch (_context14.prev = _context14.next) {
case 0:
wrapper.find('.tooltip-content').simulate('mouseEnter', nativeEvent);
_context14.next = 3;
return updateWrapper(wrapper, 0);
case 3:
case "end":
return _context14.stop();
}
}
}, _callee14);
})));
case 21:
expectTooltipIsShow(wrapper); // off content
_context20.next = 24;
return act( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee15() {
return _regeneratorRuntime.wrap(function _callee15$(_context15) {
while (1) {
switch (_context15.prev = _context15.next) {
case 0:
wrapper.find('.tooltip-content').simulate('mouseLeave', nativeEvent);
_context15.next = 3;
return updateWrapper(wrapper, 0);
case 3:
case "end":
return _context15.stop();
}
}
}, _callee15);
})));
case 24:
expectTooltipIsHidden(wrapper); // hover t -> hover c and leave t -> leave c
// on
_context20.next = 27;
return act( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee16() {
return _regeneratorRuntime.wrap(function _callee16$(_context16) {
while (1) {
switch (_context16.prev = _context16.next) {
case 0:
wrapper.find('.tooltip').simulate('mouseEnter', nativeEvent);
_context16.next = 3;
return updateWrapper(wrapper, 0);
case 3:
case "end":
return _context16.stop();
}
}
}, _callee16);
})));
case 27:
expectTooltipIsShow(wrapper); // on content
_context20.next = 30;
return act( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee17() {
return _regeneratorRuntime.wrap(function _callee17$(_context17) {
while (1) {
switch (_context17.prev = _context17.next) {
case 0:
wrapper.find('.tooltip-content').simulate('mouseEnter', nativeEvent);
_context17.next = 3;
return updateWrapper(wrapper, 0);
case 3:
case "end":
return _context17.stop();
}
}
}, _callee17);
})));
case 30:
expectTooltipIsShow(wrapper); // off
_context20.next = 33;
return act( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee18() {
return _regeneratorRuntime.wrap(function _callee18$(_context18) {
while (1) {
switch (_context18.prev = _context18.next) {
case 0:
wrapper.find('.tooltip').simulate('mouseLeave', nativeEvent);
_context18.next = 3;
return updateWrapper(wrapper, 0);
case 3:
case "end":
return _context18.stop();
}
}
}, _callee18);
})));
case 33:
_context20.next = 35;
return updateWrapper(wrapper, 250);
case 35:
expectTooltipIsShow(wrapper); // off content
_context20.next = 38;
return act( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee19() {
return _regeneratorRuntime.wrap(function _callee19$(_context19) {
while (1) {
switch (_context19.prev = _context19.next) {
case 0:
wrapper.find('.tooltip-content').simulate('mouseLeave', nativeEvent);
_context19.next = 3;
return updateWrapper(wrapper, 0);
case 3:
case "end":
return _context19.stop();
}
}
}, _callee19);
})));
case 38:
expectTooltipIsHidden(wrapper);
expect(function () {
return wrapper.unmount();
}).not.toThrow();
case 40:
case "end":
return _context20.stop();
}
}
}, _callee20);
})));
});