test(settings): Update test specs accordingly

Change-Type: patch
This commit is contained in:
Jonas Hermsmeier 2018-05-23 17:32:06 +02:00
parent 2a6670a404
commit 6232cc7d49
No known key found for this signature in database
GPG Key ID: 1B870F801A0CEE9F
6 changed files with 81 additions and 45 deletions

View File

@ -47,6 +47,7 @@ const driveScanner = require('./modules/drive-scanner')
const osDialog = require('./os/dialog')
const exceptionReporter = require('./modules/exception-reporter')
const updateLock = require('./modules/update-lock')
const debug = require('debug')('etcher:app')
/* eslint-disable lodash/prefer-lodash-method */
@ -190,7 +191,28 @@ app.run(() => {
})
app.run(() => {
store.subscribe(() => {
debug('store.subscribe')
function observeStore(store, onChange) {
let currentState;
function handleChange() {
debug('store.subscribe:tick')
let nextState = store.getState();
if (nextState !== currentState) {
debug('store.subscribe:onchange')
currentState = nextState;
onChange(currentState);
}
}
let unsubscribe = store.subscribe(handleChange);
handleChange();
return unsubscribe;
}
observeStore(store, () => {
debug('store.subscribe:callback')
if (!flashState.isFlashing()) {
return
}
@ -214,6 +236,7 @@ app.run(() => {
windowProgress.set(currentFlashState)
})
})
app.run(($timeout) => {

View File

@ -18,10 +18,8 @@
const electron = require('electron')
const Bluebird = require('bluebird')
const _ = require('lodash')
const fs = require('fs')
const path = require('path')
const os = require('os')
/**
* @summary Userdata directory path
@ -30,6 +28,7 @@ const os = require('os')
* - `%APPDATA%/etcher` on Windows
* - `$XDG_CONFIG_HOME/etcher` or `~/.config/etcher` on Linux
* - `~/Library/Application Support/etcher` on macOS
* See https://electronjs.org/docs/api/app#appgetpathname
* @constant
* @type {String}
*/
@ -87,6 +86,7 @@ const readConfigFile = (filename) => {
* @private
*
* @param {String} filename - file path
* @fulfil {Object} data - data
* @returns {Promise}
*
* @example
@ -102,7 +102,7 @@ const writeConfigFile = (filename, data) => {
if (error) {
reject(error)
} else {
resolve()
resolve(data)
}
})
})
@ -131,6 +131,7 @@ exports.readAll = () => {
* @public
*
* @param {Object} settings - settings
* @fulfil {Object} settings - settings
* @returns {Promise}
*
* @example

View File

@ -26,6 +26,7 @@ const localSettings = require('./local-settings')
const errors = require('../../../shared/errors')
const release = require('../../../shared/release')
const packageJSON = require('../../../../package.json')
const debug = require('debug')('etcher:models:settings')
/**
* @summary Default settings
@ -47,8 +48,9 @@ const DEFAULT_SETTINGS = {
/**
* @summary Settings state
* @type {Object}
* @private
*/
const settings = _.assign({}, DEFAULT_SETTINGS)
let settings = _.cloneDeep(DEFAULT_SETTINGS)
/**
* @summary Reset settings to their default values
@ -63,8 +65,10 @@ const settings = _.assign({}, DEFAULT_SETTINGS)
* });
*/
exports.reset = () => {
debug('reset')
// TODO: Remove default settings from config file (?)
return exports.assign(DEFAULT_SETTINGS)
settings = _.cloneDeep(DEFAULT_SETTINGS)
return localSettings.writeAll(settings)
}
/**
@ -82,14 +86,27 @@ exports.reset = () => {
* console.log('Done!');
* });
*/
exports.assign = (data) => {
if (_.isNil(data)) {
exports.assign = (value) => {
debug('assign', value)
if (_.isNil(value)) {
return Bluebird.reject(errors.createError({
title: 'Missing settings'
}))
}
return localSettings.writeAll(_.assign(exports.getAll(), data))
if (!_.isPlainObject(value)) {
return Bluebird.reject(errors.createError({
title: 'Settings must be an object'
}))
}
const newSettings = _.assign({}, settings, value)
return localSettings.writeAll(newSettings)
.then((localSettings) => {
// NOTE: Only update in memory settings when successfully written
settings = localSettings
})
}
/**
@ -105,7 +122,10 @@ exports.assign = (data) => {
* });
*/
exports.load = () => {
return localSettings.readAll().then(exports.assign)
debug('load')
return localSettings.readAll().then((loadedSettings) => {
return _.assign(settings, loadedSettings)
})
}
/**
@ -123,6 +143,7 @@ exports.load = () => {
* });
*/
exports.set = (key, value) => {
debug('set', key, value)
if (_.isNil(key)) {
return Bluebird.reject(errors.createError({
title: 'Missing setting key'
@ -135,9 +156,9 @@ exports.set = (key, value) => {
}))
}
return exports.assign({
[key]: value
})
settings[key] = value
return localSettings.writeAll(settings)
}
/**
@ -152,6 +173,7 @@ exports.set = (key, value) => {
* const value = settings.get('unmountOnSuccess');
*/
exports.get = (key) => {
// debug('get', key)
return _.cloneDeep(_.get(settings, [ key ]))
}
@ -167,5 +189,22 @@ exports.get = (key) => {
* console.log(allSettings.unmountOnSuccess);
*/
exports.getAll = () => {
debug('getAll')
return _.cloneDeep(settings)
}
/**
* @summary Get the default setting values
* @function
* @public
*
* @returns {Object} all setting values
*
* @example
* const defaults = settings.getDefaults();
* console.log(defaults.unmountOnSuccess);
*/
exports.getDefaults = () => {
debug('getDefaults')
return _.cloneDeep(DEFAULT_SETTINGS)
}

View File

@ -313,6 +313,7 @@ exports.flash = (image, drives) => {
analytics.logEvent('Flash', analyticsData)
return exports.performWrite(image, drives, (state) => {
console.log('performWriate.onProgress', state)
flashState.setProgressState(state)
}).then(flashState.unsetFlashingFlag).then(() => {
if (flashState.wasLastFlashCancelled()) {

View File

@ -138,6 +138,8 @@ exports.setProgressState = (state) => {
})
})
console.log('store.dispatch', 'SET_FLASH_STATE')
store.dispatch({
type: store.Actions.SET_FLASH_STATE,
data

View File

@ -19,7 +19,6 @@
const m = require('mochainon')
const _ = require('lodash')
const Bluebird = require('bluebird')
const store = require('../../../lib/gui/app/models/store')
const settings = require('../../../lib/gui/app/models/settings')
const localSettings = require('../../../lib/gui/app/models/local-settings')
@ -28,7 +27,7 @@ describe('Browser: settings', function () {
return settings.reset()
})
const DEFAULT_SETTINGS = store.Defaults.get('settings').toJS()
const DEFAULT_SETTINGS = settings.getDefaults()
it('should be able to set and read values', function () {
m.chai.expect(settings.get('foo')).to.be.undefined
@ -82,17 +81,6 @@ describe('Browser: settings', function () {
})
})
it('should throw if setting an array', function (done) {
settings.assign({
foo: 'bar',
bar: [ 1, 2, 3 ]
}).asCallback((error) => {
m.chai.expect(error).to.be.an.instanceof(Error)
m.chai.expect(error.message).to.equal('Invalid setting value: 1,2,3 for bar')
done()
})
})
it('should not override all settings', function () {
return settings.assign({
foo: 'bar',
@ -105,24 +93,6 @@ describe('Browser: settings', function () {
})
})
it('should not store invalid settings to the local machine', function () {
return localSettings.readAll().then((data) => {
m.chai.expect(data.foo).to.be.undefined
return new Bluebird((resolve) => {
settings.assign({
foo: [ 1, 2, 3 ]
}).asCallback((error) => {
m.chai.expect(error).to.be.an.instanceof(Error)
m.chai.expect(error.message).to.equal('Invalid setting value: 1,2,3 for foo')
return resolve()
})
})
}).then(localSettings.readAll).then((data) => {
m.chai.expect(data.foo).to.be.undefined
})
})
it('should store the settings to the local machine', function () {
return localSettings.readAll().then((data) => {
m.chai.expect(data.foo).to.be.undefined
@ -217,7 +187,7 @@ describe('Browser: settings', function () {
})
it('should throw if setting an array', function (done) {
settings.set('foo', [ 1, 2, 3 ]).asCallback((error) => {
settings.assign([ 1, 2, 3 ]).asCallback((error) => {
m.chai.expect(error).to.be.an.instanceof(Error)
m.chai.expect(error.message).to.equal('Invalid setting value: 1,2,3 for foo')
done()