Skip to content

Commit

Permalink
🐛 en-us dictionary has a new different key (en)
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Nuri <[email protected]>
  • Loading branch information
manusa committed Oct 12, 2023
1 parent 3a30e8e commit dcc4ea0
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 22 deletions.
27 changes: 15 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@
"dictionary-ca-valencia": "2.6.0",
"dictionary-de": "2.0.4",
"dictionary-en-gb": "2.4.0",
"dictionary-en-us": "3.0.0",
"dictionary-en": "3.2.0",
"dictionary-es": "3.2.0",
"dictionary-eu": "3.2.0",
"dictionary-fr": "2.8.0",
Expand Down
25 changes: 19 additions & 6 deletions src/settings/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('Settings module test suite', () => {
expectHomeDirectoryCreated();
expect(fs.readFileSync).toHaveBeenCalledTimes(1);
expect(result.tabs).toEqual([]);
expect(result.enabledDictionaries).toEqual(['en-US']);
expect(result.enabledDictionaries).toEqual(['en']);
expect(result.theme).toEqual('system');
});
test('settings (empty) exist, should load settings from file system and merge with defaults', () => {
Expand All @@ -56,7 +56,7 @@ describe('Settings module test suite', () => {
expectHomeDirectoryCreated();
expect(fs.readFileSync).toHaveBeenCalledWith(path.join('$HOME', '.electronim', 'settings.json'));
expect(result.tabs).toEqual([]);
expect(result.enabledDictionaries).toEqual(['en-US']);
expect(result.enabledDictionaries).toEqual(['en']);
expect(result.theme).toEqual('system');
expect(result.trayEnabled).toEqual(false);
});
Expand All @@ -70,7 +70,7 @@ describe('Settings module test suite', () => {
expectHomeDirectoryCreated();
expect(fs.readFileSync).toHaveBeenCalledWith(path.join('$HOME', '.electronim', 'settings.json'));
expect(result.tabs).toEqual([{id: '1'}]);
expect(result.enabledDictionaries).toEqual(['en-US']);
expect(result.enabledDictionaries).toEqual(['en']);
expect(result.activeTab).toBe('1');
expect(result.otherSetting).toBe(1337);
});
Expand All @@ -84,6 +84,19 @@ describe('Settings module test suite', () => {
expect(result.tabs).toEqual([{id: '1', disabled: true}, {id: '2'}]);
expect(result.activeTab).toBe('2');
});
test.each([
{enabledDictionaries: ['en-US', 'es'], expected: ['es', 'en']},
{enabledDictionaries: ['en-US', 'en', 'es'], expected: ['es', 'en']},
{enabledDictionaries: ['en-GB', 'en'], expected: ['en-GB', 'en']}
])('settings with $enabledDictionaries dictionary, should migrate to $expected', ({enabledDictionaries, expected}) => {
// Given
fs.existsSync.mockImplementationOnce(() => true);
fs.readFileSync.mockImplementationOnce(() => JSON.stringify({enabledDictionaries}));
// When
const result = settings.loadSettings();
// Then
expect(result.enabledDictionaries).toEqual(expected);
});
test.each([
{key: 'theme', value: '"light"', expected: 'light'},
{key: 'activeTab', value: 42, expected: 42},
Expand All @@ -110,7 +123,7 @@ describe('Settings module test suite', () => {
expect(fs.writeFileSync).toHaveBeenCalledWith(path.join('$HOME', '.electronim', 'settings.json'),
'{\n "tabs": [],\n' +
' "useNativeSpellChecker": false,\n' +
' "enabledDictionaries": [\n "en-US"\n ],\n' +
' "enabledDictionaries": [\n "en"\n ],\n' +
' "theme": "system",\n "trayEnabled": false,\n' +
' "closeButtonBehavior": "quit"\n' +
'}');
Expand All @@ -124,7 +137,7 @@ describe('Settings module test suite', () => {
expect(fs.writeFileSync).toHaveBeenCalledWith(path.join('$HOME', '.electronim', 'settings.json'),
'{\n "tabs": [\n {\n "id": 1337\n }\n ],\n' +
' "useNativeSpellChecker": false,\n' +
' "enabledDictionaries": [\n "en-US"\n ],\n' +
' "enabledDictionaries": [\n "en"\n ],\n' +
' "theme": "system",\n' +
' "trayEnabled": false,\n' +
' "closeButtonBehavior": "quit",\n' +
Expand All @@ -141,7 +154,7 @@ describe('Settings module test suite', () => {
expect(fs.writeFileSync).toHaveBeenCalledWith(path.join('$HOME', '.electronim', 'settings.json'),
'{\n "tabs": [\n {\n "id": 1337\n }\n ],\n' +
' "useNativeSpellChecker": false,\n' +
' "enabledDictionaries": [\n "en-US"\n ],\n' +
' "enabledDictionaries": [\n "en"\n ],\n' +
' "theme": "system",\n' +
' "trayEnabled": false,\n' +
' "closeButtonBehavior": "quit",\n' +
Expand Down
15 changes: 13 additions & 2 deletions src/settings/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const SETTINGS_FILE = 'settings.json';
const DEFAULT_SETTINGS = {
tabs: [],
useNativeSpellChecker: false,
enabledDictionaries: ['en-US'],
enabledDictionaries: ['en'],
theme: 'system',
trayEnabled: false,
closeButtonBehavior: CLOSE_BUTTON_BEHAVIORS.quit
Expand Down Expand Up @@ -62,6 +62,17 @@ const ensureDefaultValues = settings => {
return {...settings, activeTab};
};

const migrate = settings => {
// en-us dictionary key is now en
if (settings.enabledDictionaries.includes('en-US')) {
settings.enabledDictionaries = settings.enabledDictionaries
.filter(dictionary => dictionary !== 'en-US')
.filter(dictionary => dictionary !== 'en')
.concat('en');
}
return settings;
};

const initAppDir = () => fs.mkdirSync(appDir, {recursive: true});

const loadSettings = () => {
Expand All @@ -70,7 +81,7 @@ const loadSettings = () => {
if (fs.existsSync(settingsPath)) {
loadedSettings = JSON.parse(fs.readFileSync(settingsPath));
}
return ensureDefaultValues(loadedSettings);
return migrate(ensureDefaultValues(loadedSettings));
};

const writeSettings = settings => {
Expand Down
2 changes: 1 addition & 1 deletion src/spell-check/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const AVAILABLE_DICTIONARIES = {
'en-GB': {
name: 'English (GB)'
},
'en-US': {
en: {
name: 'English (US)'
},
es: {
Expand Down

0 comments on commit dcc4ea0

Please sign in to comment.