UNPKG

office-addin-mock

Version:
225 lines 9.13 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.OfficeMockObject = void 0; const office_addin_manifest_1 = require("office-addin-manifest"); const host_1 = require("./host"); const possibleErrors_1 = require("./possibleErrors"); /** * Creates an office-js mockable object * @param object Object structure to provide initial values for the mock object (Optional) * @param host Host tested by the object (Optional) */ class OfficeMockObject { constructor(object, host) { this._properties = new Map(); this._loaded = false; if (host) { this._host = host; } else { this._host = (0, host_1.getHostType)(object); } this.resetValue(undefined); if (object) { this.populate(object); } } /** * Mock replacement of the load method in the Office.js API * @param propertyArgument Argument of the load call. Will load any properties in the argument */ load(propertyArgument) { if (this._host === office_addin_manifest_1.OfficeApp.Outlook) { return; } let properties = []; if (propertyArgument === undefined) { // an empty load call mean load all properties properties = ["*"]; } else if (typeof propertyArgument === "string") { properties = Array(propertyArgument); } else if (Array.isArray(propertyArgument)) { properties = propertyArgument; } else { properties = this.parseObjectPropertyIntoArray(propertyArgument); } properties.forEach((property) => { this.loadMultipleProperties(property); }); return this; } /** * Mock replacement for the sync method in the Office.js API */ sync() { return __awaiter(this, void 0, void 0, function* () { this._properties.forEach((property, key) => __awaiter(this, void 0, void 0, function* () { yield property.sync(); this.updatePropertyCall(key); })); if (this._loaded) { this._value = this._valueBeforeLoaded; } }); } /** * addMock(name) will add a property named “name”, with a new OfficeMockObject as its value, to the object * @param objectName Object name of the object to be added */ addMock(objectName) { if (this[objectName] !== undefined) { throw new Error("Mock object already exists"); } const officeMockObject = new OfficeMockObject(undefined, this._host); officeMockObject._isObject = true; this._properties.set(objectName, officeMockObject); this[objectName] = this._properties.get(objectName); } loadAllProperties() { this._properties.forEach((property, propertyName) => { property.loadCalled(); this.updatePropertyCall(propertyName); }); } loadCalled() { if (!this._loaded) { this._loaded = true; this._value = possibleErrors_1.PossibleErrors.notSync; } } loadMultipleProperties(properties) { if (properties === "*") { this.loadAllProperties(); } else { properties .replace(/\s/g, "") .split(",") .forEach((completeProperties) => { this.loadNavigational(completeProperties); }); } } loadNavigational(completePropertyName) { const properties = completePropertyName.split("/"); let navigationalOfficeMockObject = this; // Iterating through navigational properties for (let i = 0; i < properties.length - 1; i++) { const property = properties[i]; const retrievedProperty = navigationalOfficeMockObject._properties.get(property); if (retrievedProperty) { navigationalOfficeMockObject = retrievedProperty; } else { throw new Error(`Navigational property ${property} needs to be present in object model before load is called.`); } } const scalarProperty = properties[properties.length - 1]; navigationalOfficeMockObject.loadScalar(scalarProperty); } loadScalar(scalarPropertyName) { var _a, _b; if (this._properties.has(scalarPropertyName)) { (_a = this._properties.get(scalarPropertyName)) === null || _a === void 0 ? void 0 : _a.loadCalled(); this.updatePropertyCall(scalarPropertyName); (_b = this._properties .get(scalarPropertyName)) === null || _b === void 0 ? void 0 : _b._properties.forEach((property) => { property.loadCalled(); }); } else { throw new Error(`Property ${scalarPropertyName} needs to be present in object model before load is called.`); } } parseObjectPropertyIntoArray(objectData) { let composedProperties = []; Object.keys(objectData).forEach((propertyName) => { const property = this._properties.get(propertyName); if (property) { const propertyValue = objectData[propertyName]; if (property._isObject) { const composedProperty = property.parseObjectPropertyIntoArray(propertyValue); if (composedProperty.length !== 0) { composedProperty.forEach((prop) => { composedProperties = composedProperties.concat(propertyName + "/" + prop); }); } } else if (propertyValue) { composedProperties = composedProperties.concat(propertyName); } } else { throw new Error(`Property ${propertyName} needs to be present in object model before load is called.`); } }); return composedProperties; } populate(objectData) { Object.keys(objectData).forEach((propertyName) => { const property = objectData[propertyName]; const dataType = typeof property; if (dataType === "object" && !Array.isArray(property) && !(property instanceof Date)) { this.addMock(propertyName); this[propertyName].populate(property); } else { this.setValue(propertyName, property); } }); } resetValue(value) { if (this._host === office_addin_manifest_1.OfficeApp.Outlook) { this._value = value; } else { this._value = possibleErrors_1.PossibleErrors.notLoaded; this._valueBeforeLoaded = value; this._loaded = false; } } /** * Sets a property of any type or function to the object * @param propertyName Property name to the property to be added * @param value Value this added property will have */ setValue(propertyName, value) { var _a, _b; if (typeof value === "function") { this[propertyName] = value; } else { if (!this._properties.has(propertyName)) { const officeMockObject = new OfficeMockObject(undefined, this._host); officeMockObject._isObject = false; this._properties.set(propertyName, officeMockObject); } (_a = this._properties.get(propertyName)) === null || _a === void 0 ? void 0 : _a.resetValue(value); this[propertyName] = (_b = this._properties.get(propertyName)) === null || _b === void 0 ? void 0 : _b._value; } } updatePropertyCall(propertyName) { var _a, _b; if ((_a = this._properties.get(propertyName)) === null || _a === void 0 ? void 0 : _a._isObject) { this[propertyName] = this._properties.get(propertyName); } else if ((0, possibleErrors_1.isValidError)(this[propertyName])) { // It is a known error this[propertyName] = (_b = this._properties.get(propertyName)) === null || _b === void 0 ? void 0 : _b._value; } } } exports.OfficeMockObject = OfficeMockObject; //# sourceMappingURL=officeMockObject.js.map