Skip to content

Commit

Permalink
Optimization about preference window
Browse files Browse the repository at this point in the history
- Delete action and store of preference
  - because It don't need to manage globally.
  - unnecessary action can cause confuse.
  • Loading branch information
HyunmoAhn committed Feb 1, 2019
1 parent 389de77 commit 1667dd8
Show file tree
Hide file tree
Showing 22 changed files with 131 additions and 306 deletions.
50 changes: 0 additions & 50 deletions app/actions/__tests__/preference.spec.js

This file was deleted.

3 changes: 0 additions & 3 deletions app/actions/constant/actionTypes.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
export const MODAL_OPEN = 'MODAL_OPEN';
export const MODAL_CLOSE = 'MODAL_CLOSE';
export const OPEN_BROWSER_WINDOW = 'OPEN_BROWSER_WINDOW';
export const PREFERENCE_ALLOCATE_ID = 'PREFERENCE_ALLOCATE_ID';
export const PREFERENCE_OPEN = 'PREFERENCE_OPEN';
export const PREFERENCE_CLOSE = 'PREFERENCE_CLOSE';
export const SET_HOT_KEY_SEARCH_WINDOW = 'SET_HOT_KEY_SEARCH_WINDOW';
export const SET_INITIAL_STORE = 'SET_INITIAL_STORE';
export const SET_LANGUAGE_ENGLISH = 'SET_LANGUAGE_ENGLISH';
Expand Down
28 changes: 0 additions & 28 deletions app/actions/preference.js

This file was deleted.

4 changes: 2 additions & 2 deletions app/main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { app } from 'electron';
import store from 'store/storeMain';
import saveData from 'main/utils/disk/saveData';
import { preferenceClose } from 'actions/preference';
import { widgetCloseWhole } from 'actions/widget';
import { setInitialStore, setWhenQuitApp } from 'actions/setting';
import { isOpenWidgetWhenStartSelector } from 'store/reducers/share/status/selectors';
Expand All @@ -13,6 +12,7 @@ import handlingSearchHotKey from 'main/utils/menu/handlingSearchHotKey';
import { hotKeySearchWindowSelector } from 'store/reducers/share/config/selectors';
import openAllWidgetStatusOpen from 'main/utils/window/openAllWidgetStatusOpen';
import openReduxDevTools from 'main/utils/window/openReduxDevTools';
import handleIPC from 'main/utils/handleIPC';
import { autoUpdater } from 'electron-updater';

const SAVE_SETTING_INTERVAL = 300000;
Expand All @@ -35,6 +35,7 @@ app.on('ready', () => {
autoLaunchConfig();
autoUpdater.checkForUpdatesAndNotify();

handleIPC();
// config data
setInterval(saveData, SAVE_SETTING_INTERVAL);
store.dispatch(setInitialStore());
Expand Down Expand Up @@ -67,7 +68,6 @@ app.on('before-quit', () => {
if (!isOpenWidgetWhenStart) {
dispatch(widgetCloseWhole());
}
dispatch(preferenceClose());
dispatch(setWhenQuitApp());
saveData();
});
14 changes: 0 additions & 14 deletions app/main/controllers/__tests__/preference.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { autoUpdater } from 'electron-updater';
import { CancellationToken } from 'electron-builder-http';
import * as TYPES from 'actions/constant/actionTypes';
import * as openPreference from 'main/utils/window/openPreference';
import * as autoLaunch from 'main/utils/window/autoLaunch';
import store from 'store/storeMain';
import createMenu from 'main/utils/menu/createMenu';
Expand All @@ -21,19 +20,6 @@ describe('test preference controller', () => {
jest.clearAllMocks();
});

it('should handle OPEN_PREFERENCE', () => {
const mockOpenPreference = jest.spyOn(openPreference, 'default');
mockOpenPreference.mockImplementation(jest.fn());
const mockAction = {
type: TYPES.PREFERENCE_OPEN,
};

preferenceController(mockAction);

expect(mockOpenPreference).toHaveBeenCalledTimes(1);
expect(mockOpenPreference).toHaveBeenCalledWith();
});

it('should handle TOGGLE_OPEN_APP_WHEN_LOGIN', () => {
const mockAutoLaunch = jest.spyOn(autoLaunch, 'default');
mockAutoLaunch.mockImplementation(jest.fn());
Expand Down
5 changes: 0 additions & 5 deletions app/main/controllers/preference.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { autoUpdater } from 'electron-updater';
import { CancellationToken } from 'electron-builder-http';
import store from 'store/storeMain';
import openPreference from 'main/utils/window/openPreference';
import * as TYPES from 'actions/constant/actionTypes';
import autoLaunch from 'main/utils/window/autoLaunch';
import createMenu from 'main/utils/menu/createMenu';
Expand All @@ -18,10 +17,6 @@ let cancellationToken;
const preferenceController = (action) => {
const { type } = action;
switch (type) { // eslint-disable-line default-case
case TYPES.PREFERENCE_OPEN: {
openPreference();
break;
}
case TYPES.TOGGLE_OPEN_APP_WHEN_LOGIN: {
autoLaunch();
break;
Expand Down
22 changes: 22 additions & 0 deletions app/main/utils/__tests__/handleIPC.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { ipcMain } from 'electron';
import * as pref from 'main/utils/window/preference';
import handleIPC from '../handleIPC';

describe('test handleIPC', () => {
handleIPC();

it('should match number of whole handler', () => {
expect(ipcMain.on).toHaveBeenCalledTimes(1);
});

it('should call preference open', () => {
expect(ipcMain.on)
.toHaveBeenCalledWith('preference.open', expect.any(Function));

const openPreference = jest.spyOn(pref, 'openPreference');
const cb = ipcMain.on.mock.calls.filter(i => i[0] === 'preference.open')[0][1];

cb();
expect(openPreference).toHaveBeenCalledTimes(1);
});
});
112 changes: 0 additions & 112 deletions app/main/utils/__tests__/window/openPreference.spec.js

This file was deleted.

46 changes: 46 additions & 0 deletions app/main/utils/__tests__/window/preference.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { BrowserWindow } from 'electron';
import { PREFERENCE_PATH } from 'config';
import { preference, openPreference } from 'main/utils/window/preference';


describe('test window/preference', () => {
describe('when preference is null', () => {
afterAll(() => {
jest.clearAllMocks();
});
const mockWindow = new BrowserWindow();
BrowserWindow.mockImplementationOnce(() => mockWindow);

openPreference();

it('match preference', () => {
expect(preference).toEqual(mockWindow);
});

it('match loadURL', () => {
expect(preference.loadURL).toHaveBeenCalledTimes(1);
expect(preference.loadURL).toHaveBeenCalledWith(PREFERENCE_PATH);
});

it('match on.close', () => {
expect(mockWindow.on).toHaveBeenCalledTimes(1);
expect(mockWindow.on).toHaveBeenCalledWith('close', expect.any(Function));

expect(preference).toEqual(mockWindow);
const cb = mockWindow.on.mock.calls[0][1];

cb();
expect(preference).toEqual(null);
});
});

it('when preference is not null', () => {
const mockWindow = new BrowserWindow();
BrowserWindow.mockImplementationOnce(() => mockWindow);

openPreference();
openPreference();

expect(preference.show).toHaveBeenCalledTimes(1);
});
});
10 changes: 10 additions & 0 deletions app/main/utils/handleIPC.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ipcMain } from 'electron';
import { openPreference } from 'main/utils/window/preference';

function handleIPC() {
ipcMain.on('preference.open', () => {
openPreference();
});
}

export default handleIPC;
2 changes: 1 addition & 1 deletion app/main/utils/menu/createMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { app, Menu } from 'electron';
import os from 'os';
import store from 'store/storeMain';
import { widgetMakeRequest } from 'actions/widget';
import openPreference from 'main/utils/window/openPreference';
import { openPreference } from 'main/utils/window/preference';
import i18n from 'constants/i18n';

// reference by https://electronjs.org/docs/api/menu#examples
Expand Down
2 changes: 1 addition & 1 deletion app/main/utils/menu/trayMenuBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
TRAY_ICON_PATH,
} from 'config';
import i18n from 'constants/i18n';
import openPreference from 'main/utils/window/openPreference';
import { openPreference } from 'main/utils/window/preference';

const trayMenuBar = menuBar({
icon: TRAY_ICON_PATH,
Expand Down
Loading

0 comments on commit 1667dd8

Please sign in to comment.