@shko.online/componentframework-mock
Version:
Mocking library to help with testing PowerApps Component Framework Components
92 lines (90 loc) • 3.87 kB
JavaScript
/*
Copyright (c) 2022 Betim Beja and Shko Online LLC
Licensed under the MIT license.
*/
import { spy, stub } from 'sinon';
import { MetadataDB } from './Metadata.db';
import { mockGetEntityMetadata } from './mockGetEntityMetadata';
import { mockSetControlState } from './mockSetControlState';
import { mockSetControlResource } from './mockSetControlResource';
import { mockRefreshParameters } from './mockRefreshParameters';
import { mockNotifyOutputChanged } from './mockNotifyOutputChanged';
import { ContextMock, DataSetMock } from '../ComponentFramework-Mock';
import { showBanner } from '../utils';
import { mockRefreshDatasets } from './mockRefreshDatasets';
/**
* This class mocks the standard version of the PowerApps Component Framework control.
*/
export class ComponentFrameworkMockGenerator {
constructor(control, inputs, container, outputs, overrides) {
var _overrides$metadata, _this$control$getOutp;
this._PendingUpdates = void 0;
this.RefreshDatasets = void 0;
this.RefreshParameters = void 0;
this.SetControlResource = void 0;
this.UpdateValues = void 0;
this.container = void 0;
this.context = void 0;
this.control = void 0;
this.metadata = void 0;
this.notifyOutputChanged = void 0;
this.onOutputChanged = void 0;
this.outputOnlyProperties = void 0;
this.resizeObserver = void 0;
this.state = void 0;
showBanner(control.name);
this._PendingUpdates = [];
this.state = {};
this.outputOnlyProperties = {};
this.container = container !== null && container !== void 0 ? container : document.createElement('div');
this.control = spy(new control());
this.metadata = (_overrides$metadata = overrides === null || overrides === void 0 ? void 0 : overrides.metadata) !== null && _overrides$metadata !== void 0 ? _overrides$metadata : new MetadataDB();
this.context = new ContextMock(inputs, this.metadata);
this.resizeObserver = new ResizeObserver(entries => {
const size = entries[0];
this.context.mode.allocatedHeight = size.contentRect.height;
this.context.mode.allocatedWidth = size.contentRect.width;
this.ExecuteUpdateView();
});
this.context.mode.trackContainerResize.callsFake(value => {
if (value) this.resizeObserver.observe(this.container);else this.resizeObserver.unobserve(this.container);
});
const inputProperties = Object.getOwnPropertyNames(this.context._parameters);
inputProperties.forEach(p => {
const parameter = this.context._parameters[p];
if (parameter instanceof DataSetMock) {
parameter._updateView = this.ExecuteUpdateView.bind(this);
}
});
if (outputs) {
Object.getOwnPropertyNames(outputs).forEach(p => {
if (inputProperties.includes(p)) {
return;
}
this.outputOnlyProperties[p] = outputs[p];
});
}
this.UpdateValues = stub();
mockGetEntityMetadata(this);
this.notifyOutputChanged = stub();
mockNotifyOutputChanged(this, (_this$control$getOutp = this.control.getOutputs) === null || _this$control$getOutp === void 0 ? void 0 : _this$control$getOutp.bind(this.control), this.ExecuteUpdateView.bind(this));
this.onOutputChanged = stub();
this.RefreshParameters = stub();
mockRefreshParameters(this);
this.RefreshDatasets = stub();
mockRefreshDatasets(this, this.ExecuteUpdateView.bind(this));
this.SetControlResource = stub();
mockSetControlResource(this);
mockSetControlState(this);
}
ExecuteInit() {
this.RefreshParameters();
const state = this.state === undefined ? this.state : Object.assign({}, this.state);
this.control.init(this.context, this.notifyOutputChanged, state, this.container);
}
ExecuteUpdateView() {
this.RefreshParameters();
this.control.updateView(this.context);
this.RefreshDatasets();
}
}