SymphonyElectron/spec/__mocks__/electron.ts

118 lines
3.0 KiB
TypeScript
Raw Normal View History

2019-01-22 09:12:16 -06:00
import { EventEmitter } from 'events';
import * as path from 'path';
2019-03-11 09:25:34 -05:00
import { isWindowsOS } from '../../src/common/env';
2019-01-22 09:12:16 -06:00
const ipcEmitter = new EventEmitter();
const mockIdleTime: number = 15;
2019-01-22 09:12:16 -06:00
const appName: string = 'Symphony';
const executableName: string = '/Symphony.exe';
const isReady: boolean = true;
2019-02-11 21:54:46 -06:00
const version: string = '4.0.0';
2019-01-22 09:12:16 -06:00
interface IApp {
getAppPath(): string;
getPath(type: string): string;
getName(): string;
isReady(): boolean;
2019-02-11 21:54:46 -06:00
getVersion(): string;
2019-01-22 09:12:16 -06:00
}
interface IIpcMain {
on(event: any, cb: any): void;
send(event: any, cb: any): void;
}
interface IIpcRenderer {
sendSync(event: any, cb: any): any;
on(eventName: any, cb: any): void;
send(event: any, cb: any): void;
removeListener(eventName: any, cb: any): void;
}
interface IPowerMonitor {
querySystemIdleTime(): void;
}
2019-01-22 09:12:16 -06:00
const pathToConfigDir = (): string => {
2019-03-11 09:25:34 -05:00
if (isWindowsOS) {
return path.join(__dirname, '/../..') as string;
} else {
return path.join(__dirname, '/..') as string;
}
2019-01-22 09:12:16 -06:00
};
// electron app mock...
const app: IApp = {
getAppPath: pathToConfigDir,
getPath: (type) => {
if (type === 'exe') {
return path.join(pathToConfigDir(), executableName);
}
2019-02-11 21:54:46 -06:00
if (type === 'userData') {
return path.join(pathToConfigDir(), '/../config');
}
2019-01-22 09:12:16 -06:00
return pathToConfigDir();
},
getName: () => appName,
isReady: () => isReady,
2019-02-11 21:54:46 -06:00
getVersion: () => version,
2019-01-22 09:12:16 -06:00
};
// simple ipc mocks for render and main process ipc using
// nodes' EventEmitter
const ipcMain: IIpcMain = {
on: (event, cb) => {
ipcEmitter.on(event, cb);
},
send: (event, args) => {
const senderEvent = {
sender: {
send: (eventSend, arg) => {
ipcEmitter.emit(eventSend, arg);
},
},
};
ipcEmitter.emit(event, senderEvent, args);
},
};
const powerMonitor: IPowerMonitor = {
querySystemIdleTime: jest.fn().mockImplementation((cb) => cb(mockIdleTime)),
};
2019-01-22 09:12:16 -06:00
const ipcRenderer: IIpcRenderer = {
sendSync: (event, args) => {
const listeners = ipcEmitter.listeners(event);
if (listeners.length > 0) {
const listener = listeners[0];
const eventArg = {};
listener(eventArg, args);
return eventArg;
}
return null;
},
send: (event, args) => {
const senderEvent = {
sender: {
send: (eventSend, arg) => {
ipcEmitter.emit(eventSend, arg);
},
},
};
ipcEmitter.emit(event, senderEvent, args);
},
on: (eventName, cb) => {
ipcEmitter.on(eventName, cb);
},
removeListener: (eventName, cb) => {
ipcEmitter.removeListener(eventName, cb);
},
};
export = {
app,
ipcMain,
ipcRenderer,
powerMonitor,
2019-01-22 09:12:16 -06:00
require: jest.fn(),
match: jest.fn(),
remote: jest.fn(),
dialog: jest.fn(),
};