UNPKG

@meve/ui

Version:

Argon design system components library

289 lines (265 loc) 7.49 kB
import _extends from "@babel/runtime/helpers/esm/extends"; import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator"; import _regeneratorRuntime from "@babel/runtime/regenerator"; import Vue from 'vue'; import { config } from '@vue/test-utils'; import { isPlainObject } from './shared'; export var delay = function delay(time) { if (time === void 0) { time = 0; } return new Promise(function (resolve) { return setTimeout(resolve, time); }); }; export function mockOffset(_temp) { var _ref = _temp === void 0 ? {} : _temp, offsetWidth = _ref.offsetWidth, offsetHeight = _ref.offsetHeight, clientWidth = _ref.clientWidth, clientHeight = _ref.clientHeight, offsetLeft = _ref.offsetLeft, offsetTop = _ref.offsetTop, scrollWidth = _ref.scrollWidth, scrollHeight = _ref.scrollHeight; Object.defineProperties(HTMLElement.prototype, { offsetWidth: { get: function get() { return offsetWidth != null ? offsetWidth : parseFloat(window.getComputedStyle(this).width) || 0; } }, offsetHeight: { get: function get() { return offsetHeight != null ? offsetHeight : parseFloat(window.getComputedStyle(this).height) || 0; } }, clientWidth: { get: function get() { return clientWidth != null ? clientWidth : parseFloat(window.getComputedStyle(this).width) || 0; } }, clientHeight: { get: function get() { return clientHeight != null ? clientHeight : parseFloat(window.getComputedStyle(this).height) || 0; } }, offsetLeft: { get: function get() { return offsetLeft != null ? offsetLeft : parseFloat(window.getComputedStyle(this).marginLeft) || 0; } }, offsetTop: { get: function get() { return offsetTop != null ? offsetTop : parseFloat(window.getComputedStyle(this).marginTop) || 0; } }, offsetParent: { get: function get() { var _this$parentNode; return (_this$parentNode = this.parentNode) != null ? _this$parentNode : {}; } }, scrollWidth: { get: function get() { return scrollWidth != null ? scrollWidth : 0; } }, scrollHeight: { get: function get() { return scrollHeight != null ? scrollHeight : 0; } } }); return { mockRestore: function mockRestore() { Object.defineProperties(HTMLElement.prototype, { offsetWidth: { get: function get() { return parseFloat(window.getComputedStyle(this).width) || 0; } }, offsetHeight: { get: function get() { return parseFloat(window.getComputedStyle(this).height) || 0; } }, clientWidth: { get: function get() { return parseFloat(window.getComputedStyle(this).width) || 0; } }, clientHeight: { get: function get() { return parseFloat(window.getComputedStyle(this).height) || 0; } }, offsetLeft: { get: function get() { return parseFloat(window.getComputedStyle(this).marginLeft) || 0; } }, offsetTop: { get: function get() { return parseFloat(window.getComputedStyle(this).marginTop) || 0; } }, offsetParent: { get: function get() { var _this$parentNode2; return (_this$parentNode2 = this.parentNode) != null ? _this$parentNode2 : {}; } }, scrollWidth: { get: function get() { return 0; } }, scrollHeight: { get: function get() { return 0; } } }); } }; } export function triggerDrag(_x, _x2, _x3, _x4, _x5) { return _triggerDrag.apply(this, arguments); } function _triggerDrag() { _triggerDrag = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(el, x, y, triggerTouchend, triggerDocument) { var delegationEl; return _regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: if (x === void 0) { x = 0; } if (y === void 0) { y = 0; } if (triggerTouchend === void 0) { triggerTouchend = true; } if (triggerDocument === void 0) { triggerDocument = false; } delegationEl = triggerDocument ? document : el; _context.next = 7; return trigger(el, 'touchstart', 0, 0); case 7: _context.next = 9; return trigger(delegationEl, 'touchmove', x / 4, y / 4); case 9: _context.next = 11; return trigger(delegationEl, 'touchmove', x / 3, y / 3); case 11: _context.next = 13; return trigger(delegationEl, 'touchmove', x / 2, y / 2); case 13: _context.next = 15; return trigger(delegationEl, 'touchmove', x, y); case 15: if (!triggerTouchend) { _context.next = 18; break; } _context.next = 18; return trigger(delegationEl, 'touchend', x, y); case 18: case "end": return _context.stop(); } } }, _callee); })); return _triggerDrag.apply(this, arguments); } export function getTouch(el, x, y) { return { identifier: Date.now(), target: el, pageX: x, pageY: y, clientX: x, clientY: y, radiusX: 2.5, radiusY: 2.5, rotationAngle: 10, force: 0.5 }; } export function trigger(wrapper, eventName, x, y, deltaX, deltaY, offsetX, offsetY, args) { if (x === void 0) { x = 0; } if (y === void 0) { y = 0; } if (deltaX === void 0) { deltaX = 0; } if (deltaY === void 0) { deltaY = 0; } if (offsetX === void 0) { offsetX = 0; } if (offsetY === void 0) { offsetY = 0; } if (args === void 0) { args = {}; } var el = 'element' in wrapper ? wrapper.element : wrapper; var touchList = [getTouch(el, x, y)]; var event = document.createEvent('CustomEvent'); event.initCustomEvent(eventName, true, true, {}); Object.assign(event, _extends({ clientX: x, clientY: y, deltaX: deltaX, deltaY: deltaY, offsetX: offsetX, offsetY: offsetY, touches: touchList, targetTouches: touchList, changedTouches: touchList }, args)); el.dispatchEvent(event); return Vue.nextTick(); } export function mockStubs() { var originStubs = config.stubs; config.stubs = {}; return { mockRestore: function mockRestore() { config.stubs = originStubs; } }; } export function mockDoubleRaf() { var originMethod = window.requestAnimationFrame; Object.assign(window, { requestAnimationFrame: function requestAnimationFrame(fn) { setTimeout(fn, 16); } }); return { mockRestore: function mockRestore() { window.requestAnimationFrame = originMethod; } }; } export function mockScrollTo(Element) { Element.prototype.scrollTo = function (x, y) { if (isPlainObject(x)) { this.scrollLeft = x.left; this.scrollTop = x.top; } else { this.scrollLeft = x; this.scrollTop = y; } }; }