@shko.online/componentframework-mock
Version:
Mocking library to help with testing PowerApps Component Framework Components
94 lines (93 loc) • 2.79 kB
JavaScript
/*
Copyright (c) 2022 Betim Beja and Shko Online LLC
Licensed under the MIT license.
*/
import { stub } from 'sinon';
export class DeviceMock {
constructor() {
this.captureAudio = void 0;
this.captureImage = void 0;
this.captureVideo = void 0;
this.getBarcodeValue = void 0;
this.getCurrentPosition = void 0;
this.pickFile = void 0;
this.captureAudio = stub();
this.captureAudio.callsFake(() => Promise.resolve({
fileContent: 'demo',
fileName: 'fakeFile.wav',
fileSize: 200,
mimeType: 'audio/wav'
}));
this.captureImage = stub();
this.captureImage.callsFake(options => {
var _options;
options = (_options = options) !== null && _options !== void 0 ? _options : {
allowEdit: false,
width: 1024,
height: 768,
preferFrontCamera: false,
quality: 100
};
return Promise.resolve({
fileContent: 'demo',
fileName: 'fakeFile.png',
fileSize: options.width * options.height * 4,
// Rough estimation of file size based on dimensions (RGBA)
mimeType: 'image/png'
});
});
this.captureVideo = stub();
this.captureVideo.callsFake(() => Promise.resolve({
fileContent: 'demo',
fileName: 'fakeFile.mp4',
fileSize: 2000,
mimeType: 'video/mp4'
}));
this.getBarcodeValue = stub();
this.getBarcodeValue.callsFake(() => Promise.resolve('SHKO-ONLINE'));
this.getCurrentPosition = stub();
this.getCurrentPosition.callsFake(() => Promise.resolve({
coords: {
accuracy: 141,
latitude: 41.3415145,
longitude: 19.7769355,
altitude: 0,
altitudeAccuracy: 0,
heading: 0,
speed: 0
},
timestamp: new Date()
}));
this.pickFile = stub();
this.pickFile.callsFake(options => {
return new Promise((resolve, reject) => {
const configuredOptions = options || {
accept: 'image'
};
if (configuredOptions.accept === 'image') {
return resolve([{
fileContent: 'demo',
fileName: 'fakeFile.png',
fileSize: 200,
mimeType: 'image/png'
}]);
} else if (configuredOptions.accept === 'audio') {
return resolve([{
fileContent: 'demo',
fileName: 'fakeFile.wav',
fileSize: 200,
mimeType: 'audio/wav'
}]);
} else if (configuredOptions.accept === 'video') {
return resolve([{
fileContent: 'demo',
fileName: 'fakeFile.mp4',
fileSize: 2000,
mimeType: 'video/mp4'
}]);
}
return reject(new Error('Unsupported file type'));
});
});
}
}