Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use ipcRenderer.sendSync instead of remote.getGlobal #314

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions packages/electron-redux/src/__mocks__/electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,5 @@ export const ipcMain = {
export const ipcRenderer = {
on: jest.fn(),
send: jest.fn(),
};

export const remote = {
getGlobal: jest.fn(),
sendSync: jest.fn(),
};
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { remote } from 'electron';
import { ipcRenderer } from 'electron';
import getInitialStateRenderer from '../getInitialStateRenderer';

jest.unmock('../getInitialStateRenderer');

describe('getInitialStateRenderer', () => {
it('should return the initial state', () => {
const state = { foo: 456 };
remote.getGlobal.mockImplementation(() => () => JSON.stringify(state));
ipcRenderer.sendSync.mockImplementation(() => JSON.stringify(state));

expect(getInitialStateRenderer()).toEqual(state);
});
Expand Down
34 changes: 24 additions & 10 deletions packages/electron-redux/src/helpers/__tests__/replayActionMain.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import replayActionMain from '../replayActionMain';
jest.unmock('../replayActionMain');

describe('replayActionMain', () => {
afterEach(() => {
jest.resetAllMocks();
});

it('should replay any actions received', () => {
const store = {
dispatch: jest.fn(),
Expand All @@ -14,35 +18,45 @@ describe('replayActionMain', () => {

replayActionMain(store);

expect(ipcMain.on).toHaveBeenCalledTimes(1);
expect(ipcMain.on.mock.calls[0][0]).toBe('redux-action');
expect(ipcMain.on.mock.calls[0][1]).toBeInstanceOf(Function);
expect(ipcMain.on).toHaveBeenCalledTimes(2);
expect(ipcMain.on.mock.calls[1][0]).toBe('redux-action');
expect(ipcMain.on.mock.calls[1][1]).toBeInstanceOf(Function);

const cb = ipcMain.on.mock.calls[0][1];
const cb = ipcMain.on.mock.calls[1][1];
cb('someEvent', payload);

expect(store.dispatch).toHaveBeenCalledTimes(1);
expect(store.dispatch).toHaveBeenCalledWith(payload);
});

it('should return the current state from the global', () => {
const initialState = { initial: 'state' };
const newState = { new: 'state' };
it('should reply current state', () => {
const store = {
dispatch: jest.fn(),
getState: jest.fn(),
subscribe: jest.fn(),
};

const initialState = { initial: 'state' };
const newState = { new: 'state' };

replayActionMain(store);

store.getState.mockReturnValueOnce(initialState);
store.getState.mockReturnValueOnce(newState);

replayActionMain(store);
expect(ipcMain.on).toHaveBeenCalledTimes(2);
expect(ipcMain.on.mock.calls[0][0]).toBe('get-redux-state');
expect(ipcMain.on.mock.calls[0][1]).toBeInstanceOf(Function);

const cb = ipcMain.on.mock.calls[0][1];
const event = { returnValue: '' };
cb(event);

expect(global.getReduxState()).toEqual(JSON.stringify(initialState));
expect(event.returnValue).toEqual(JSON.stringify(initialState));
expect(store.getState).toHaveBeenCalledTimes(1);

expect(global.getReduxState()).toEqual(JSON.stringify(newState));
cb(event);
expect(event.returnValue).toEqual(JSON.stringify(newState));
expect(store.getState).toHaveBeenCalledTimes(2);
});
});
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { remote } from 'electron';
import { ipcRenderer } from 'electron';

export default function getInitialStateRenderer() {
const getReduxState = remote.getGlobal('getReduxState');
if (!getReduxState) {
const reduxState = ipcRenderer.sendSync('get-redux-state');
if (!reduxState) {
throw new Error(
'Could not find reduxState global in main process, did you forget to call replayActionMain?',
);
}
return JSON.parse(getReduxState());
return JSON.parse(reduxState);
}
4 changes: 3 additions & 1 deletion packages/electron-redux/src/helpers/replayActionMain.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ export default function replayActionMain(store) {
*
* Refer to https://github.com/electron/electron/blob/master/docs/api/remote.md#remote-objects
*/
global.getReduxState = () => JSON.stringify(store.getState());
ipcMain.on('get-redux-state', (event) => {
event.returnValue = JSON.stringify(store.getState());
});

ipcMain.on('redux-action', (event, payload) => {
store.dispatch(payload);
Expand Down