From 6232cc7d49781716f9d3d4b44d760ca5ed4d1fc0 Mon Sep 17 00:00:00 2001 From: Jonas Hermsmeier Date: Wed, 23 May 2018 17:32:06 +0200 Subject: [PATCH] test(settings): Update test specs accordingly Change-Type: patch --- lib/gui/app/app.js | 25 +++++++++++- lib/gui/app/models/local-settings.js | 7 ++-- lib/gui/app/models/settings.js | 57 +++++++++++++++++++++++----- lib/gui/app/modules/image-writer.js | 1 + lib/shared/models/flash-state.js | 2 + tests/gui/models/settings.spec.js | 34 +---------------- 6 files changed, 81 insertions(+), 45 deletions(-) diff --git a/lib/gui/app/app.js b/lib/gui/app/app.js index c56fa71f..2ce1802a 100644 --- a/lib/gui/app/app.js +++ b/lib/gui/app/app.js @@ -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) => { diff --git a/lib/gui/app/models/local-settings.js b/lib/gui/app/models/local-settings.js index 69c59a41..b7096c34 100644 --- a/lib/gui/app/models/local-settings.js +++ b/lib/gui/app/models/local-settings.js @@ -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 diff --git a/lib/gui/app/models/settings.js b/lib/gui/app/models/settings.js index 80616c9e..0ad76ecb 100644 --- a/lib/gui/app/models/settings.js +++ b/lib/gui/app/models/settings.js @@ -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) +} diff --git a/lib/gui/app/modules/image-writer.js b/lib/gui/app/modules/image-writer.js index c86cff34..d95bbc46 100644 --- a/lib/gui/app/modules/image-writer.js +++ b/lib/gui/app/modules/image-writer.js @@ -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()) { diff --git a/lib/shared/models/flash-state.js b/lib/shared/models/flash-state.js index 78630245..33966013 100644 --- a/lib/shared/models/flash-state.js +++ b/lib/shared/models/flash-state.js @@ -138,6 +138,8 @@ exports.setProgressState = (state) => { }) }) + console.log('store.dispatch', 'SET_FLASH_STATE') + store.dispatch({ type: store.Actions.SET_FLASH_STATE, data diff --git a/tests/gui/models/settings.spec.js b/tests/gui/models/settings.spec.js index 3fbfd272..7f10da11 100644 --- a/tests/gui/models/settings.spec.js +++ b/tests/gui/models/settings.spec.js @@ -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()