UNPKG

scriptable-testlab

Version:

A lightweight, efficient tool designed to manage and update scripts for Scriptable.

142 lines 4.86 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); var registry_exports = {}; __export(registry_exports, { GlobalMockManager: () => GlobalMockManager, GlobalRegistry: () => GlobalRegistry, MockError: () => MockError, MockErrorCode: () => MockErrorCode }); module.exports = __toCommonJS(registry_exports); var import_mocker = __toESM(require("@corez/mocker")); var import_mocks = require("./mocks"); class MockError extends Error { constructor(code, message, details) { super(message); this.code = code; this.message = message; this.details = details; this.name = "MockError"; } } const MockErrorCode = { INVALID_TARGET: "INVALID_TARGET" }; class GlobalRegistry { /** * Register a mock object globally */ static register(identifier, mockObject) { if (mockObject != null && (typeof mockObject === "object" || typeof mockObject === "function")) { this.registry.set(identifier, mockObject); global[identifier] = mockObject; } } /** * Unregister a mock object */ static unregister(identifier) { if (this.registry.has(identifier)) { this.registry.delete(identifier); delete global[identifier]; } } /** * Get a registered mock object */ static get(identifier) { const mockObject = this.registry.get(identifier); if (!mockObject) { throw { code: MockErrorCode.INVALID_TARGET, message: `Mock not found: ${identifier}` }; } return mockObject; } /** * Check if identifier is registered */ static has(identifier) { return this.registry.has(identifier); } /** * Clear all registered mocks */ static clear() { this.registry.forEach((_, identifier) => { delete global[identifier]; }); this.registry.clear(); } /** * Get all registered identifiers */ static getRegisteredIdentifiers() { return Array.from(this.registry.keys()); } } __publicField(GlobalRegistry, "registry", /* @__PURE__ */ new Map()); class GlobalMockManager { static initialize() { this.clear(); import_mocks.GLOBAL_CLASSES.forEach((Class) => { Class.initialize(Class, (instance) => import_mocker.default.object(instance, { inPlace: true, callThrough: true })); GlobalRegistry.register(Class.identifier, Class); }); import_mocks.GLOBAL_VARIABLES.forEach((Variable) => { GlobalRegistry.register( Variable.identifier, import_mocker.default.object(Variable.instance, { inPlace: true, callThrough: true }) ); }); } static clear() { GlobalRegistry.getRegisteredIdentifiers().forEach((identifier) => { if (GlobalRegistry.has(identifier)) { const mockObject = GlobalRegistry.get(identifier); if (import_mocker.default.isMock(mockObject)) { mockObject.mockClear(); } } }); GlobalRegistry.clear(); import_mocks.GLOBAL_CLASSES.forEach((Class) => Class.clearInitializer(Class)); import_mocks.GLOBAL_VARIABLES.forEach((Variable) => Variable.clearInitializer(Variable)); } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { GlobalMockManager, GlobalRegistry, MockError, MockErrorCode }); //# sourceMappingURL=registry.js.map