Skip to content

Commit

Permalink
test: add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
erikian committed Jul 11, 2023
1 parent 0222125 commit 74e216d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
26 changes: 26 additions & 0 deletions tests/renderer/state-spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { reaction } from 'mobx';

import {
AppStateBroadcastChannel,
AppStateBroadcastMessageType,
BlockableAccelerator,
ElectronReleaseChannel,
GenericDialogType,
Expand Down Expand Up @@ -743,4 +745,28 @@ describe('AppState', () => {
expect(actual).toBe(expected);
});
});

describe('broadcastChannel', () => {
it('updates the version state in response to changes in other windows', async () => {
const broadcastChannel: AppStateBroadcastChannel = new BroadcastChannel(
'AppState',
);

const fakeVersion = {
version: '13.9.9',
state: InstallState.downloading,
} as RunnableVersion;

expect(appState.versions[fakeVersion.version]).toBeFalsy();

broadcastChannel.postMessage({
type: AppStateBroadcastMessageType.syncVersions,
payload: [fakeVersion],
});

expect(appState.versions[fakeVersion.version].state).toEqual(
InstallState.downloading,
);
});
});
});
23 changes: 20 additions & 3 deletions tests/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,26 @@ jest.mock('@sentry/electron/renderer', () => ({
init: jest.fn(),
}));

global.BroadcastChannel = class {
addEventListener = jest.fn();
postMessage = jest.fn();
class FakeBroadcastChannel {
listeners = [];

addEventListener = (_, callback) => {
this.listeners.push(callback);
};

postMessage = (message) => {
for (const listener of this.listeners) {
listener(new MessageEvent('message', { data: message }));
}
};
}

global.BroadcastChannel = class Singleton {
static instance = new FakeBroadcastChannel();

constructor(_) {
return Singleton.instance;
}
};

expect.addSnapshotSerializer(createSerializer({ mode: 'deep' }));
Expand Down

0 comments on commit 74e216d

Please sign in to comment.