Merge pull request #234 from resin-io/refactor/selection-state-model

Convert SelectionStateService into a model
This commit is contained in:
Juan Cruz Viotti 2016-04-01 10:31:51 -04:00
commit f879245fce
5 changed files with 64 additions and 62 deletions

View File

@ -27,7 +27,7 @@ const dialog = electron.remote.require('./src/dialog');
require('angular-ui-bootstrap');
require('angular-ui-router');
require('./browser/modules/selection-state');
require('./browser/models/selection-state');
require('./browser/modules/drive-scanner');
require('./browser/modules/image-writer');
require('./browser/modules/path');
@ -46,7 +46,6 @@ const app = angular.module('Etcher', [
// Etcher modules
'Etcher.path',
'Etcher.selection-state',
'Etcher.drive-scanner',
'Etcher.image-writer',
'Etcher.notifier',
@ -55,6 +54,9 @@ const app = angular.module('Etcher', [
// Controllers
'Etcher.controllers.navigation',
// Models
'Etcher.Models.SelectionState',
// Components
'Etcher.Components.ProgressButton',
'Etcher.Components.DriveSelector',
@ -84,14 +86,14 @@ app.controller('AppController', function(
$scope,
NotifierService,
DriveScannerService,
SelectionStateService,
SelectionStateModel,
ImageWriterService,
AnalyticsService,
DriveSelectorService,
WindowProgressService
) {
let self = this;
this.selection = SelectionStateService;
this.selection = SelectionStateModel;
this.writer = ImageWriterService;
this.scanner = DriveScannerService;

View File

@ -17,14 +17,14 @@
'use strict';
/**
* @module Etcher.selection-state
* @module Etcher.Models.SelectionState
*/
const _ = require('lodash');
const angular = require('angular');
const selectionState = angular.module('Etcher.selection-state', []);
const SelectionStateModel = angular.module('Etcher.Models.SelectionState', []);
selectionState.service('SelectionStateService', function() {
SelectionStateModel.service('SelectionStateModel', function() {
let self = this;
/**
@ -42,7 +42,7 @@ selectionState.service('SelectionStateService', function() {
* @param {Object} drive - drive
*
* @example
* SelectionStateService.setDrive({
* SelectionStateModel.setDrive({
* device: '/dev/disk2'
* });
*/
@ -58,7 +58,7 @@ selectionState.service('SelectionStateService', function() {
* @param {String} image - image
*
* @example
* SelectionStateService.setImage('foo.img');
* SelectionStateModel.setImage('foo.img');
*/
this.setImage = function(image) {
selection.image = image;
@ -72,7 +72,7 @@ selectionState.service('SelectionStateService', function() {
* @returns {Object} drive
*
* @example
* const drive = SelectionStateService.getDrive();
* const drive = SelectionStateModel.getDrive();
*/
this.getDrive = function() {
return selection.drive;
@ -86,7 +86,7 @@ selectionState.service('SelectionStateService', function() {
* @returns {String} image
*
* @example
* const image = SelectionStateService.getImage();
* const image = SelectionStateModel.getImage();
*/
this.getImage = function() {
return selection.image;
@ -100,7 +100,7 @@ selectionState.service('SelectionStateService', function() {
* @returns {Boolean} whether there is a selected drive
*
* @example
* if (SelectionStateService.hasDrive()) {
* if (SelectionStateModel.hasDrive()) {
* console.log('There is a drive!');
* }
*/
@ -116,7 +116,7 @@ selectionState.service('SelectionStateService', function() {
* @returns {Boolean} whether there is a selected image
*
* @example
* if (SelectionStateService.hasImage()) {
* if (SelectionStateModel.hasImage()) {
* console.log('There is an image!');
* }
*/
@ -130,7 +130,7 @@ selectionState.service('SelectionStateService', function() {
* @public
*
* @example
* SelectionStateService.removeDrive();
* SelectionStateModel.removeDrive();
*/
this.removeDrive = _.partial(self.setDrive, undefined);
@ -140,7 +140,7 @@ selectionState.service('SelectionStateService', function() {
* @public
*
* @example
* SelectionStateService.removeImage();
* SelectionStateModel.removeImage();
*/
this.removeImage = _.partial(self.setImage, undefined);
@ -153,10 +153,10 @@ selectionState.service('SelectionStateService', function() {
* @param {Boolean} [options.preserveImage] - preserve image
*
* @example
* SelectionStateService.clear();
* SelectionStateModel.clear();
*
* @example
* SelectionStateService.clear({ preserveImage: true });
* SelectionStateModel.clear({ preserveImage: true });
*/
this.clear = function(options) {
if (options && options.preserveImage) {

View File

@ -16,7 +16,7 @@
'use strict';
module.exports = function($state, SelectionStateService, ImageWriterService, AnalyticsService, SettingsModel) {
module.exports = function($state, SelectionStateModel, ImageWriterService, AnalyticsService, SettingsModel) {
/**
* @summary Settings data
@ -37,7 +37,7 @@ module.exports = function($state, SelectionStateService, ImageWriterService, Ana
* FinishController.restart({ preserveImage: true });
*/
this.restart = function(options) {
SelectionStateService.clear(options);
SelectionStateModel.clear(options);
ImageWriterService.resetState();
AnalyticsService.logEvent('Restart', options);
$state.go('main');

View File

@ -28,16 +28,16 @@
const angular = require('angular');
require('angular-ui-router');
require('../../modules/selection-state');
require('../../modules/image-writer');
require('../../modules/analytics');
require('../../models/selection-state');
require('../../models/settings');
const FinishPage = angular.module('Etcher.Pages.Finish', [
'ui.router',
'Etcher.selection-state',
'Etcher.image-writer',
'Etcher.analytics',
'Etcher.Models.SelectionState',
'Etcher.Models.Settings'
]);

View File

@ -3,43 +3,43 @@
const m = require('mochainon');
const angular = require('angular');
require('angular-mocks');
require('../../../lib/browser/modules/selection-state');
require('../../../lib/browser/models/selection-state');
describe('Browser: SelectionState', function() {
beforeEach(angular.mock.module('Etcher.selection-state'));
beforeEach(angular.mock.module('Etcher.Models.SelectionState'));
describe('SelectionStateService', function() {
describe('SelectionStateModel', function() {
let SelectionStateService;
let SelectionStateModel;
beforeEach(angular.mock.inject(function(_SelectionStateService_) {
SelectionStateService = _SelectionStateService_;
beforeEach(angular.mock.inject(function(_SelectionStateModel_) {
SelectionStateModel = _SelectionStateModel_;
}));
describe('given a clean state', function() {
beforeEach(function() {
SelectionStateService.clear();
SelectionStateModel.clear();
});
it('getDrive() should return undefined', function() {
const drive = SelectionStateService.getDrive();
const drive = SelectionStateModel.getDrive();
m.chai.expect(drive).to.be.undefined;
});
it('getImage() should return undefined', function() {
const image = SelectionStateService.getImage();
const image = SelectionStateModel.getImage();
m.chai.expect(image).to.be.undefined;
});
it('hasDrive() should return false', function() {
const hasDrive = SelectionStateService.hasDrive();
const hasDrive = SelectionStateModel.hasDrive();
m.chai.expect(hasDrive).to.be.false;
});
it('hasImage() should return false', function() {
const hasImage = SelectionStateService.hasImage();
const hasImage = SelectionStateModel.hasImage();
m.chai.expect(hasImage).to.be.false;
});
@ -48,13 +48,13 @@ describe('Browser: SelectionState', function() {
describe('given a drive', function() {
beforeEach(function() {
SelectionStateService.setDrive('/dev/disk2');
SelectionStateModel.setDrive('/dev/disk2');
});
describe('.getDrive()', function() {
it('should return the drive', function() {
const drive = SelectionStateService.getDrive();
const drive = SelectionStateModel.getDrive();
m.chai.expect(drive).to.equal('/dev/disk2');
});
@ -63,7 +63,7 @@ describe('Browser: SelectionState', function() {
describe('.hasDrive()', function() {
it('should return true', function() {
const hasDrive = SelectionStateService.hasDrive();
const hasDrive = SelectionStateModel.hasDrive();
m.chai.expect(hasDrive).to.be.true;
});
@ -72,8 +72,8 @@ describe('Browser: SelectionState', function() {
describe('.setDrive()', function() {
it('should override the drive', function() {
SelectionStateService.setDrive('/dev/disk5');
const drive = SelectionStateService.getDrive();
SelectionStateModel.setDrive('/dev/disk5');
const drive = SelectionStateModel.getDrive();
m.chai.expect(drive).to.equal('/dev/disk5');
});
@ -82,8 +82,8 @@ describe('Browser: SelectionState', function() {
describe('.removeDrive()', function() {
it('should clear the drive', function() {
SelectionStateService.removeDrive();
const drive = SelectionStateService.getDrive();
SelectionStateModel.removeDrive();
const drive = SelectionStateModel.getDrive();
m.chai.expect(drive).to.be.undefined;
});
@ -96,8 +96,8 @@ describe('Browser: SelectionState', function() {
describe('.setDrive()', function() {
it('should be able to set a drive', function() {
SelectionStateService.setDrive('/dev/disk5');
const drive = SelectionStateService.getDrive();
SelectionStateModel.setDrive('/dev/disk5');
const drive = SelectionStateModel.getDrive();
m.chai.expect(drive).to.equal('/dev/disk5');
});
@ -108,13 +108,13 @@ describe('Browser: SelectionState', function() {
describe('given an image', function() {
beforeEach(function() {
SelectionStateService.setImage('foo.img');
SelectionStateModel.setImage('foo.img');
});
describe('.getImage()', function() {
it('should return the image', function() {
const image = SelectionStateService.getImage();
const image = SelectionStateModel.getImage();
m.chai.expect(image).to.equal('foo.img');
});
@ -123,7 +123,7 @@ describe('Browser: SelectionState', function() {
describe('.hasImage()', function() {
it('should return true', function() {
const hasImage = SelectionStateService.hasImage();
const hasImage = SelectionStateModel.hasImage();
m.chai.expect(hasImage).to.be.true;
});
@ -132,8 +132,8 @@ describe('Browser: SelectionState', function() {
describe('.setImage()', function() {
it('should override the image', function() {
SelectionStateService.setImage('bar.img');
const image = SelectionStateService.getImage();
SelectionStateModel.setImage('bar.img');
const image = SelectionStateModel.getImage();
m.chai.expect(image).to.equal('bar.img');
});
@ -142,8 +142,8 @@ describe('Browser: SelectionState', function() {
describe('.removeImage()', function() {
it('should clear the image', function() {
SelectionStateService.removeImage();
const image = SelectionStateService.getImage();
SelectionStateModel.removeImage();
const image = SelectionStateModel.getImage();
m.chai.expect(image).to.be.undefined;
});
@ -156,8 +156,8 @@ describe('Browser: SelectionState', function() {
describe('.setImage()', function() {
it('should be able to set an image', function() {
SelectionStateService.setImage('foo.img');
const image = SelectionStateService.getImage();
SelectionStateModel.setImage('foo.img');
const image = SelectionStateModel.getImage();
m.chai.expect(image).to.equal('foo.img');
});
@ -168,20 +168,20 @@ describe('Browser: SelectionState', function() {
describe('given a drive', function() {
beforeEach(function() {
SelectionStateService.setDrive('/dev/disk2');
SelectionStateService.setImage('foo.img');
SelectionStateModel.setDrive('/dev/disk2');
SelectionStateModel.setImage('foo.img');
});
describe('.clear()', function() {
it('should clear all selections', function() {
m.chai.expect(SelectionStateService.hasDrive()).to.be.true;
m.chai.expect(SelectionStateService.hasImage()).to.be.true;
m.chai.expect(SelectionStateModel.hasDrive()).to.be.true;
m.chai.expect(SelectionStateModel.hasImage()).to.be.true;
SelectionStateService.clear();
SelectionStateModel.clear();
m.chai.expect(SelectionStateService.hasDrive()).to.be.false;
m.chai.expect(SelectionStateService.hasImage()).to.be.false;
m.chai.expect(SelectionStateModel.hasDrive()).to.be.false;
m.chai.expect(SelectionStateModel.hasImage()).to.be.false;
});
});
@ -189,28 +189,28 @@ describe('Browser: SelectionState', function() {
describe('given the preserveImage option', function() {
beforeEach(function() {
SelectionStateService.clear({
SelectionStateModel.clear({
preserveImage: true
});
});
it('getDrive() should return undefined', function() {
const drive = SelectionStateService.getDrive();
const drive = SelectionStateModel.getDrive();
m.chai.expect(drive).to.be.undefined;
});
it('getImage() should return the image', function() {
const image = SelectionStateService.getImage();
const image = SelectionStateModel.getImage();
m.chai.expect(image).to.equal('foo.img');
});
it('hasDrive() should return false', function() {
const hasDrive = SelectionStateService.hasDrive();
const hasDrive = SelectionStateModel.hasDrive();
m.chai.expect(hasDrive).to.be.false;
});
it('hasImage() should return true', function() {
const hasImage = SelectionStateService.hasImage();
const hasImage = SelectionStateModel.hasImage();
m.chai.expect(hasImage).to.be.true;
});