SymphonyElectron/tests/__mocks__/electron.js

81 lines
2.0 KiB
JavaScript
Raw Normal View History

2017-02-26 19:58:48 -06:00
const path = require('path');
const EventEmitter = require('events');
let ipcEmitter = new EventEmitter();
2017-02-26 19:58:48 -06:00
2017-02-26 20:47:23 -06:00
// use config provided by test framework
2017-02-26 19:58:48 -06:00
function pathToConfigDir() {
2017-02-26 20:47:23 -06:00
return path.join(__dirname, '/../fixtures');
2017-02-26 19:58:48 -06:00
}
// electron app mock...
const app = {
getAppPath: pathToConfigDir,
getPath: function(type) {
if (type === 'exe') {
return path.join(pathToConfigDir(), '/Symphony.exe');
}
return pathToConfigDir();
},
on: function() {
// no-op
}
};
// simple ipc mocks for render and main process ipc using
// nodes' EventEmitter
const ipcMain = {
on: function(event, cb) {
ipcEmitter.on(event, cb);
},
send: function (event, args) {
var senderEvent = {
sender: {
send: function (event, arg) {
ipcEmitter.emit(event, arg);
}
}
};
ipcEmitter.emit(event, senderEvent, args);
},
};
const ipcRenderer = {
sendSync: function(event, args) {
let listeners = ipcEmitter.listeners(event);
if (listeners.length > 0) {
let listener = listeners[0];
var eventArg = {};
listener(eventArg, args);
return eventArg.returnValue;
}
return null;
},
send: function(event, args) {
var senderEvent = {
sender: {
send: function(event, arg) {
ipcEmitter.emit(event, arg);
}
}
};
ipcEmitter.emit(event, senderEvent, args);
},
on: function(eventName, cb) {
ipcEmitter.on(eventName, cb);
},
removeListener: function(eventName, cb) {
ipcEmitter.removeListener(eventName, cb);
2017-02-26 20:47:23 -06:00
}
};
2017-02-26 19:58:48 -06:00
module.exports = {
require: jest.genMockFunction(),
match: jest.genMockFunction(),
app: jest.genMockFunction(),
ipcMain: ipcMain,
ipcRenderer: ipcRenderer,
2017-02-26 19:58:48 -06:00
remote: jest.genMockFunction(),
dialog: jest.genMockFunction()
};