Skip to content

Commit

Permalink
test: mock navigator.locks and fix existing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
erikian committed Jul 23, 2023
1 parent 96a196b commit c22780c
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 3 deletions.
3 changes: 3 additions & 0 deletions tests/mocks/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { objectDifference } from '../utils';
export class StateMock {
public acceleratorsToBlock: BlockableAccelerator[] = [];
public activeGistAction = GistActionState.none;
public activeVersions = new Set<string>();
public channelsToShow: ElectronReleaseChannel[] = [];
public editorMosaic = new EditorMosaic();
public environmentVariables: string[] = [];
Expand Down Expand Up @@ -85,6 +86,7 @@ export class StateMock {
public setVersion = jest.fn().mockImplementation((version: string) => {
this.currentElectronVersion = this.versions[version];
this.version = version;
this.activeVersions.add(version);
});
public isVersionUsable = jest.fn().mockImplementation(() => {
return { ver: this.currentElectronVersion };
Expand Down Expand Up @@ -120,6 +122,7 @@ export class StateMock {
makeObservable(this, {
acceleratorsToBlock: observable,
activeGistAction: observable,
activeVersions: observable,
channelsToShow: observable,
editorMosaic: observable,
environmentVariables: observable,
Expand Down
10 changes: 8 additions & 2 deletions tests/renderer/components/settings-electron-spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@ import { ElectronSettings } from '../../../src/renderer/components/settings-elec
import { AppState } from '../../../src/renderer/state';
import { disableDownload } from '../../../src/renderer/utils/disable-download';
import { AppMock, StateMock, VersionsMock } from '../../mocks/mocks';
import { waitFor } from '../../utils';

jest.mock('../../../src/renderer/utils/disable-download.ts');

describe('ElectronSettings component', () => {
let store: StateMock;
let mockVersions: Record<string, RunnableVersion>;
let mockVersionsArray: RunnableVersion[];
const version = '2.0.1';

beforeEach(() => {
({ mockVersions, mockVersionsArray } = new VersionsMock());
({ state: store } = window.ElectronFiddle.app as unknown as AppMock);

store.initVersions('2.0.1', { ...mockVersions });
store.initVersions(version, { ...mockVersions });
store.channelsToShow = [
ElectronReleaseChannel.stable,
ElectronReleaseChannel.beta,
Expand All @@ -38,7 +40,7 @@ describe('ElectronSettings component', () => {
store.versionsToShow[i++].state = InstallState.installing;
});

it('renders', () => {
it('renders', async () => {
const spy = jest
.spyOn(window.ElectronFiddle, 'getOldestSupportedMajor')
.mockReturnValue(9);
Expand All @@ -64,6 +66,10 @@ describe('ElectronSettings component', () => {
const wrapper = shallow(
<ElectronSettings appState={store as unknown as AppState} />,
);

await store.setVersion(version);
await waitFor(() => store.activeVersions.size > 0);

expect(wrapper).toMatchSnapshot();

spy.mockRestore();
Expand Down
10 changes: 9 additions & 1 deletion tests/renderer/state-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ import {
saveLocalVersions,
} from '../../src/renderer/versions';
import { VersionsMock, createEditorValues } from '../mocks/mocks';
import { overrideRendererPlatform, resetRendererPlatform } from '../utils';
import {
overrideRendererPlatform,
resetRendererPlatform,
waitFor,
} from '../utils';

jest.mock('../../src/renderer/versions', () => {
const { getReleaseChannel } = jest.requireActual(
Expand Down Expand Up @@ -310,6 +314,10 @@ describe('AppState', () => {

it('does not remove the active version', async () => {
const ver = appState.versions[active];

await appState.setVersion(ver.version);
await waitFor(() => appState.activeVersions.size > 0);

broadcastMessageSpy.mockClear();
await appState.removeVersion(ver);
expect(removeSpy).not.toHaveBeenCalled();
Expand Down
46 changes: 46 additions & 0 deletions tests/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,52 @@ delete (window as any).localStorage;
window.navigator = window.navigator ?? {};
(window.navigator.clipboard as any) = {};

class FakeNavigatorLocks implements LockManager {
locks = {
held: new Set<Lock>(),
pending: new Set<Lock>(),
};

query = async () => {
const result = {
held: [...this.locks.held],
pending: [...this.locks.pending],
};

return result as LockManagerSnapshot;
};

/**
* WIP. Right now, this is a **very** naive mock that will just happily grant a shared lock when one is requested,
* but I'll add some bookkeeping and expand it to cover the exclusive lock case as well.
*
* @TODO remove this comment
*/
request = (async (...args: Parameters<LockManager['request']>) => {
const [
name,
options = {
mode: 'exclusive',
},
cb,
] = args;

const { mode } = options;

const lock = { name, mode, cb } as Lock;

if (mode === 'shared') {
this.locks.held.add(lock);

await cb(lock);

return;
}
}) as LockManager['request'];
}

(window.navigator.locks as any) = new FakeNavigatorLocks();

/**
* Mock these properties twice so that they're available
* both at the top-level of files and also within the
Expand Down

0 comments on commit c22780c

Please sign in to comment.