-
Notifications
You must be signed in to change notification settings - Fork 456
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor(lib): initPlugin and getSettings now are more testable and plugin-agnostic Now they don't depend on the plugin import statement * test(mocks): added new mock functions * refactor(lib): separate concepts of get config and read config This makes `get` function more testable * test: added getUserSettings test * refactor(dirs): moved test files to __tests__ folders * test: added more tests * test: add coverage
- Loading branch information
Showing
16 changed files
with
261 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
module.exports = { | ||
app: { | ||
getPath: () => '', | ||
getLocale: () => '', | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
module.exports = { | ||
app: { | ||
getPath: jest.fn(), | ||
getLocale: jest.fn(), | ||
}, | ||
ipcRenderer: { | ||
on: jest.fn(), | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import themesLoader from '../loadThemes' | ||
|
||
const productionThemes = [ | ||
{ | ||
value: '../dist/main/css/themes/light.css', | ||
label: 'Light' | ||
}, | ||
{ | ||
value: '../dist/main/css/themes/dark.css', | ||
label: 'Dark' | ||
} | ||
] | ||
|
||
const developmentThemes = [ | ||
{ | ||
value: 'http://localhost:3000/dist/main/css/themes/light.css', | ||
label: 'Light' | ||
}, | ||
{ | ||
value: 'http://localhost:3000/dist/main/css/themes/dark.css', | ||
label: 'Dark' | ||
} | ||
] | ||
|
||
test('returns themes for production', () => { | ||
expect(themesLoader()).toEqual(productionThemes) | ||
}) | ||
|
||
test('returns themes for development', () => { | ||
process.env.NODE_ENV = 'development' | ||
expect(themesLoader()).toEqual(developmentThemes) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { send } from 'lib/rpc' | ||
import { settings as pluginSettings } from 'lib/plugins' | ||
|
||
/** | ||
* Initialices plugin sync and/or async by calling the `initialize` and `initializeAsync` functions | ||
* @param {Object} plugin A plugin object | ||
* @param {string} name The name entry in the plugin package.json | ||
*/ | ||
const initPlugin = (plugin, name) => { | ||
const { initialize, initializeAsync } = plugin | ||
|
||
// Foreground plugin initialization | ||
if (initialize) { | ||
console.log('Initialize sync plugin', name) | ||
try { | ||
initialize(pluginSettings.getUserSettings(plugin, name)) | ||
} catch (e) { | ||
console.error(`Failed to initialize plugin: ${name}`, e) | ||
} | ||
} | ||
|
||
// Background plugin initialization | ||
if (initializeAsync) { | ||
console.log('Initialize async plugin', name) | ||
send('initializePluginAsync', { name }) | ||
} | ||
} | ||
|
||
export default initPlugin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import getUserSettings from '../get' | ||
|
||
const plugin = { | ||
settings: { | ||
test_setting1: { | ||
type: 'string', | ||
defaultValue: 'test', | ||
}, | ||
test_setting2: { | ||
type: 'number', | ||
defaultValue: 1, | ||
}, | ||
} | ||
} | ||
|
||
describe('Test getUserSettings', () => { | ||
it('returns valid settings object', () => { | ||
expect(getUserSettings(plugin, 'test-plugin')) | ||
.toEqual({ test_setting1: 'test', test_setting2: 1 }) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import validate from '../validate' | ||
|
||
const validSettings = { | ||
option1: { | ||
description: 'Just a test description', | ||
type: 'option', | ||
options: ['option_1', 'option_2'], | ||
}, | ||
option2: { | ||
description: 'Just a test description', | ||
type: 'number', | ||
defaultValue: 0 | ||
}, | ||
option3: { | ||
description: 'Just a test description', | ||
type: 'number', | ||
defaultValue: 0 | ||
}, | ||
option4: { | ||
description: 'Just a test description', | ||
type: 'bool' | ||
}, | ||
option5: { | ||
description: 'Just a test description', | ||
type: 'string', | ||
defaultValue: 'test' | ||
} | ||
} | ||
|
||
const invalidSettingsNoOptionsProvided = { | ||
option1: { | ||
description: 'Just a test description', | ||
type: 'option', | ||
options: [], | ||
} | ||
} | ||
|
||
const invalidSettingsInvalidType = { | ||
option1: { | ||
description: 'Just a test description', | ||
type: 'test' | ||
} | ||
} | ||
|
||
describe('Validate settings function', () => { | ||
it('returns true when plugin has no settings field', () => { | ||
const plugin = { | ||
fn: () => {} | ||
} | ||
expect(validate(plugin)).toEqual(true) | ||
}) | ||
|
||
it('returns true when plugin has empty settings field', () => { | ||
const plugin = { | ||
fn: () => {}, | ||
settings: {} | ||
} | ||
expect(validate(plugin)).toEqual(true) | ||
}) | ||
|
||
it('returns true when plugin has valid settings', () => { | ||
const plugin = { | ||
fn: () => {}, | ||
settings: validSettings | ||
} | ||
expect(validate(plugin)).toEqual(true) | ||
}) | ||
|
||
it('returns false when option type is options and no options provided', () => { | ||
const plugin = { | ||
fn: () => {}, | ||
settings: invalidSettingsNoOptionsProvided | ||
} | ||
expect(validate(plugin)).toEqual(false) | ||
}) | ||
|
||
it('returns false when option type is incorrect', () => { | ||
const plugin = { | ||
fn: () => {}, | ||
settings: invalidSettingsInvalidType | ||
} | ||
expect(validate(plugin)).toEqual(false) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,34 @@ | ||
import config from 'lib/config' | ||
import plugins from 'plugins' | ||
|
||
const getSettings = pluginName => config.get('plugins')[pluginName] || {} | ||
/** | ||
* Returns the settings established by the user and previously saved in the config file | ||
* @param {string} pluginName The name entry of the plugin package.json | ||
* @returns An object with keys and values of the **stored** plugin settings | ||
*/ | ||
const getExistingSettings = (pluginName) => config.get('plugins')[pluginName] || {} | ||
|
||
const getUserSettings = (pluginName) => { | ||
const settings = getSettings(pluginName) | ||
/** | ||
* Returns the sum of the default settings and the user settings | ||
* We use packageJsonName to avoid conflicts with plugins that export | ||
* a different name from the bundle. Two plugins can export the same name | ||
* but can't have the same package.json name | ||
* @param {Object} plugin | ||
* @param {string} packageJsonName | ||
* @returns An object with keys and values of the plugin settings | ||
*/ | ||
const getUserSettings = (plugin, packageJsonName) => { | ||
const userSettings = {} | ||
const existingSettings = getExistingSettings(packageJsonName) | ||
const { settings: pluginSettings } = plugin | ||
|
||
if (plugins[pluginName].settings) { | ||
if (pluginSettings) { | ||
// Provide default values if nothing is set by user | ||
Object.keys(plugins[pluginName].settings).forEach((key) => { | ||
settings[key] = settings[key] || plugins[pluginName].settings[key].defaultValue | ||
Object.keys(pluginSettings).forEach((key) => { | ||
userSettings[key] = existingSettings[key] || pluginSettings[key].defaultValue | ||
}) | ||
} | ||
|
||
return settings | ||
return userSettings | ||
} | ||
|
||
export default getUserSettings |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
|
||
import { | ||
SET_STATUS_BAR_TEXT | ||
} from 'main/constants/actionTypes' | ||
|
||
import * as actions from '../statusBar' | ||
|
||
describe('reset', () => { | ||
it('returns valid action', () => { | ||
expect(actions.reset()).toEqual({ | ||
type: SET_STATUS_BAR_TEXT, | ||
payload: null | ||
}) | ||
}) | ||
}) | ||
|
||
describe('setValue', () => { | ||
it('returns valid action when value passed', () => { | ||
expect(actions.setValue('test value')).toEqual({ | ||
type: SET_STATUS_BAR_TEXT, | ||
payload: 'test value' | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.