Skip to content

Commit

Permalink
Switch to recommended inter-process communication
Browse files Browse the repository at this point in the history
  • Loading branch information
Brendan Mulholland committed Oct 3, 2023
1 parent 81a8748 commit d756682
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 104 deletions.
8 changes: 6 additions & 2 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,12 @@ menubarApp.on('ready', () => {
}
}
});
ipcMain.on('get-platform', (event) => {
event.returnValue = process.platform;
ipcMain.handle('get-platform', async () => {
return process.platform;
});

ipcMain.handle('get-app-version', async () => {
return app.getVersion();
});

menubarApp.window.webContents.on('devtools-opened', () => {
Expand Down
1 change: 0 additions & 1 deletion src/__mocks__/@electron/remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ module.exports = {
platform: 'darwin',
},
app: {
getVersion: () => '0.0.1',
getLoginItemSettings: jest.fn(),
setLoginItemSettings: () => {},
},
Expand Down
12 changes: 11 additions & 1 deletion src/__mocks__/electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,17 @@ module.exports = {
ipcRenderer: {
send: jest.fn(),
on: jest.fn(),
sendSync: () => 'darwin',
sendSync: jest.fn(),
invoke: jest.fn((channel, ...args) => {
switch (channel) {
case 'get-platform':
return Promise.resolve('darwin');
case 'get-app-version':
return Promise.resolve('0.0.1');
default:
return Promise.reject(new Error(`Unknown channel: ${channel}`));
}
}),
},
shell: {
openExternal: jest.fn(),
Expand Down
241 changes: 149 additions & 92 deletions src/routes/Settings.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import TestRenderer from 'react-test-renderer';
import TestRenderer, { act } from 'react-test-renderer';
import { render, fireEvent } from '@testing-library/react';
import { Router } from 'react-router';
import { MemoryRouter } from 'react-router-dom';
Expand All @@ -26,29 +26,38 @@ describe('routes/Settings.tsx', () => {
updateSetting.mockReset();
});

it('should render itself & its children', () => {
const tree = TestRenderer.create(
<AppContext.Provider value={{ settings: mockSettings }}>
<MemoryRouter>
<SettingsRoute />
</MemoryRouter>
</AppContext.Provider>,
);
it('should render itself & its children', async () => {
let tree: TestRenderer;

await act(async () => {
tree = TestRenderer.create(
<AppContext.Provider value={{ settings: mockSettings }}>
<MemoryRouter>
<SettingsRoute />
</MemoryRouter>
</AppContext.Provider>,
);
});
expect(tree).toMatchSnapshot();
});

it('should press the logout', () => {
it('should press the logout', async () => {
const logoutMock = jest.fn();

const { getByLabelText } = render(
<AppContext.Provider
value={{ settings: mockSettings, logout: logoutMock }}
>
<Router location={history.location} navigator={history}>
<SettingsRoute />
</Router>
</AppContext.Provider>,
);
let getByLabelText;

await act(async () => {
const { getByLabelText: getByLabelTextLocal } = render(
<AppContext.Provider
value={{ settings: mockSettings, logout: logoutMock }}
>
<Router location={history.location} navigator={history}>
<SettingsRoute />
</Router>
</AppContext.Provider>,
);

getByLabelText = getByLabelTextLocal;
});

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

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

it('should go back by pressing the icon', () => {
const { getByLabelText } = render(
<AppContext.Provider value={{ settings: mockSettings }}>
<Router location={history.location} navigator={history}>
<SettingsRoute />
</Router>
</AppContext.Provider>,
);
it('should go back by pressing the icon', async () => {
let getByLabelText;

await act(async () => {
const { getByLabelText: getByLabelTextLocal } = render(
<AppContext.Provider value={{ settings: mockSettings }}>
<Router location={history.location} navigator={history}>
<SettingsRoute />
</Router>
</AppContext.Provider>,
);

getByLabelText = getByLabelTextLocal;
});
fireEvent.click(getByLabelText('Go Back'));
expect(mockNavigate).toHaveBeenNthCalledWith(1, -1);
});

it('should toggle the showOnlyParticipating checkbox', () => {
const { getByLabelText } = render(
<AppContext.Provider value={{ settings: mockSettings, updateSetting }}>
<MemoryRouter>
<SettingsRoute />
</MemoryRouter>
</AppContext.Provider>,
);
it('should toggle the showOnlyParticipating checkbox', async () => {
let getByLabelText;

await act(async () => {
const { getByLabelText: getByLabelTextLocal } = render(
<AppContext.Provider value={{ settings: mockSettings, updateSetting }}>
<MemoryRouter>
<SettingsRoute />
</MemoryRouter>
</AppContext.Provider>,
);
getByLabelText = getByLabelTextLocal;
});

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

it('should toggle the playSound checkbox', () => {
const { getByLabelText } = render(
<AppContext.Provider value={{ settings: mockSettings, updateSetting }}>
<MemoryRouter>
<SettingsRoute />
</MemoryRouter>
</AppContext.Provider>,
);
it('should toggle the playSound checkbox', async () => {
let getByLabelText;

await act(async () => {
const { getByLabelText: getByLabelTextLocal } = render(
<AppContext.Provider value={{ settings: mockSettings, updateSetting }}>
<MemoryRouter>
<SettingsRoute />
</MemoryRouter>
</AppContext.Provider>,
);
getByLabelText = getByLabelTextLocal;
});

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

it('should toggle the showNotifications checkbox', () => {
const { getByLabelText } = render(
<AppContext.Provider value={{ settings: mockSettings, updateSetting }}>
<MemoryRouter>
<SettingsRoute />
</MemoryRouter>
</AppContext.Provider>,
);
it('should toggle the showNotifications checkbox', async () => {
let getByLabelText;

await act(async () => {
const { getByLabelText: getByLabelTextLocal } = render(
<AppContext.Provider value={{ settings: mockSettings, updateSetting }}>
<MemoryRouter>
<SettingsRoute />
</MemoryRouter>
</AppContext.Provider>,
);
getByLabelText = getByLabelTextLocal;
});

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

it('should toggle the onClickMarkAsRead checkbox', () => {
const { getByLabelText } = render(
<AppContext.Provider value={{ settings: mockSettings, updateSetting }}>
<MemoryRouter>
<SettingsRoute />
</MemoryRouter>
</AppContext.Provider>,
);
it('should toggle the onClickMarkAsRead checkbox', async () => {
let getByLabelText;

await act(async () => {
const { getByLabelText: getByLabelTextLocal } = render(
<AppContext.Provider value={{ settings: mockSettings, updateSetting }}>
<MemoryRouter>
<SettingsRoute />
</MemoryRouter>
</AppContext.Provider>,
);
getByLabelText = getByLabelTextLocal;
});

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

it('should toggle the openAtStartup checkbox', () => {
const { getByLabelText } = render(
<AppContext.Provider value={{ settings: mockSettings, updateSetting }}>
<MemoryRouter>
<SettingsRoute />
</MemoryRouter>
</AppContext.Provider>,
);
it('should toggle the openAtStartup checkbox', async () => {
let getByLabelText;

await act(async () => {
const { getByLabelText: getByLabelTextLocal } = render(
<AppContext.Provider value={{ settings: mockSettings, updateSetting }}>
<MemoryRouter>
<SettingsRoute />
</MemoryRouter>
</AppContext.Provider>,
);
getByLabelText = getByLabelTextLocal;
});

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

it('should change the appearance radio group', () => {
const { getByLabelText } = render(
<AppContext.Provider value={{ settings: mockSettings, updateSetting }}>
<MemoryRouter>
<SettingsRoute />
</MemoryRouter>
</AppContext.Provider>,
);
it('should change the appearance radio group', async () => {
let getByLabelText;

await act(async () => {
const { getByLabelText: getByLabelTextLocal } = render(
<AppContext.Provider value={{ settings: mockSettings, updateSetting }}>
<MemoryRouter>
<SettingsRoute />
</MemoryRouter>
</AppContext.Provider>,
);
getByLabelText = getByLabelTextLocal;
});

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

expect(updateSetting).toHaveBeenCalledTimes(1);
expect(updateSetting).toHaveBeenCalledWith('appearance', 'LIGHT');
});

it('should go to the enterprise login route', () => {
const { getByLabelText } = render(
<AppContext.Provider value={{ settings: mockSettings }}>
<Router location={history.location} navigator={history}>
<SettingsRoute />
</Router>
</AppContext.Provider>,
);
it('should go to the enterprise login route', async () => {
let getByLabelText;

await act(async () => {
const { getByLabelText: getByLabelTextLocal } = render(
<AppContext.Provider value={{ settings: mockSettings }}>
<Router location={history.location} navigator={history}>
<SettingsRoute />
</Router>
</AppContext.Provider>,
);
getByLabelText = getByLabelTextLocal;
});

fireEvent.click(getByLabelText('Login with GitHub Enterprise'));
expect(mockNavigate).toHaveBeenNthCalledWith(1, '/login-enterprise', {
replace: true,
});
});

it('should quit the app', () => {
const { getByLabelText } = render(
<AppContext.Provider value={{ settings: mockSettings }}>
<MemoryRouter>
<SettingsRoute />
</MemoryRouter>
</AppContext.Provider>,
);
it('should quit the app', async () => {
let getByLabelText;

await act(async () => {
const { getByLabelText: getByLabelTextLocal } = render(
<AppContext.Provider value={{ settings: mockSettings }}>
<MemoryRouter>
<SettingsRoute />
</MemoryRouter>
</AppContext.Provider>,
);
getByLabelText = getByLabelTextLocal;
});

fireEvent.click(getByLabelText('Quit Gitify'));
expect(ipcRenderer.send).toHaveBeenCalledWith('app-quit');
});
Expand Down
Loading

0 comments on commit d756682

Please sign in to comment.