Skip to content

Commit d756682

Browse files
author
Brendan Mulholland
committed
Switch to recommended inter-process communication
1 parent 81a8748 commit d756682

File tree

5 files changed

+181
-104
lines changed

5 files changed

+181
-104
lines changed

main.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,12 @@ menubarApp.on('ready', () => {
7777
}
7878
}
7979
});
80-
ipcMain.on('get-platform', (event) => {
81-
event.returnValue = process.platform;
80+
ipcMain.handle('get-platform', async () => {
81+
return process.platform;
82+
});
83+
84+
ipcMain.handle('get-app-version', async () => {
85+
return app.getVersion();
8286
});
8387

8488
menubarApp.window.webContents.on('devtools-opened', () => {

src/__mocks__/@electron/remote.js

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ module.exports = {
3131
platform: 'darwin',
3232
},
3333
app: {
34-
getVersion: () => '0.0.1',
3534
getLoginItemSettings: jest.fn(),
3635
setLoginItemSettings: () => {},
3736
},

src/__mocks__/electron.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,17 @@ module.exports = {
3131
ipcRenderer: {
3232
send: jest.fn(),
3333
on: jest.fn(),
34-
sendSync: () => 'darwin',
34+
sendSync: jest.fn(),
35+
invoke: jest.fn((channel, ...args) => {
36+
switch (channel) {
37+
case 'get-platform':
38+
return Promise.resolve('darwin');
39+
case 'get-app-version':
40+
return Promise.resolve('0.0.1');
41+
default:
42+
return Promise.reject(new Error(`Unknown channel: ${channel}`));
43+
}
44+
}),
3545
},
3646
shell: {
3747
openExternal: jest.fn(),

src/routes/Settings.test.tsx

+149-92
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import TestRenderer from 'react-test-renderer';
2+
import TestRenderer, { act } from 'react-test-renderer';
33
import { render, fireEvent } from '@testing-library/react';
44
import { Router } from 'react-router';
55
import { MemoryRouter } from 'react-router-dom';
@@ -26,29 +26,38 @@ describe('routes/Settings.tsx', () => {
2626
updateSetting.mockReset();
2727
});
2828

29-
it('should render itself & its children', () => {
30-
const tree = TestRenderer.create(
31-
<AppContext.Provider value={{ settings: mockSettings }}>
32-
<MemoryRouter>
33-
<SettingsRoute />
34-
</MemoryRouter>
35-
</AppContext.Provider>,
36-
);
29+
it('should render itself & its children', async () => {
30+
let tree: TestRenderer;
31+
32+
await act(async () => {
33+
tree = TestRenderer.create(
34+
<AppContext.Provider value={{ settings: mockSettings }}>
35+
<MemoryRouter>
36+
<SettingsRoute />
37+
</MemoryRouter>
38+
</AppContext.Provider>,
39+
);
40+
});
3741
expect(tree).toMatchSnapshot();
3842
});
3943

40-
it('should press the logout', () => {
44+
it('should press the logout', async () => {
4145
const logoutMock = jest.fn();
42-
43-
const { getByLabelText } = render(
44-
<AppContext.Provider
45-
value={{ settings: mockSettings, logout: logoutMock }}
46-
>
47-
<Router location={history.location} navigator={history}>
48-
<SettingsRoute />
49-
</Router>
50-
</AppContext.Provider>,
51-
);
46+
let getByLabelText;
47+
48+
await act(async () => {
49+
const { getByLabelText: getByLabelTextLocal } = render(
50+
<AppContext.Provider
51+
value={{ settings: mockSettings, logout: logoutMock }}
52+
>
53+
<Router location={history.location} navigator={history}>
54+
<SettingsRoute />
55+
</Router>
56+
</AppContext.Provider>,
57+
);
58+
59+
getByLabelText = getByLabelTextLocal;
60+
});
5261

5362
fireEvent.click(getByLabelText('Logout'));
5463

@@ -59,26 +68,37 @@ describe('routes/Settings.tsx', () => {
5968
expect(mockNavigate).toHaveBeenNthCalledWith(1, -1);
6069
});
6170

62-
it('should go back by pressing the icon', () => {
63-
const { getByLabelText } = render(
64-
<AppContext.Provider value={{ settings: mockSettings }}>
65-
<Router location={history.location} navigator={history}>
66-
<SettingsRoute />
67-
</Router>
68-
</AppContext.Provider>,
69-
);
71+
it('should go back by pressing the icon', async () => {
72+
let getByLabelText;
73+
74+
await act(async () => {
75+
const { getByLabelText: getByLabelTextLocal } = render(
76+
<AppContext.Provider value={{ settings: mockSettings }}>
77+
<Router location={history.location} navigator={history}>
78+
<SettingsRoute />
79+
</Router>
80+
</AppContext.Provider>,
81+
);
82+
83+
getByLabelText = getByLabelTextLocal;
84+
});
7085
fireEvent.click(getByLabelText('Go Back'));
7186
expect(mockNavigate).toHaveBeenNthCalledWith(1, -1);
7287
});
7388

74-
it('should toggle the showOnlyParticipating checkbox', () => {
75-
const { getByLabelText } = render(
76-
<AppContext.Provider value={{ settings: mockSettings, updateSetting }}>
77-
<MemoryRouter>
78-
<SettingsRoute />
79-
</MemoryRouter>
80-
</AppContext.Provider>,
81-
);
89+
it('should toggle the showOnlyParticipating checkbox', async () => {
90+
let getByLabelText;
91+
92+
await act(async () => {
93+
const { getByLabelText: getByLabelTextLocal } = render(
94+
<AppContext.Provider value={{ settings: mockSettings, updateSetting }}>
95+
<MemoryRouter>
96+
<SettingsRoute />
97+
</MemoryRouter>
98+
</AppContext.Provider>,
99+
);
100+
getByLabelText = getByLabelTextLocal;
101+
});
82102

83103
fireEvent.click(getByLabelText('Show only participating'), {
84104
target: { checked: true },
@@ -88,14 +108,19 @@ describe('routes/Settings.tsx', () => {
88108
expect(updateSetting).toHaveBeenCalledWith('participating', false);
89109
});
90110

91-
it('should toggle the playSound checkbox', () => {
92-
const { getByLabelText } = render(
93-
<AppContext.Provider value={{ settings: mockSettings, updateSetting }}>
94-
<MemoryRouter>
95-
<SettingsRoute />
96-
</MemoryRouter>
97-
</AppContext.Provider>,
98-
);
111+
it('should toggle the playSound checkbox', async () => {
112+
let getByLabelText;
113+
114+
await act(async () => {
115+
const { getByLabelText: getByLabelTextLocal } = render(
116+
<AppContext.Provider value={{ settings: mockSettings, updateSetting }}>
117+
<MemoryRouter>
118+
<SettingsRoute />
119+
</MemoryRouter>
120+
</AppContext.Provider>,
121+
);
122+
getByLabelText = getByLabelTextLocal;
123+
});
99124

100125
fireEvent.click(getByLabelText('Play sound'), {
101126
target: { checked: true },
@@ -105,14 +130,19 @@ describe('routes/Settings.tsx', () => {
105130
expect(updateSetting).toHaveBeenCalledWith('playSound', false);
106131
});
107132

108-
it('should toggle the showNotifications checkbox', () => {
109-
const { getByLabelText } = render(
110-
<AppContext.Provider value={{ settings: mockSettings, updateSetting }}>
111-
<MemoryRouter>
112-
<SettingsRoute />
113-
</MemoryRouter>
114-
</AppContext.Provider>,
115-
);
133+
it('should toggle the showNotifications checkbox', async () => {
134+
let getByLabelText;
135+
136+
await act(async () => {
137+
const { getByLabelText: getByLabelTextLocal } = render(
138+
<AppContext.Provider value={{ settings: mockSettings, updateSetting }}>
139+
<MemoryRouter>
140+
<SettingsRoute />
141+
</MemoryRouter>
142+
</AppContext.Provider>,
143+
);
144+
getByLabelText = getByLabelTextLocal;
145+
});
116146

117147
fireEvent.click(getByLabelText('Show notifications'), {
118148
target: { checked: true },
@@ -122,14 +152,19 @@ describe('routes/Settings.tsx', () => {
122152
expect(updateSetting).toHaveBeenCalledWith('showNotifications', false);
123153
});
124154

125-
it('should toggle the onClickMarkAsRead checkbox', () => {
126-
const { getByLabelText } = render(
127-
<AppContext.Provider value={{ settings: mockSettings, updateSetting }}>
128-
<MemoryRouter>
129-
<SettingsRoute />
130-
</MemoryRouter>
131-
</AppContext.Provider>,
132-
);
155+
it('should toggle the onClickMarkAsRead checkbox', async () => {
156+
let getByLabelText;
157+
158+
await act(async () => {
159+
const { getByLabelText: getByLabelTextLocal } = render(
160+
<AppContext.Provider value={{ settings: mockSettings, updateSetting }}>
161+
<MemoryRouter>
162+
<SettingsRoute />
163+
</MemoryRouter>
164+
</AppContext.Provider>,
165+
);
166+
getByLabelText = getByLabelTextLocal;
167+
});
133168

134169
fireEvent.click(getByLabelText('Mark as read on click'), {
135170
target: { checked: true },
@@ -139,14 +174,19 @@ describe('routes/Settings.tsx', () => {
139174
expect(updateSetting).toHaveBeenCalledWith('markOnClick', false);
140175
});
141176

142-
it('should toggle the openAtStartup checkbox', () => {
143-
const { getByLabelText } = render(
144-
<AppContext.Provider value={{ settings: mockSettings, updateSetting }}>
145-
<MemoryRouter>
146-
<SettingsRoute />
147-
</MemoryRouter>
148-
</AppContext.Provider>,
149-
);
177+
it('should toggle the openAtStartup checkbox', async () => {
178+
let getByLabelText;
179+
180+
await act(async () => {
181+
const { getByLabelText: getByLabelTextLocal } = render(
182+
<AppContext.Provider value={{ settings: mockSettings, updateSetting }}>
183+
<MemoryRouter>
184+
<SettingsRoute />
185+
</MemoryRouter>
186+
</AppContext.Provider>,
187+
);
188+
getByLabelText = getByLabelTextLocal;
189+
});
150190

151191
fireEvent.click(getByLabelText('Open at startup'), {
152192
target: { checked: true },
@@ -156,43 +196,60 @@ describe('routes/Settings.tsx', () => {
156196
expect(updateSetting).toHaveBeenCalledWith('openAtStartup', false);
157197
});
158198

159-
it('should change the appearance radio group', () => {
160-
const { getByLabelText } = render(
161-
<AppContext.Provider value={{ settings: mockSettings, updateSetting }}>
162-
<MemoryRouter>
163-
<SettingsRoute />
164-
</MemoryRouter>
165-
</AppContext.Provider>,
166-
);
199+
it('should change the appearance radio group', async () => {
200+
let getByLabelText;
201+
202+
await act(async () => {
203+
const { getByLabelText: getByLabelTextLocal } = render(
204+
<AppContext.Provider value={{ settings: mockSettings, updateSetting }}>
205+
<MemoryRouter>
206+
<SettingsRoute />
207+
</MemoryRouter>
208+
</AppContext.Provider>,
209+
);
210+
getByLabelText = getByLabelTextLocal;
211+
});
167212

168213
fireEvent.click(getByLabelText('Light'));
169214

170215
expect(updateSetting).toHaveBeenCalledTimes(1);
171216
expect(updateSetting).toHaveBeenCalledWith('appearance', 'LIGHT');
172217
});
173218

174-
it('should go to the enterprise login route', () => {
175-
const { getByLabelText } = render(
176-
<AppContext.Provider value={{ settings: mockSettings }}>
177-
<Router location={history.location} navigator={history}>
178-
<SettingsRoute />
179-
</Router>
180-
</AppContext.Provider>,
181-
);
219+
it('should go to the enterprise login route', async () => {
220+
let getByLabelText;
221+
222+
await act(async () => {
223+
const { getByLabelText: getByLabelTextLocal } = render(
224+
<AppContext.Provider value={{ settings: mockSettings }}>
225+
<Router location={history.location} navigator={history}>
226+
<SettingsRoute />
227+
</Router>
228+
</AppContext.Provider>,
229+
);
230+
getByLabelText = getByLabelTextLocal;
231+
});
232+
182233
fireEvent.click(getByLabelText('Login with GitHub Enterprise'));
183234
expect(mockNavigate).toHaveBeenNthCalledWith(1, '/login-enterprise', {
184235
replace: true,
185236
});
186237
});
187238

188-
it('should quit the app', () => {
189-
const { getByLabelText } = render(
190-
<AppContext.Provider value={{ settings: mockSettings }}>
191-
<MemoryRouter>
192-
<SettingsRoute />
193-
</MemoryRouter>
194-
</AppContext.Provider>,
195-
);
239+
it('should quit the app', async () => {
240+
let getByLabelText;
241+
242+
await act(async () => {
243+
const { getByLabelText: getByLabelTextLocal } = render(
244+
<AppContext.Provider value={{ settings: mockSettings }}>
245+
<MemoryRouter>
246+
<SettingsRoute />
247+
</MemoryRouter>
248+
</AppContext.Provider>,
249+
);
250+
getByLabelText = getByLabelTextLocal;
251+
});
252+
196253
fireEvent.click(getByLabelText('Quit Gitify'));
197254
expect(ipcRenderer.send).toHaveBeenCalledWith('app-quit');
198255
});

0 commit comments

Comments
 (0)