-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
36213a6
commit a0a3d77
Showing
5 changed files
with
218 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { EventEmitter } from 'events'; | ||
import * as path from 'path'; | ||
const ipcEmitter = new EventEmitter(); | ||
|
||
const appName: string = 'Symphony'; | ||
const executableName = '/Symphony.exe'; | ||
interface IApp { | ||
getAppPath(): string; | ||
getPath(type: string): string; | ||
getName(): string; | ||
} | ||
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; | ||
} | ||
|
||
// use config provided by test framework | ||
const pathToConfigDir = (): string => { | ||
return path.join(__dirname, '/..') as string; | ||
}; | ||
|
||
// electron app mock... | ||
const app: IApp = { | ||
getAppPath: pathToConfigDir, | ||
getPath: (type) => { | ||
if (type === 'exe') { | ||
return path.join(pathToConfigDir(), executableName); | ||
} | ||
return pathToConfigDir(); | ||
}, | ||
getName: () => appName, | ||
}; | ||
|
||
// 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 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, | ||
require: jest.fn(), | ||
match: jest.fn(), | ||
remote: jest.fn(), | ||
dialog: jest.fn(), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { ILogMsg } from '../src/common/logger'; | ||
|
||
describe('logger', () => { | ||
let instance; | ||
beforeEach(() => { | ||
// I did it for reset module imported between tests | ||
const { logger } = require('../src/common/logger'); | ||
instance = logger; | ||
jest.resetModules(); | ||
}); | ||
|
||
it('when no logger registered then queue items', () => { | ||
instance.debug('test'); | ||
instance.debug('test2'); | ||
const queue: ILogMsg[] = instance.getLogQueue(); | ||
expect(queue.length).toBe(2); | ||
}); | ||
|
||
it('should call send when logger get registered', () => { | ||
instance.debug('test'); | ||
instance.debug('test2'); | ||
|
||
const mock = jest.fn<Electron.WebContents>(() => ({ | ||
send: jest.fn(), | ||
})); | ||
const mockWin = new mock(); | ||
instance.setLoggerWindow(mockWin); | ||
expect(mockWin.send).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should call `logger.error` correctly', () => { | ||
const spy = jest.spyOn(instance, 'log'); | ||
|
||
instance.error('test error', { error: 'test error' }); | ||
|
||
expect(spy).toBeCalledWith('error', 'test error', [{ error: 'test error' }]); | ||
}); | ||
|
||
it('should call `logger.warn` correctly', () => { | ||
const spy = jest.spyOn(instance, 'log'); | ||
|
||
instance.warn('test warn', { warn: 'test warn' }); | ||
|
||
expect(spy).toBeCalledWith('warn', 'test warn', [{ warn: 'test warn' }]); | ||
}); | ||
|
||
it('should call `logger.debug` correctly', () => { | ||
const spy = jest.spyOn(instance, 'log'); | ||
|
||
instance.debug('test debug', { debug: 'test debug' }); | ||
|
||
expect(spy).toBeCalledWith('debug', 'test debug', [{ debug: 'test debug' }]); | ||
}); | ||
|
||
it('should call `logger.info` correctly', () => { | ||
const spy = jest.spyOn(instance, 'log'); | ||
|
||
instance.info('test info', { info: 'test info' }); | ||
|
||
expect(spy).toBeCalledWith('info', 'test info', [{ info: 'test info' }]); | ||
}); | ||
|
||
it('should call `logger.verbose` correctly', () => { | ||
const spy = jest.spyOn(instance, 'log'); | ||
|
||
instance.verbose('test verbose', { verbose: 'test verbose' }); | ||
|
||
expect(spy).toBeCalledWith('verbose', 'test verbose', [{ verbose: 'test verbose' }]); | ||
}); | ||
|
||
it('should call `logger.silly` correctly', () => { | ||
const spy = jest.spyOn(instance, 'log'); | ||
|
||
instance.silly('test silly', { silly: 'test silly' }); | ||
|
||
expect(spy).toBeCalledWith('silly', 'test silly', [{ silly: 'test silly' }]); | ||
}); | ||
|
||
it('should call `logger.sendToCloud` when `logger.debug` is called', () => { | ||
const spyLog = jest.spyOn(instance, 'log'); | ||
const spySendToCloud = jest.spyOn(instance, 'sendToCloud'); | ||
instance.debug('test'); | ||
|
||
expect(spyLog).toBeCalled(); | ||
expect(spySendToCloud).toBeCalled(); | ||
|
||
}); | ||
|
||
it('should cap at 100 queued log messages', () => { | ||
for (let i = 0; i < 110; i++) { | ||
instance.debug('test' + i); | ||
} | ||
const queue = instance.getLogQueue(); | ||
expect(queue.length).toBe(100); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters