@uni/loading
Version:
224 lines (201 loc) • 7.53 kB
JavaScript
function _createForOfIteratorHelperLoose(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (it) return (it = it.call(o)).next.bind(it); if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; return function () { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }; } throw new TypeError("Invalid attempt to iterate 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 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); }); }; }
import { JSDOM } from "jsdom"; // Note: `ali` includes: `dingtalk`
export var mapContainerToGlobalsKey = {
web: 'window',
wechat: 'wx',
ali: 'my',
dingtalk: 'dd',
bytedance: 'tt',
kuaishou: 'ks',
baidu: 'swan'
};
export function createNoop() {
return function () {};
}
export var noop = createNoop();
export function sleep(ms) {
return new Promise(function (resolve) {
setTimeout(resolve, ms);
});
}
export function createPromisifyImpl(value) {
if (value === void 0) {
value = {};
}
return function (args) {
setTimeout(function () {
args == null ? void 0 : args.success == null ? void 0 : args.success(value);
}, 0);
};
}
export function isAliContainer(container) {
return container === 'ali' || container === 'dingtalk';
}
/**
* run test in mock container environment for special api
* @param container
* @param callback
* @example see example at: src/packages/interactive/animation/__test__/api.test.ts
*/
export function testContainerAPI(container, callback) {
var map = {
web: {},
wechat: {
wx: {
request: noop
}
},
ali: {
my: {
alert: noop
}
},
dingtalk: {
my: {
alert: noop
},
dd: {
alert: noop
}
},
bytedance: {
tt: {
showToast: noop
}
},
kuaishou: {
ks: {
showToast: noop
}
},
baidu: {
swan: {
showToast: noop
}
}
};
if (container === 'web') {
var _JSDOM = new JSDOM(),
window = _JSDOM.window;
map.web.window = window;
map.web.document = window.document; // innerText implementation
// @see https://github.com/jsdom/jsdom/issues/1245
Object.defineProperty(window.HTMLElement.prototype, 'innerText', {
get: function get() {
return this.textContent;
},
set: function set(val) {
this.innerHTML = val.replace(/</g, '<').replace(/>/g, '>');
}
});
}
var globals = map[container] || {};
var proxyGlobals = new Proxy({}, {
set: function set(target, p, value, receiver) {
global[p] = value;
return Reflect.set(target, p, value, receiver);
},
defineProperty: function defineProperty(target, p, attributes) {
Object.defineProperty(global, p, attributes);
return Reflect.defineProperty(target, p, attributes);
},
deleteProperty: function deleteProperty(target, p) {
delete global[p];
return Reflect.deleteProperty(target, p);
}
});
test("Test container: " + container, /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
// 执行测试前清除 require 模块缓存
jest.resetModules(); // set
Object.assign(proxyGlobals, globals);
_context.prev = 2;
_context.next = 5;
return callback(proxyGlobals);
case 5:
_context.prev = 5;
// clear
Object.keys(proxyGlobals).forEach(function (k) {
delete global[k];
});
return _context.finish(5);
case 8:
case "end":
return _context.stop();
}
}
}, _callee, null, [[2,, 5, 8]]);
})));
}
/**
* batch run test in multiple mock container environment for special api
* @param name
* @param containers
* @param callback
*/
export function testPlatformAPI(name, containers, callback) {
describe("Test Platform API: " + name, function () {
var _loop = function _loop() {
var container = _step.value;
testContainerAPI(container, /*#__PURE__*/function () {
var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(globals) {
var configAPI;
return regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
configAPI = function configAPI(api, mockImpl) {
var key = mapContainerToGlobalsKey[container];
if (globals[key]) {
globals[key][api] = mockImpl;
}
};
_context2.next = 3;
return callback(container, globals, configAPI);
case 3:
case "end":
return _context2.stop();
}
}
}, _callee2);
}));
return function (_x) {
return _ref2.apply(this, arguments);
};
}());
};
for (var _iterator = _createForOfIteratorHelperLoose(containers), _step; !(_step = _iterator()).done;) {
_loop();
}
});
}
export function testWebAPI(name, callback) {
describe("Test Platform API: " + name, function () {
testContainerAPI('web', /*#__PURE__*/function () {
var _ref3 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3(globals) {
return regeneratorRuntime.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
_context3.next = 2;
return callback(globals);
case 2:
case "end":
return _context3.stop();
}
}
}, _callee3);
}));
return function (_x2) {
return _ref3.apply(this, arguments);
};
}());
});
}