@shko.online/componentframework-mock
Version:
Mocking library to help with testing PowerApps Component Framework Components
78 lines (76 loc) • 3 kB
JavaScript
/*
Copyright (c) 2022 Betim Beja and Shko Online LLC
Licensed under the MIT license.
*/
import { createElement } from 'react';
import { spy, stub } from 'sinon';
import { ContextMock } from '../ComponentFramework-Mock';
import { MetadataDB } from './Metadata.db';
import { ReactResizeObserver } from './ReactResizeObserver';
import { showBanner } from '../utils';
import { mockGetEntityMetadata } from './mockGetEntityMetadata';
import { mockSetControlState } from './mockSetControlState';
import { mockSetControlResource } from './mockSetControlResource';
import { mockRefreshParameters } from './mockRefreshParameters';
/**
* This class mocks the React version of the PowerApps Component Framework control.
*/
export class ComponentFrameworkMockGeneratorReact {
constructor(control, inputs, outputs, overrides) {
var _overrides$metadata;
this._PendingUpdates = void 0;
this.RefreshParameters = void 0;
this.RefreshDatasets = void 0;
this.UpdateValues = void 0;
this.context = void 0;
this.control = void 0;
this.notifyOutputChanged = void 0;
this.onOutputChanged = void 0;
this.outputOnlyProperties = void 0;
this.resizeObserver = void 0;
this.state = void 0;
this.SetControlResource = void 0;
this.metadata = void 0;
this.circuitBreaker = false;
showBanner(control.name);
this._PendingUpdates = [];
this.state = {};
this.outputOnlyProperties = {};
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);
const inputProperties = Object.getOwnPropertyNames(this.context._parameters);
if (outputs) {
Object.getOwnPropertyNames(outputs).forEach(p => {
if (inputProperties.includes(p)) {
return;
}
this.outputOnlyProperties[p] = outputs[p];
});
}
mockGetEntityMetadata(this);
this.UpdateValues = stub();
this.notifyOutputChanged = stub(); // Mocked in ReactResizeObserver
this.onOutputChanged = stub();
this.resizeObserver = new ResizeObserver(() => undefined); // Defined in ReactResizeObserver
this.RefreshParameters = stub();
mockRefreshParameters(this);
this.RefreshDatasets = stub();
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);
}
ExecuteUpdateView() {
this.RefreshParameters();
this.circuitBreaker = !this.circuitBreaker;
return /*#__PURE__*/createElement(ReactResizeObserver, {
componentFrameworkMockGeneratorReact: this,
circuitBreaker: this.circuitBreaker
});
}
}