@shko.online/componentframework-mock
Version:
Mocking library to help with testing PowerApps Component Framework Components
135 lines (133 loc) • 5.95 kB
JavaScript
/*
Copyright (c) 2022 Betim Beja and Shko Online LLC
Licensed under the MIT license.
*/
import { arrayEqual } from '../utils';
/**
* Mocks the following APIs
* - {@link ComponentFramework.Mode.setFullScreen}
* - {@link ComponentFramework.Factory.requestRender}
* - notifyOutputChanged function
* @param mockGenerator The generator that controlls the context
* @param getOutputs The plugin getOutputs function
* @param executeUpdateView The callback function that triggers UpdateView from the framework
*/
export const mockNotifyOutputChanged = (mockGenerator, getOutputs, executeUpdateView) => {
mockGenerator.context.factory.requestRender.callsFake(() => {
setInterval(() => executeUpdateView(), 0);
});
mockGenerator.context.mode.setFullScreen.callsFake(value => {
mockGenerator.context.updatedProperties = [];
if (mockGenerator.context.mode._FullScreen != value) {
mockGenerator.context.updatedProperties.push(value ? 'fullscreen_open' : 'fullscreen_close');
}
mockGenerator.context.mode._FullScreen = value;
executeUpdateView();
});
mockGenerator.UpdateValues.callsFake(items => {
mockGenerator.context.updatedProperties = [];
for (const k in items) {
if (k in mockGenerator.context._parameters) {
let isLookup = false;
const property = mockGenerator.context._parameters[k];
if (Array.isArray(items[k])) {
const arrayUpdate = items[k];
if (arrayUpdate && typeof arrayUpdate[0] === 'object') {
isLookup = true;
}
const property = mockGenerator.context._parameters[k];
if (!arrayEqual(arrayUpdate, property.raw)) {
mockGenerator.context.updatedProperties.push(k);
} else {
continue;
}
} else if (items[k] instanceof Date) {
var _raw, _items$k;
if (((_raw = mockGenerator.context._parameters[k].raw) === null || _raw === void 0 ? void 0 : _raw.getTime()) !== ((_items$k = items[k]) === null || _items$k === void 0 ? void 0 : _items$k.getTime())) {
mockGenerator.context.updatedProperties.push(k);
} else {
continue;
}
} else if (typeof items[k] === 'object') {
isLookup = true;
const lookup = items[k];
const property = mockGenerator.context._parameters[k];
if (property.raw === null || property.raw.length === 0 || property.raw[0].entityType !== lookup.entityType || property.raw[0].id !== lookup.id || property.raw[0].name !== lookup.name) {
mockGenerator.context.updatedProperties.push(k);
} else {
continue;
}
} else if (mockGenerator.context._parameters[k].raw !== items[k]) {
mockGenerator.context.updatedProperties.push(k);
} else {
continue;
}
mockGenerator._PendingUpdates.push({
column: property._boundColumn,
row: property._boundRow,
table: property._boundTable,
value: isLookup ? items[k][0] : items[k]
});
}
}
if (mockGenerator.context.updatedProperties.length > 0) {
mockGenerator.context.updatedProperties.push('parameters');
}
});
mockGenerator.notifyOutputChanged.callsFake(() => {
var _mockGenerator$onOutp;
const updates = getOutputs === null || getOutputs === void 0 ? void 0 : getOutputs();
if (!updates) return;
mockGenerator.context.updatedProperties = [];
for (const k in updates) {
if (k in mockGenerator.context._parameters) {
let isLookup = false;
const property = mockGenerator.context._parameters[k];
if (Array.isArray(updates[k])) {
const arrayUpdate = updates[k];
if (arrayUpdate && typeof arrayUpdate[0] === 'object') {
isLookup = true;
}
const property = mockGenerator.context._parameters[k];
if (!arrayEqual(arrayUpdate, property.raw)) {
mockGenerator.context.updatedProperties.push(k);
} else {
continue;
}
} else if (updates[k] instanceof Date) {
var _raw2, _updates$k;
if (((_raw2 = mockGenerator.context._parameters[k].raw) === null || _raw2 === void 0 ? void 0 : _raw2.getTime()) !== ((_updates$k = updates[k]) === null || _updates$k === void 0 ? void 0 : _updates$k.getTime())) {
mockGenerator.context.updatedProperties.push(k);
} else {
continue;
}
} else if (typeof updates[k] === 'object') {
isLookup = true;
const lookup = updates[k];
const property = mockGenerator.context._parameters[k];
if (property.raw === null || property.raw.length === 0 || property.raw[0].entityType !== lookup.entityType || property.raw[0].id !== lookup.id || property.raw[0].name !== lookup.name) {
mockGenerator.context.updatedProperties.push(k);
} else {
continue;
}
} else if (mockGenerator.context._parameters[k].raw !== updates[k]) {
mockGenerator.context.updatedProperties.push(k);
} else {
continue;
}
if (isLookup) {
mockGenerator.metadata.UpdateValue(updates[k][0], property._boundTable, property._boundColumn, property._boundRow);
continue;
}
mockGenerator.metadata.UpdateValue(updates[k], property._boundTable, property._boundColumn, property._boundRow);
} else if (k in mockGenerator.outputOnlyProperties) {
mockGenerator.context.updatedProperties.push(k);
}
}
if (mockGenerator.context.updatedProperties.length > 0) {
mockGenerator.context.updatedProperties.push('parameters');
}
executeUpdateView();
(_mockGenerator$onOutp = mockGenerator.onOutputChanged) === null || _mockGenerator$onOutp === void 0 || _mockGenerator$onOutp.call(mockGenerator, updates);
});
};