Skip to content

Commit 3b0ebec

Browse files
authored
fix(preferences): return default values when reading preferences (#4713)
1 parent 84772e8 commit 3b0ebec

File tree

2 files changed

+49
-20
lines changed

2 files changed

+49
-20
lines changed

packages/compass-preferences-model/src/storage.spec.ts

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
1-
import { mkdtempSync, rmdirSync, existsSync, readFileSync } from 'fs';
1+
import fs from 'fs/promises';
22
import path from 'path';
33
import os from 'os';
44
import { UserStorage, StoragePreferences, type User } from './storage';
55
import { expect } from 'chai';
66
import type { UserPreferences } from './preferences';
77

8+
const getPreferencesFolder = (tmpDir: string) => {
9+
return path.join(tmpDir, 'AppPreferences');
10+
};
11+
12+
const getPreferencesFile = (tmpDir: string) => {
13+
return path.join(getPreferencesFolder(tmpDir), 'General.json');
14+
};
15+
816
describe('storage', function () {
917
let tmpDir: string;
1018
describe('UserStorage', function () {
1119
let storedUser: User;
1220
let userStorage: UserStorage;
1321
before(async function () {
14-
tmpDir = mkdtempSync(path.join(os.tmpdir()));
22+
tmpDir = await fs.mkdtemp(path.join(os.tmpdir()));
1523
userStorage = new UserStorage(tmpDir);
1624
storedUser = await userStorage.getOrCreate('');
1725
});
18-
after(function () {
19-
rmdirSync(tmpDir, { recursive: true });
26+
after(async function () {
27+
await fs.rmdir(tmpDir, { recursive: true });
2028
});
2129

2230
it('creates a new user if user does not exist', async function () {
@@ -47,12 +55,12 @@ describe('storage', function () {
4755
const defaultPreferences = {
4856
showedNetworkOptIn: true,
4957
} as unknown as UserPreferences;
50-
beforeEach(function () {
51-
tmpDir = mkdtempSync(path.join(os.tmpdir()));
58+
beforeEach(async function () {
59+
tmpDir = await fs.mkdtemp(path.join(os.tmpdir()));
5260
});
5361

54-
afterEach(function () {
55-
rmdirSync(tmpDir, { recursive: true });
62+
afterEach(async function () {
63+
await fs.rmdir(tmpDir, { recursive: true });
5664
});
5765

5866
it('sets up the storage', async function () {
@@ -61,22 +69,19 @@ describe('storage', function () {
6169

6270
const storage = new StoragePreferences(defaultPreferences, tmpDir);
6371

64-
const preferencesDir = path.join(tmpDir, 'AppPreferences');
65-
const preferencesFile = path.join(
66-
tmpDir,
67-
'AppPreferences',
68-
'General.json'
69-
);
72+
const preferencesDir = getPreferencesFolder(tmpDir);
73+
const preferencesFile = getPreferencesFile(tmpDir);
7074

71-
expect(existsSync(preferencesDir)).to.be.false;
72-
expect(existsSync(preferencesFile)).to.be.false;
75+
expect(async () => await fs.access(preferencesDir)).to.throw;
76+
expect(async () => await fs.access(preferencesFile)).to.throw;
7377

7478
await storage.setup();
7579

76-
expect(existsSync(preferencesDir)).to.be.true;
77-
expect(existsSync(preferencesFile)).to.be.true;
80+
expect(async () => await fs.access(preferencesDir)).to.not.throw;
81+
expect(async () => await fs.access(preferencesFile)).to.not.throw;
82+
7883
expect(
79-
JSON.parse(readFileSync(preferencesFile).toString())
84+
JSON.parse((await fs.readFile(preferencesFile)).toString())
8085
).to.deep.equal(defaultPreferences);
8186
});
8287

@@ -93,5 +98,26 @@ describe('storage', function () {
9398
currentUserId: '123456789',
9499
});
95100
});
101+
102+
it('returns default preference values if its not stored on disk', async function () {
103+
const storage = new StoragePreferences(defaultPreferences, tmpDir);
104+
105+
// manually setup the file with no content
106+
await fs.mkdir(getPreferencesFolder(tmpDir));
107+
await fs.writeFile(
108+
getPreferencesFile(tmpDir),
109+
JSON.stringify({}),
110+
'utf-8'
111+
);
112+
113+
await storage.updatePreferences({
114+
currentUserId: '123456789',
115+
});
116+
117+
expect(storage.getPreferences()).to.deep.equal({
118+
...defaultPreferences,
119+
currentUserId: '123456789',
120+
});
121+
});
96122
});
97123
});

packages/compass-preferences-model/src/storage.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ export class StoragePreferences extends BasePreferencesStorage {
7575
}
7676

7777
getPreferences() {
78-
return this.preferences;
78+
return {
79+
...this.defaultPreferences,
80+
...this.preferences,
81+
};
7982
}
8083

8184
async updatePreferences(attributes: Partial<UserPreferences>) {

0 commit comments

Comments
 (0)