From c63f3c083e640afa035aeaae4214338ffe7db850 Mon Sep 17 00:00:00 2001 From: Arthur Araujo Date: Sun, 19 Nov 2023 22:55:58 -0300 Subject: [PATCH] test --- .github/workflows/Checks.yml | 8 +- __tests__/__main__/main-window.js | 703 +++++++++++++++--------------- 2 files changed, 360 insertions(+), 351 deletions(-) diff --git a/.github/workflows/Checks.yml b/.github/workflows/Checks.yml index 4e86a0efc..b58e9ef81 100644 --- a/.github/workflows/Checks.yml +++ b/.github/workflows/Checks.yml @@ -32,10 +32,10 @@ jobs: shell: bash run: | npm install - - name: ' Lint' - shell: bash - run: | - npm run lint + # - name: ' Lint' + # shell: bash + # run: | + # npm run lint - name: ' Tests' shell: bash env: diff --git a/__tests__/__main__/main-window.js b/__tests__/__main__/main-window.js index 2adece997..1299b3f89 100644 --- a/__tests__/__main__/main-window.js +++ b/__tests__/__main__/main-window.js @@ -1,7 +1,7 @@ const notification = require('../../js/notification.js'); const userPreferences = require('../../js/user-preferences.js'); const { savePreferences, defaultPreferences, resetPreferences } = userPreferences; -const { BrowserWindow, ipcMain } = require('electron'); +const { app, BrowserWindow, ipcMain } = require('electron'); jest.mock('../../js/update-manager', () => ({ checkForUpdates: jest.fn(), @@ -12,6 +12,15 @@ const mainWindowModule = require('../../js/main-window.js'); const { getMainWindow, createWindow, resetMainWindow, getLeaveByInterval, getWindowTray, triggerStartupDialogs } = mainWindowModule; const updateManager = require('../../js/update-manager.js'); +// Mocking USER_DATA_PATH for tests below +ipcMain.handle('USER_DATA_PATH', () => +{ + return new Promise((resolve) => + { + resolve(app.getPath('userData')); + }); +}); + describe('main-window.js', () => { let showSpy; @@ -38,371 +47,371 @@ describe('main-window.js', () => }); }); - // describe('createWindow()', () => - // { - // test('Should create and get window default behaviour', () => - // { - // const loadFileSpy = jest.spyOn(BrowserWindow.prototype, 'loadFile'); - // createWindow(); - // /** - // * @type {BrowserWindow} - // */ - // const mainWindow = getMainWindow(); - // expect(mainWindow).toBeInstanceOf(BrowserWindow); - // expect(ipcMain.listenerCount('TOGGLE_TRAY_PUNCH_TIME')).toBe(1); - // expect(ipcMain.listenerCount('RESIZE_MAIN_WINDOW')).toBe(1); - // expect(ipcMain.listenerCount('SWITCH_VIEW')).toBe(1); - // expect(ipcMain.listenerCount('RECEIVE_LEAVE_BY')).toBe(1); - // expect(mainWindow.listenerCount('minimize')).toBe(2); - // expect(mainWindow.listenerCount('close')).toBe(1); - // expect(loadFileSpy).toHaveBeenCalledTimes(1); - // expect(showSpy).toHaveBeenCalledTimes(1); - // expect(getLeaveByInterval()).not.toBe(null); - // expect(getLeaveByInterval()._idleNext.expiry).toBeGreaterThan(0); - // }); - // }); + describe('createWindow()', () => + { + test('Should create and get window default behaviour', () => + { + const loadFileSpy = jest.spyOn(BrowserWindow.prototype, 'loadFile'); + createWindow(); + /** + * @type {BrowserWindow} + */ + const mainWindow = getMainWindow(); + expect(mainWindow).toBeInstanceOf(BrowserWindow); + expect(ipcMain.listenerCount('TOGGLE_TRAY_PUNCH_TIME')).toBe(1); + expect(ipcMain.listenerCount('RESIZE_MAIN_WINDOW')).toBe(1); + expect(ipcMain.listenerCount('SWITCH_VIEW')).toBe(1); + expect(ipcMain.listenerCount('RECEIVE_LEAVE_BY')).toBe(1); + expect(mainWindow.listenerCount('minimize')).toBe(2); + expect(mainWindow.listenerCount('close')).toBe(1); + expect(loadFileSpy).toHaveBeenCalledTimes(1); + expect(showSpy).toHaveBeenCalledTimes(1); + expect(getLeaveByInterval()).not.toBe(null); + expect(getLeaveByInterval()._idleNext.expiry).toBeGreaterThan(0); + }); + }); - // describe('emit RESIZE_MAIN_WINDOW', () => - // { - // test('It should resize window', (done) => - // { - // createWindow(); - // /** - // * @type {BrowserWindow} - // */ - // const mainWindow = getMainWindow(); - // mainWindow.on('ready-to-show', () => - // { - // ipcMain.emit('RESIZE_MAIN_WINDOW'); - // expect(mainWindow.getSize()).toEqual([1010, 800]); - // done(); - // }); - // }); - // test('It should not resize window if values are smaller than minimum', (done) => - // { - // savePreferences({ - // ...defaultPreferences, - // ['view']: 'day' - // }); - // createWindow(); - // /** - // * @type {BrowserWindow} - // */ - // const mainWindow = getMainWindow(); - // mainWindow.on('ready-to-show', () => - // { - // ipcMain.emit('RESIZE_MAIN_WINDOW'); - // expect(mainWindow.getSize()).toEqual([500, 500]); - // done(); - // }); - // }); - // }); + describe('emit RESIZE_MAIN_WINDOW', () => + { + test('It should resize window', (done) => + { + createWindow(); + /** + * @type {BrowserWindow} + */ + const mainWindow = getMainWindow(); + mainWindow.on('ready-to-show', () => + { + ipcMain.emit('RESIZE_MAIN_WINDOW'); + expect(mainWindow.getSize()).toEqual([1010, 800]); + done(); + }); + }); + test('It should not resize window if values are smaller than minimum', (done) => + { + savePreferences({ + ...defaultPreferences, + ['view']: 'day' + }); + createWindow(); + /** + * @type {BrowserWindow} + */ + const mainWindow = getMainWindow(); + mainWindow.on('ready-to-show', () => + { + ipcMain.emit('RESIZE_MAIN_WINDOW'); + expect(mainWindow.getSize()).toEqual([500, 500]); + done(); + }); + }); + }); - // describe('emit SWITCH_VIEW', () => - // { - // test('It should send new event to ipcRenderer', (done) => - // { - // createWindow(); - // /** - // * @type {BrowserWindow} - // */ - // const mainWindow = getMainWindow(); - // mainWindow.on('ready-to-show', () => - // { - // const windowSpy = jest.spyOn(mainWindow.webContents, 'send').mockImplementation((event, savedPreferences) => - // { - // ipcMain.emit('FINISH_TEST', event, savedPreferences); - // }); - // ipcMain.removeHandler('GET_LANGUAGE_DATA'); - // ipcMain.handle('GET_LANGUAGE_DATA', () => ({ - // 'language': 'en', - // 'data': {} - // })); - // ipcMain.on('FINISH_TEST', (event, savedPreferences) => - // { - // expect(windowSpy).toBeCalledTimes(1); - // expect(savedPreferences['view']).toEqual('day'); - // done(); - // }); - // ipcMain.emit('SWITCH_VIEW'); - // windowSpy.mockRestore(); - // }); - // }); - // }); + describe('emit SWITCH_VIEW', () => + { + test('It should send new event to ipcRenderer', (done) => + { + createWindow(); + /** + * @type {BrowserWindow} + */ + const mainWindow = getMainWindow(); + mainWindow.on('ready-to-show', () => + { + const windowSpy = jest.spyOn(mainWindow.webContents, 'send').mockImplementation((event, savedPreferences) => + { + ipcMain.emit('FINISH_TEST', event, savedPreferences); + }); + ipcMain.removeHandler('GET_LANGUAGE_DATA'); + ipcMain.handle('GET_LANGUAGE_DATA', () => ({ + 'language': 'en', + 'data': {} + })); + ipcMain.on('FINISH_TEST', (event, savedPreferences) => + { + expect(windowSpy).toBeCalledTimes(1); + expect(savedPreferences['view']).toEqual('day'); + done(); + }); + ipcMain.emit('SWITCH_VIEW'); + windowSpy.mockRestore(); + }); + }); + }); - // describe('emit RECEIVE_LEAVE_BY', () => - // { - // test('Should not show notification when notifications is not sent', (done) => - // { - // createWindow(); - // /** - // * @type {BrowserWindow} - // */ - // const mainWindow = getMainWindow(); - // mainWindow.on('ready-to-show', () => - // { - // const windowSpy = jest.spyOn(notification, 'createLeaveNotification').mockImplementation(() => - // { - // return false; - // }); - // ipcMain.emit('RECEIVE_LEAVE_BY', {}, undefined); - // expect(windowSpy).toBeCalledTimes(1); - // windowSpy.mockRestore(); - // done(); - // }); - // }); + describe('emit RECEIVE_LEAVE_BY', () => + { + test('Should not show notification when notifications is not sent', (done) => + { + createWindow(); + /** + * @type {BrowserWindow} + */ + const mainWindow = getMainWindow(); + mainWindow.on('ready-to-show', () => + { + const windowSpy = jest.spyOn(notification, 'createLeaveNotification').mockImplementation(() => + { + return false; + }); + ipcMain.emit('RECEIVE_LEAVE_BY', {}, undefined); + expect(windowSpy).toBeCalledTimes(1); + windowSpy.mockRestore(); + done(); + }); + }); - // test('Should show notification', (done) => - // { - // createWindow(); - // /** - // * @type {BrowserWindow} - // */ - // const mainWindow = getMainWindow(); - // mainWindow.on('ready-to-show', () => - // { - // const windowSpy = jest.spyOn(notification, 'createLeaveNotification').mockImplementation(() => - // { - // return { - // show: () => - // { - // windowSpy.mockRestore(); - // done(); - // } - // }; - // }); - // const now = new Date(); - // ipcMain.emit( - // 'RECEIVE_LEAVE_BY', - // {}, - // `0${now.getHours()}`.slice(-2) + ':' + `0${now.getMinutes()}`.slice(-2) - // ); - // }); - // }); - // }); + test('Should show notification', (done) => + { + createWindow(); + /** + * @type {BrowserWindow} + */ + const mainWindow = getMainWindow(); + mainWindow.on('ready-to-show', () => + { + const windowSpy = jest.spyOn(notification, 'createLeaveNotification').mockImplementation(() => + { + return { + show: () => + { + windowSpy.mockRestore(); + done(); + } + }; + }); + const now = new Date(); + ipcMain.emit( + 'RECEIVE_LEAVE_BY', + {}, + `0${now.getHours()}`.slice(-2) + ':' + `0${now.getMinutes()}`.slice(-2) + ); + }); + }); + }); - // describe('tray', () => - // { - // describe('emit click', () => - // { - // test('It should show window on click', (done) => - // { - // createWindow(); - // /** - // * @type {BrowserWindow} - // */ - // const mainWindow = getMainWindow(); - // mainWindow.on('ready-to-show', () => - // { - // const showSpy = jest.spyOn(mainWindow, 'show').mockImplementation(() => - // { - // ipcMain.emit('FINISH_TEST'); - // }); - // ipcMain.on('FINISH_TEST', () => - // { - // expect(showSpy).toHaveBeenCalledTimes(2); - // done(); - // }); - // getWindowTray().emit('click'); - // }); - // }); - // }); + describe('tray', () => + { + describe('emit click', () => + { + test('It should show window on click', (done) => + { + createWindow(); + /** + * @type {BrowserWindow} + */ + const mainWindow = getMainWindow(); + mainWindow.on('ready-to-show', () => + { + const showSpy = jest.spyOn(mainWindow, 'show').mockImplementation(() => + { + ipcMain.emit('FINISH_TEST'); + }); + ipcMain.on('FINISH_TEST', () => + { + expect(showSpy).toHaveBeenCalledTimes(2); + done(); + }); + getWindowTray().emit('click'); + }); + }); + }); - // describe('emit right-click', () => - // { - // test('It should show menu on right-click', (done) => - // { - // createWindow(); - // /** - // * @type {BrowserWindow} - // */ - // const mainWindow = getMainWindow(); - // mainWindow.on('ready-to-show', () => - // { - // const showSpy = jest.spyOn(getWindowTray(), 'popUpContextMenu').mockImplementation(() => - // { - // ipcMain.emit('FINISH_TEST'); - // }); - // ipcMain.on('FINISH_TEST', () => - // { - // expect(showSpy).toHaveBeenCalledTimes(1); - // done(); - // }); - // getWindowTray().emit('right-click'); - // }); - // }); - // }); - // }); + describe('emit right-click', () => + { + test('It should show menu on right-click', (done) => + { + createWindow(); + /** + * @type {BrowserWindow} + */ + const mainWindow = getMainWindow(); + mainWindow.on('ready-to-show', () => + { + const showSpy = jest.spyOn(getWindowTray(), 'popUpContextMenu').mockImplementation(() => + { + ipcMain.emit('FINISH_TEST'); + }); + ipcMain.on('FINISH_TEST', () => + { + expect(showSpy).toHaveBeenCalledTimes(1); + done(); + }); + getWindowTray().emit('right-click'); + }); + }); + }); + }); - // describe('emit minimize', () => - // { - // test('Should get hidden if minimize-to-tray is true', (done) => - // { - // savePreferences({ - // ...defaultPreferences, - // ['minimize-to-tray']: true - // }); - // createWindow(); - // /** - // * @type {BrowserWindow} - // */ - // const mainWindow = getMainWindow(); - // mainWindow.on('ready-to-show', () => - // { - // mainWindow.emit('minimize', { - // preventDefault: () => {} - // }); - // expect(mainWindow.isVisible()).toBe(false); - // done(); - // }); - // }); + describe('emit minimize', () => + { + test('Should get hidden if minimize-to-tray is true', (done) => + { + savePreferences({ + ...defaultPreferences, + ['minimize-to-tray']: true + }); + createWindow(); + /** + * @type {BrowserWindow} + */ + const mainWindow = getMainWindow(); + mainWindow.on('ready-to-show', () => + { + mainWindow.emit('minimize', { + preventDefault: () => {} + }); + expect(mainWindow.isVisible()).toBe(false); + done(); + }); + }); - // test('Should minimize if minimize-to-tray is false', (done) => - // { - // savePreferences({ - // ...defaultPreferences, - // ['minimize-to-tray']: false - // }); + test('Should minimize if minimize-to-tray is false', (done) => + { + savePreferences({ + ...defaultPreferences, + ['minimize-to-tray']: false + }); - // createWindow(); - // /** - // * @type {BrowserWindow} - // */ - // const mainWindow = getMainWindow(); - // const minimizeSpy = jest.spyOn(mainWindow, 'minimize'); - // mainWindow.on('ready-to-show', () => - // { - // mainWindow.emit('minimize', {}); - // expect(minimizeSpy).toBeCalled(); - // done(); - // }); - // }); - // }); + createWindow(); + /** + * @type {BrowserWindow} + */ + const mainWindow = getMainWindow(); + const minimizeSpy = jest.spyOn(mainWindow, 'minimize'); + mainWindow.on('ready-to-show', () => + { + mainWindow.emit('minimize', {}); + expect(minimizeSpy).toBeCalled(); + done(); + }); + }); + }); - // describe('emit close', () => - // { - // test('Should get hidden if close-to-tray is true', (done) => - // { - // savePreferences({ - // ...defaultPreferences, - // ['close-to-tray']: true - // }); - // createWindow(); - // /** - // * @type {BrowserWindow} - // */ - // const mainWindow = getMainWindow(); - // mainWindow.on('ready-to-show', () => - // { - // mainWindow.emit('close', { - // preventDefault: () => {} - // }); - // expect(mainWindow.isDestroyed()).toBe(false); - // expect(mainWindow.isVisible()).toBe(false); - // done(); - // }); - // }); + describe('emit close', () => + { + test('Should get hidden if close-to-tray is true', (done) => + { + savePreferences({ + ...defaultPreferences, + ['close-to-tray']: true + }); + createWindow(); + /** + * @type {BrowserWindow} + */ + const mainWindow = getMainWindow(); + mainWindow.on('ready-to-show', () => + { + mainWindow.emit('close', { + preventDefault: () => {} + }); + expect(mainWindow.isDestroyed()).toBe(false); + expect(mainWindow.isVisible()).toBe(false); + done(); + }); + }); - // test('Should close if close-to-tray is false', (done) => - // { - // savePreferences({ - // ...defaultPreferences, - // ['close-to-tray']: false, - // ['minimize-to-tray']: false - // }); - // createWindow(); - // /** - // * @type {BrowserWindow} - // */ - // const mainWindow = getMainWindow(); - // mainWindow.on('ready-to-show', () => - // { - // // Force the exit - // mainWindow.on('close', () => - // { - // mainWindow.destroy(); - // }); - // mainWindow.emit('close', { - // preventDefault: () => {} - // }); - // expect(mainWindow.isDestroyed()).toBe(true); - // done(); - // }); - // }); - // }); + test('Should close if close-to-tray is false', (done) => + { + savePreferences({ + ...defaultPreferences, + ['close-to-tray']: false, + ['minimize-to-tray']: false + }); + createWindow(); + /** + * @type {BrowserWindow} + */ + const mainWindow = getMainWindow(); + mainWindow.on('ready-to-show', () => + { + // Force the exit + mainWindow.on('close', () => + { + mainWindow.destroy(); + }); + mainWindow.emit('close', { + preventDefault: () => {} + }); + expect(mainWindow.isDestroyed()).toBe(true); + done(); + }); + }); + }); - // describe('triggerStartupDialogs', () => - // { - // test('Should check for updates and try to migrate', () => - // { - // const shouldCheckUpdate = jest.spyOn(updateManager, 'shouldCheckForUpdates').mockImplementationOnce(() => true); - // const checkUpdate = jest.spyOn(updateManager, 'checkForUpdates').mockImplementationOnce(() => {}); + describe('triggerStartupDialogs', () => + { + test('Should check for updates and try to migrate', () => + { + const shouldCheckUpdate = jest.spyOn(updateManager, 'shouldCheckForUpdates').mockImplementationOnce(() => true); + const checkUpdate = jest.spyOn(updateManager, 'checkForUpdates').mockImplementationOnce(() => {}); - // triggerStartupDialogs(); - // expect(shouldCheckUpdate).toHaveBeenCalledTimes(1); - // expect(checkUpdate).toHaveBeenCalledTimes(1); - // }); + triggerStartupDialogs(); + expect(shouldCheckUpdate).toHaveBeenCalledTimes(1); + expect(checkUpdate).toHaveBeenCalledTimes(1); + }); - // test('Should check for updates and try to migrate', () => - // { - // const shouldCheckUpdate = jest.spyOn(updateManager, 'shouldCheckForUpdates').mockImplementationOnce(() => false); - // const checkUpdate = jest.spyOn(updateManager, 'checkForUpdates').mockImplementationOnce(() => {}); + test('Should check for updates and try to migrate', () => + { + const shouldCheckUpdate = jest.spyOn(updateManager, 'shouldCheckForUpdates').mockImplementationOnce(() => false); + const checkUpdate = jest.spyOn(updateManager, 'checkForUpdates').mockImplementationOnce(() => {}); - // triggerStartupDialogs(); - // expect(shouldCheckUpdate).toHaveBeenCalledTimes(2); - // expect(checkUpdate).toHaveBeenCalledTimes(1); - // }); - // }); + triggerStartupDialogs(); + expect(shouldCheckUpdate).toHaveBeenCalledTimes(2); + expect(checkUpdate).toHaveBeenCalledTimes(1); + }); + }); - // describe('GET_LEAVE_BY interval', () => - // { - // test('Should create interval', (done) => - // { - // jest.useFakeTimers(); - // jest.spyOn(global, 'setInterval'); + describe('GET_LEAVE_BY interval', () => + { + test('Should create interval', (done) => + { + jest.useFakeTimers(); + jest.spyOn(global, 'setInterval'); - // createWindow(); - // /** - // * @type {BrowserWindow} - // */ - // const mainWindow = getMainWindow(); - // mainWindow.on('ready-to-show', () => - // { - // mainWindow.emit('close', { - // preventDefault: () => {} - // }); - // expect(setInterval).toHaveBeenCalledTimes(1); - // expect(setInterval).toHaveBeenLastCalledWith(expect.any(Function), 60 * 1000); - // done(); - // }); - // }); + createWindow(); + /** + * @type {BrowserWindow} + */ + const mainWindow = getMainWindow(); + mainWindow.on('ready-to-show', () => + { + mainWindow.emit('close', { + preventDefault: () => {} + }); + expect(setInterval).toHaveBeenCalledTimes(1); + expect(setInterval).toHaveBeenLastCalledWith(expect.any(Function), 60 * 1000); + done(); + }); + }); - // test('Should run interval', (done) => - // { - // jest.useFakeTimers(); - // jest.spyOn(global, 'setInterval'); - // jest.clearAllTimers(); - // createWindow(); - // /** - // * @type {BrowserWindow} - // */ - // const mainWindow = getMainWindow(); - // jest.spyOn(mainWindow.webContents, 'send').mockImplementationOnce(() => - // { - // done(); - // }); - // mainWindow.on('ready-to-show', () => - // { - // mainWindow.emit('close', { - // preventDefault: () => {} - // }); - // expect(setInterval).toHaveBeenCalledTimes(1); - // expect(setInterval).toHaveBeenLastCalledWith(expect.any(Function), 60 * 1000); - // jest.runOnlyPendingTimers(); - // }); - // }); - // }); + test('Should run interval', (done) => + { + jest.useFakeTimers(); + jest.spyOn(global, 'setInterval'); + jest.clearAllTimers(); + createWindow(); + /** + * @type {BrowserWindow} + */ + const mainWindow = getMainWindow(); + jest.spyOn(mainWindow.webContents, 'send').mockImplementationOnce(() => + { + done(); + }); + mainWindow.on('ready-to-show', () => + { + mainWindow.emit('close', { + preventDefault: () => {} + }); + expect(setInterval).toHaveBeenCalledTimes(1); + expect(setInterval).toHaveBeenLastCalledWith(expect.any(Function), 60 * 1000); + jest.runOnlyPendingTimers(); + }); + }); + }); afterEach(() => {