insomnia/packages/insomnia-app/app/__mocks__/electron.ts

93 lines
1.6 KiB
TypeScript
Raw Normal View History

import { EventEmitter } from 'events';
2021-07-22 23:04:56 +00:00
import mkdirp from 'mkdirp';
import os from 'os';
import path from 'path';
const RANDOM_STRING = Math.random().toString().replace('.', '');
const remote = {
app: {
2018-06-25 17:42:50 +00:00
getPath(name) {
const dir = path.join(os.tmpdir(), `insomnia-tests-${RANDOM_STRING}`, name);
mkdirp.sync(dir);
return dir;
},
2018-06-25 17:42:50 +00:00
getLocale() {
return 'en-US';
},
exit: jest.fn(),
},
net: {
request() {
const req = new EventEmitter();
// @ts-expect-error -- TSCONVERSION appears to be genuine
2018-06-25 17:42:50 +00:00
req.end = function() {};
return req;
},
},
BrowserWindow: {
2018-06-25 17:42:50 +00:00
getAllWindows() {
return [];
},
2018-06-25 17:42:50 +00:00
getFocusedWindow() {
return {
2018-06-25 17:42:50 +00:00
getContentBounds() {
return {
width: 1900,
height: 1060,
};
},
};
},
},
screen: {
2018-06-25 17:42:50 +00:00
getPrimaryDisplay() {
return {
workAreaSize: {
width: 1920,
height: 1080,
},
};
},
},
};
const dialog = {
showErrorBox: jest.fn(),
};
const electron = {
...remote,
remote,
dialog,
ipcMain: {
on: jest.fn(),
once() {},
2016-11-23 23:42:10 +00:00
},
ipcRenderer: {
2020-11-11 22:44:03 +00:00
on: jest.fn(),
removeAllListeners: jest.fn(),
2018-06-25 17:42:50 +00:00
once() {},
send: jest.fn(),
},
shell: {
openExternal: jest.fn(),
},
clipboard: {
writeText: jest.fn(),
readText: jest.fn(),
clear: jest.fn(),
},
};
// WARNING: changing this to `export default` will break the mock and be incredibly hard to debug. Ask me how I know.
module.exports = electron;