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 osDialog = require('./os/dialog')
const exceptionReporter = require('./modules/exception-reporter') const exceptionReporter = require('./modules/exception-reporter')
const updateLock = require('./modules/update-lock') const updateLock = require('./modules/update-lock')
const debug = require('debug')('etcher:app')
/* eslint-disable lodash/prefer-lodash-method */ /* eslint-disable lodash/prefer-lodash-method */
@ -190,7 +191,28 @@ app.run(() => {
}) })
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()) { if (!flashState.isFlashing()) {
return return
} }
@ -214,6 +236,7 @@ app.run(() => {
windowProgress.set(currentFlashState) windowProgress.set(currentFlashState)
}) })
}) })
app.run(($timeout) => { app.run(($timeout) => {

View File

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

View File

@ -26,6 +26,7 @@ const localSettings = require('./local-settings')
const errors = require('../../../shared/errors') const errors = require('../../../shared/errors')
const release = require('../../../shared/release') const release = require('../../../shared/release')
const packageJSON = require('../../../../package.json') const packageJSON = require('../../../../package.json')
const debug = require('debug')('etcher:models:settings')
/** /**
* @summary Default settings * @summary Default settings
@ -47,8 +48,9 @@ const DEFAULT_SETTINGS = {
/** /**
* @summary Settings state * @summary Settings state
* @type {Object} * @type {Object}
* @private
*/ */
const settings = _.assign({}, DEFAULT_SETTINGS) let settings = _.cloneDeep(DEFAULT_SETTINGS)
/** /**
* @summary Reset settings to their default values * @summary Reset settings to their default values
@ -63,8 +65,10 @@ const settings = _.assign({}, DEFAULT_SETTINGS)
* }); * });
*/ */
exports.reset = () => { exports.reset = () => {
debug('reset')
// TODO: Remove default settings from config file (?) // 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!'); * console.log('Done!');
* }); * });
*/ */
exports.assign = (data) => { exports.assign = (value) => {
if (_.isNil(data)) { debug('assign', value)
if (_.isNil(value)) {
return Bluebird.reject(errors.createError({ return Bluebird.reject(errors.createError({
title: 'Missing settings' 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 = () => { 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) => { exports.set = (key, value) => {
debug('set', key, value)
if (_.isNil(key)) { if (_.isNil(key)) {
return Bluebird.reject(errors.createError({ return Bluebird.reject(errors.createError({
title: 'Missing setting key' title: 'Missing setting key'
@ -135,9 +156,9 @@ exports.set = (key, value) => {
})) }))
} }
return exports.assign({ settings[key] = value
[key]: value
}) return localSettings.writeAll(settings)
} }
/** /**
@ -152,6 +173,7 @@ exports.set = (key, value) => {
* const value = settings.get('unmountOnSuccess'); * const value = settings.get('unmountOnSuccess');
*/ */
exports.get = (key) => { exports.get = (key) => {
// debug('get', key)
return _.cloneDeep(_.get(settings, [ key ])) return _.cloneDeep(_.get(settings, [ key ]))
} }
@ -167,5 +189,22 @@ exports.get = (key) => {
* console.log(allSettings.unmountOnSuccess); * console.log(allSettings.unmountOnSuccess);
*/ */
exports.getAll = () => { exports.getAll = () => {
debug('getAll')
return _.cloneDeep(settings) 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) analytics.logEvent('Flash', analyticsData)
return exports.performWrite(image, drives, (state) => { return exports.performWrite(image, drives, (state) => {
console.log('performWriate.onProgress', state)
flashState.setProgressState(state) flashState.setProgressState(state)
}).then(flashState.unsetFlashingFlag).then(() => { }).then(flashState.unsetFlashingFlag).then(() => {
if (flashState.wasLastFlashCancelled()) { if (flashState.wasLastFlashCancelled()) {

View File

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

View File

@ -19,7 +19,6 @@
const m = require('mochainon') const m = require('mochainon')
const _ = require('lodash') const _ = require('lodash')
const Bluebird = require('bluebird') const Bluebird = require('bluebird')
const store = require('../../../lib/gui/app/models/store')
const settings = require('../../../lib/gui/app/models/settings') const settings = require('../../../lib/gui/app/models/settings')
const localSettings = require('../../../lib/gui/app/models/local-settings') const localSettings = require('../../../lib/gui/app/models/local-settings')
@ -28,7 +27,7 @@ describe('Browser: settings', function () {
return settings.reset() 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 () { it('should be able to set and read values', function () {
m.chai.expect(settings.get('foo')).to.be.undefined 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 () { it('should not override all settings', function () {
return settings.assign({ return settings.assign({
foo: 'bar', 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 () { it('should store the settings to the local machine', function () {
return localSettings.readAll().then((data) => { return localSettings.readAll().then((data) => {
m.chai.expect(data.foo).to.be.undefined 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) { 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).to.be.an.instanceof(Error)
m.chai.expect(error.message).to.equal('Invalid setting value: 1,2,3 for foo') m.chai.expect(error.message).to.equal('Invalid setting value: 1,2,3 for foo')
done() done()