Skip to content

Commit 052d7d2

Browse files
authored
chore(compass-preferences-model): add tests (#3482)
1 parent 3a9eb5d commit 052d7d2

File tree

5 files changed

+126
-3
lines changed

5 files changed

+126
-3
lines changed

package-lock.json

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/compass-preferences-model/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"@mongodb-js/mocha-config-compass": "^1.0.0",
5656
"@types/lodash.isempty": "^4.4.7",
5757
"@types/lodash.pickby": "^4.6.7",
58+
"chai": "^4.3.6",
5859
"depcheck": "^1.4.1",
5960
"eslint": "^7.25.0",
6061
"hadron-ipc": "^3.0.0",
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { promises as fs } from 'fs';
2+
import path from 'path';
3+
import os from 'os';
4+
import Preferences from './preferences';
5+
import { expect } from 'chai';
6+
7+
describe('Preferences class', function () {
8+
let tmpdir: string;
9+
let i = 0;
10+
11+
beforeEach(async function () {
12+
tmpdir = path.join(os.tmpdir(), `preferences-test-${Date.now()}-${i++}`);
13+
await fs.mkdir(tmpdir, { recursive: true });
14+
});
15+
16+
after(async function () {
17+
await fs.rm(tmpdir, { recursive: true });
18+
});
19+
20+
it('allows provides default preferences', async function () {
21+
const preferences = new Preferences(tmpdir);
22+
const result = await preferences.fetchPreferences();
23+
expect(result.id).to.equal('General');
24+
expect(result.enableMaps).to.equal(false);
25+
});
26+
27+
it('allows saving preferences', async function () {
28+
const preferences = new Preferences(tmpdir);
29+
await preferences.savePreferences({ enableMaps: true });
30+
const result = await preferences.fetchPreferences();
31+
expect(result.id).to.equal('General');
32+
expect(result.enableMaps).to.equal(true);
33+
});
34+
35+
it('stores preferences across instances', async function () {
36+
const preferences1 = new Preferences(tmpdir);
37+
await preferences1.savePreferences({ enableMaps: true });
38+
const preferences2 = new Preferences(tmpdir);
39+
const result = await preferences2.fetchPreferences();
40+
expect(result.id).to.equal('General');
41+
expect(result.enableMaps).to.equal(true);
42+
});
43+
44+
it('notifies callers of preferences changes', async function () {
45+
const preferences = new Preferences(tmpdir);
46+
const calls: any[] = [];
47+
preferences.onPreferencesChanged((prefs) => calls.push(prefs));
48+
await preferences.savePreferences({ enableMaps: true });
49+
expect(calls).to.deep.equal([{ enableMaps: true }]);
50+
});
51+
52+
it('can return user-configurable preferences after setting their defaults', async function () {
53+
const preferences = new Preferences(tmpdir);
54+
const result = await preferences.getConfigurableUserPreferences();
55+
expect(result).not.to.have.property('id');
56+
expect(result.enableMaps).to.equal(true);
57+
});
58+
});
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { makePreferencesIpc } from './renderer-ipc';
2+
import { EventEmitter } from 'events';
3+
import { expect } from 'chai';
4+
5+
describe('Renderer IPC', function () {
6+
const ipcImpl = {
7+
'compass:save-preferences'(attributes) {
8+
return { savePreferences: 1, attributes };
9+
},
10+
'compass:get-preferences'() {
11+
return { getPreferences: 1 };
12+
},
13+
'compass:get-configurable-user-preferences'() {
14+
return { getConfigurableUserPreferences: 1 };
15+
},
16+
};
17+
const ipcMock = Object.assign(new EventEmitter(), {
18+
invoke(method: string, ...args: any[]) {
19+
return Promise.resolve(ipcImpl[method](...args));
20+
},
21+
});
22+
const preferencesIpc = makePreferencesIpc({ ipcRenderer: ipcMock } as any);
23+
24+
it('should be able to call savePreferences', async function () {
25+
expect(
26+
await preferencesIpc.savePreferences({ enableMaps: true })
27+
).to.deep.equal({ savePreferences: 1, attributes: { enableMaps: true } });
28+
});
29+
30+
it('should be able to call getPreferences', async function () {
31+
expect(await preferencesIpc.getPreferences()).to.deep.equal({
32+
getPreferences: 1,
33+
});
34+
});
35+
36+
it('should be able to call getConfigurableUserPreferences', async function () {
37+
expect(await preferencesIpc.getConfigurableUserPreferences()).to.deep.equal(
38+
{ getConfigurableUserPreferences: 1 }
39+
);
40+
});
41+
42+
it('should be able to listen with onPreferenceValueChanged', function () {
43+
const calls: any[] = [];
44+
const unsubscribe = preferencesIpc.onPreferenceValueChanged(
45+
'enableMaps',
46+
(value) => calls.push(value)
47+
);
48+
ipcMock.emit('compass:preferences-changed', null, {
49+
trackUserStatistics: false,
50+
});
51+
expect(calls).to.deep.equal([]);
52+
ipcMock.emit('compass:preferences-changed', null, { enableMaps: true });
53+
expect(calls).to.deep.equal([true]);
54+
ipcMock.emit('compass:preferences-changed', null, { enableMaps: true });
55+
expect(calls).to.deep.equal([true, true]);
56+
unsubscribe();
57+
ipcMock.emit('compass:preferences-changed', null, { enableMaps: true });
58+
expect(calls).to.deep.equal([true, true]);
59+
});
60+
});

packages/compass-preferences-model/src/renderer-ipc.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import ipc from 'hadron-ipc';
1+
import hadronIpc from 'hadron-ipc';
22
import type {
33
GlobalPreferences,
44
UserConfigurablePreferences,
@@ -8,7 +8,7 @@ import type {
88
/**
99
* API to communicate with preferences from the electron renderer process.
1010
*/
11-
export const preferencesIpc = {
11+
export const makePreferencesIpc = (ipc: typeof hadronIpc) => ({
1212
savePreferences(
1313
attributes: Partial<GlobalPreferences>
1414
): Promise<GlobalPreferences> {
@@ -51,4 +51,6 @@ export const preferencesIpc = {
5151
/** Missing ipc fallback */
5252
};
5353
},
54-
};
54+
});
55+
56+
export const preferencesIpc = makePreferencesIpc(hadronIpc);

0 commit comments

Comments
 (0)