refactor(GUI): remove angular dependency from selection-state (#1509)

We remove the dependency on Angular from SelectionStateModel and rename
it to selectionState.
This commit is contained in:
Benedict Aas 2017-06-12 13:40:52 +01:00 committed by Juan Cruz Viotti
parent cff445b64b
commit ef739fb222
16 changed files with 1235 additions and 1284 deletions

View File

@ -41,6 +41,7 @@ const windowProgress = require('./os/window-progress');
const analytics = require('./modules/analytics'); const analytics = require('./modules/analytics');
const updateNotifier = require('./components/update-notifier'); const updateNotifier = require('./components/update-notifier');
const availableDrives = require('./models/available-drives'); const availableDrives = require('./models/available-drives');
const selectionState = require('./models/selection-state');
const Store = require('./models/store'); const Store = require('./models/store');
@ -53,9 +54,6 @@ const app = angular.module('Etcher', [
require('./modules/error'), require('./modules/error'),
require('./modules/drive-scanner'), require('./modules/drive-scanner'),
// Models
require('./models/selection-state'),
// Components // Components
require('./components/svg-icon/svg-icon'), require('./components/svg-icon/svg-icon'),
require('./components/warning-modal/warning-modal'), require('./components/warning-modal/warning-modal'),
@ -89,7 +87,7 @@ app.run(() => {
].join('\n')); ].join('\n'));
}); });
app.run((ErrorService, SelectionStateModel) => { app.run((ErrorService) => {
analytics.logEvent('Application start'); analytics.logEvent('Application start');
const currentVersion = packageJSON.version; const currentVersion = packageJSON.version;
@ -139,7 +137,7 @@ app.run((ErrorService, SelectionStateModel) => {
// the user about the new version if he didn't start the flash // the user about the new version if he didn't start the flash
// process (e.g: selected an image), otherwise such interruption // process (e.g: selected an image), otherwise such interruption
// might be annoying. // might be annoying.
if (SelectionStateModel.hasImage()) { if (selectionState.hasImage()) {
analytics.logEvent('Update notification skipped', { analytics.logEvent('Update notification skipped', {
reason: 'Image selected' reason: 'Image selected'
}); });
@ -278,7 +276,7 @@ app.config(($provide) => {
}); });
}); });
app.controller('HeaderController', function(SelectionStateModel, OSOpenExternalService) { app.controller('HeaderController', function(OSOpenExternalService) {
/** /**
* @summary Open help page * @summary Open help page
@ -294,7 +292,7 @@ app.controller('HeaderController', function(SelectionStateModel, OSOpenExternalS
*/ */
this.openHelpPage = () => { this.openHelpPage = () => {
const DEFAULT_SUPPORT_URL = 'https://github.com/resin-io/etcher/blob/master/SUPPORT.md'; const DEFAULT_SUPPORT_URL = 'https://github.com/resin-io/etcher/blob/master/SUPPORT.md';
const supportUrl = SelectionStateModel.getImageSupportUrl() || DEFAULT_SUPPORT_URL; const supportUrl = selectionState.getImageSupportUrl() || DEFAULT_SUPPORT_URL;
OSOpenExternalService.open(supportUrl); OSOpenExternalService.open(supportUrl);
}; };

View File

@ -22,11 +22,11 @@ const messages = require('../../../../shared/messages');
const constraints = require('../../../../shared/drive-constraints'); const constraints = require('../../../../shared/drive-constraints');
const analytics = require('../../../modules/analytics'); const analytics = require('../../../modules/analytics');
const availableDrives = require('../../../models/available-drives'); const availableDrives = require('../../../models/available-drives');
const selectionState = require('../../../models/selection-state');
module.exports = function( module.exports = function(
$q, $q,
$uibModalInstance, $uibModalInstance,
SelectionStateModel,
WarningModalService WarningModalService
) { ) {
@ -35,7 +35,7 @@ module.exports = function(
* @type {Object} * @type {Object}
* @public * @public
*/ */
this.state = SelectionStateModel; this.state = selectionState;
/** /**
* @summary Static methods to check a drive's properties * @summary Static methods to check a drive's properties
@ -72,11 +72,11 @@ module.exports = function(
* }); * });
*/ */
const shouldChangeDriveSelectionState = (drive) => { const shouldChangeDriveSelectionState = (drive) => {
if (!constraints.isDriveValid(drive, SelectionStateModel.getImage())) { if (!constraints.isDriveValid(drive, selectionState.getImage())) {
return $q.resolve(false); return $q.resolve(false);
} }
if (constraints.isDriveSizeRecommended(drive, SelectionStateModel.getImage())) { if (constraints.isDriveSizeRecommended(drive, selectionState.getImage())) {
return $q.resolve(true); return $q.resolve(true);
} }
@ -84,7 +84,7 @@ module.exports = function(
confirmationLabel: 'Yes, continue', confirmationLabel: 'Yes, continue',
description: [ description: [
messages.warning.unrecommendedDriveSize({ messages.warning.unrecommendedDriveSize({
image: SelectionStateModel.getImage(), image: selectionState.getImage(),
drive drive
}), }),
'Are you sure you want to continue?' 'Are you sure you want to continue?'
@ -111,12 +111,12 @@ module.exports = function(
analytics.logEvent('Toggle drive', { analytics.logEvent('Toggle drive', {
drive, drive,
previouslySelected: SelectionStateModel.isCurrentDrive(drive.device) previouslySelected: selectionState.isCurrentDrive(drive.device)
}); });
return shouldChangeDriveSelectionState(drive).then((canChangeDriveSelectionState) => { return shouldChangeDriveSelectionState(drive).then((canChangeDriveSelectionState) => {
if (canChangeDriveSelectionState) { if (canChangeDriveSelectionState) {
SelectionStateModel.toggleSetDrive(drive.device); selectionState.toggleSetDrive(drive.device);
} }
}); });
@ -131,7 +131,7 @@ module.exports = function(
* DriveSelectorController.closeModal(); * DriveSelectorController.closeModal();
*/ */
this.closeModal = () => { this.closeModal = () => {
const selectedDrive = SelectionStateModel.getDrive(); const selectedDrive = selectionState.getDrive();
// Sanity check to cover the case where a drive is selected, // Sanity check to cover the case where a drive is selected,
// the drive is then unplugged from the computer and the modal // the drive is then unplugged from the computer and the modal
@ -162,7 +162,7 @@ module.exports = function(
this.selectDriveAndClose = (drive) => { this.selectDriveAndClose = (drive) => {
return shouldChangeDriveSelectionState(drive).then((canChangeDriveSelectionState) => { return shouldChangeDriveSelectionState(drive).then((canChangeDriveSelectionState) => {
if (canChangeDriveSelectionState) { if (canChangeDriveSelectionState) {
SelectionStateModel.setDrive(drive.device); selectionState.setDrive(drive.device);
analytics.logEvent('Drive selected (double click)'); analytics.logEvent('Drive selected (double click)');

View File

@ -25,7 +25,6 @@ const MODULE_NAME = 'Etcher.Components.DriveSelector';
const DriveSelector = angular.module(MODULE_NAME, [ const DriveSelector = angular.module(MODULE_NAME, [
require('../modal/modal'), require('../modal/modal'),
require('../warning-modal/warning-modal'), require('../warning-modal/warning-modal'),
require('../../models/selection-state'),
require('../../utils/byte-size/byte-size') require('../../utils/byte-size/byte-size')
]); ]);

View File

@ -23,8 +23,7 @@
const angular = require('angular'); const angular = require('angular');
const MODULE_NAME = 'Etcher.Components.FlashErrorModal'; const MODULE_NAME = 'Etcher.Components.FlashErrorModal';
const FlashErrorModal = angular.module(MODULE_NAME, [ const FlashErrorModal = angular.module(MODULE_NAME, [
require('../warning-modal/warning-modal'), require('../warning-modal/warning-modal')
require('../../models/selection-state')
]); ]);
FlashErrorModal.service('FlashErrorModalService', require('./services/flash-error-modal')); FlashErrorModal.service('FlashErrorModalService', require('./services/flash-error-modal'));

View File

@ -17,9 +17,10 @@
'use strict'; 'use strict';
const flashState = require('../../../models/flash-state'); const flashState = require('../../../models/flash-state');
const selectionState = require('../../../models/selection-state');
const analytics = require('../../../modules/analytics'); const analytics = require('../../../modules/analytics');
module.exports = function(WarningModalService, SelectionStateModel) { module.exports = function(WarningModalService) {
/** /**
* @summary Open the flash error modal * @summary Open the flash error modal
@ -42,7 +43,7 @@ module.exports = function(WarningModalService, SelectionStateModel) {
if (confirmed) { if (confirmed) {
analytics.logEvent('Restart after failure'); analytics.logEvent('Restart after failure');
} else { } else {
SelectionStateModel.clear(); selectionState.clear();
} }
}); });
}; };

View File

@ -16,18 +16,9 @@
'use strict'; 'use strict';
/**
* @module Etcher.Models.SelectionState
*/
const _ = require('lodash'); const _ = require('lodash');
const angular = require('angular');
const Store = require('./store'); const Store = require('./store');
const availableDrives = require('./available-drives'); const availableDrives = require('./available-drives');
const MODULE_NAME = 'Etcher.Models.SelectionState';
const SelectionStateModel = angular.module(MODULE_NAME, []);
SelectionStateModel.service('SelectionStateModel', function() {
/** /**
* @summary Set a drive * @summary Set a drive
@ -37,9 +28,9 @@ SelectionStateModel.service('SelectionStateModel', function() {
* @param {String} drive - drive device * @param {String} drive - drive device
* *
* @example * @example
* SelectionStateModel.setDrive('/dev/disk2'); * selectionState.setDrive('/dev/disk2');
*/ */
this.setDrive = (drive) => { exports.setDrive = (drive) => {
Store.dispatch({ Store.dispatch({
type: Store.Actions.SELECT_DRIVE, type: Store.Actions.SELECT_DRIVE,
data: drive data: drive
@ -54,13 +45,13 @@ SelectionStateModel.service('SelectionStateModel', function() {
* @param {String} drive - drive device * @param {String} drive - drive device
* *
* @example * @example
* SelectionStateModel.toggleSetDrive('/dev/disk2'); * selectionState.toggleSetDrive('/dev/disk2');
*/ */
this.toggleSetDrive = (drive) => { exports.toggleSetDrive = (drive) => {
if (this.isCurrentDrive(drive)) { if (exports.isCurrentDrive(drive)) {
this.removeDrive(); exports.removeDrive();
} else { } else {
this.setDrive(drive); exports.setDrive(drive);
} }
}; };
@ -72,11 +63,11 @@ SelectionStateModel.service('SelectionStateModel', function() {
* @param {Object} image - image * @param {Object} image - image
* *
* @example * @example
* SelectionStateModel.setImage({ * selectionState.setImage({
* path: 'foo.img' * path: 'foo.img'
* }); * });
*/ */
this.setImage = (image) => { exports.setImage = (image) => {
Store.dispatch({ Store.dispatch({
type: Store.Actions.SELECT_IMAGE, type: Store.Actions.SELECT_IMAGE,
data: image data: image
@ -91,9 +82,9 @@ SelectionStateModel.service('SelectionStateModel', function() {
* @returns {Object} drive * @returns {Object} drive
* *
* @example * @example
* const drive = SelectionStateModel.getDrive(); * const drive = selectionState.getDrive();
*/ */
this.getDrive = () => { exports.getDrive = () => {
return _.find(availableDrives.getDrives(), { return _.find(availableDrives.getDrives(), {
device: Store.getState().getIn([ 'selection', 'drive' ]) device: Store.getState().getIn([ 'selection', 'drive' ])
}); });
@ -107,9 +98,9 @@ SelectionStateModel.service('SelectionStateModel', function() {
* @returns {Object} image * @returns {Object} image
* *
* @example * @example
* const image = SelectionStateModel.getImage(); * const image = selectionState.getImage();
*/ */
this.getImage = () => { exports.getImage = () => {
return _.get(Store.getState().toJS(), [ 'selection', 'image' ]); return _.get(Store.getState().toJS(), [ 'selection', 'image' ]);
}; };
@ -121,9 +112,9 @@ SelectionStateModel.service('SelectionStateModel', function() {
* @returns {String} image path * @returns {String} image path
* *
* @example * @example
* const imagePath = SelectionStateModel.getImagePath(); * const imagePath = selectionState.getImagePath();
*/ */
this.getImagePath = () => { exports.getImagePath = () => {
return _.get(Store.getState().toJS(), [ return _.get(Store.getState().toJS(), [
'selection', 'selection',
'image', 'image',
@ -139,9 +130,9 @@ SelectionStateModel.service('SelectionStateModel', function() {
* @returns {Number} image size * @returns {Number} image size
* *
* @example * @example
* const imageSize = SelectionStateModel.getImageSize(); * const imageSize = selectionState.getImageSize();
*/ */
this.getImageSize = () => { exports.getImageSize = () => {
return _.get(Store.getState().toJS(), [ return _.get(Store.getState().toJS(), [
'selection', 'selection',
'image', 'image',
@ -159,9 +150,9 @@ SelectionStateModel.service('SelectionStateModel', function() {
* @returns {String} image url * @returns {String} image url
* *
* @example * @example
* const imageUrl = SelectionStateModel.getImageUrl(); * const imageUrl = selectionState.getImageUrl();
*/ */
this.getImageUrl = () => { exports.getImageUrl = () => {
return _.get(Store.getState().toJS(), [ return _.get(Store.getState().toJS(), [
'selection', 'selection',
'image', 'image',
@ -177,9 +168,9 @@ SelectionStateModel.service('SelectionStateModel', function() {
* @returns {String} image name * @returns {String} image name
* *
* @example * @example
* const imageName = SelectionStateModel.getImageName(); * const imageName = selectionState.getImageName();
*/ */
this.getImageName = () => { exports.getImageName = () => {
return _.get(Store.getState().toJS(), [ return _.get(Store.getState().toJS(), [
'selection', 'selection',
'image', 'image',
@ -195,9 +186,9 @@ SelectionStateModel.service('SelectionStateModel', function() {
* @returns {String} image logo * @returns {String} image logo
* *
* @example * @example
* const imageLogo = SelectionStateModel.getImageLogo(); * const imageLogo = selectionState.getImageLogo();
*/ */
this.getImageLogo = () => { exports.getImageLogo = () => {
return _.get(Store.getState().toJS(), [ return _.get(Store.getState().toJS(), [
'selection', 'selection',
'image', 'image',
@ -213,9 +204,9 @@ SelectionStateModel.service('SelectionStateModel', function() {
* @returns {String} image support url * @returns {String} image support url
* *
* @example * @example
* const imageSupportUrl = SelectionStateModel.getImageSupportUrl(); * const imageSupportUrl = selectionState.getImageSupportUrl();
*/ */
this.getImageSupportUrl = () => { exports.getImageSupportUrl = () => {
return _.get(Store.getState().toJS(), [ return _.get(Store.getState().toJS(), [
'selection', 'selection',
'image', 'image',
@ -231,9 +222,9 @@ SelectionStateModel.service('SelectionStateModel', function() {
* @returns {String} image recommended drive size * @returns {String} image recommended drive size
* *
* @example * @example
* const imageRecommendedDriveSize = SelectionStateModel.getImageRecommendedDriveSize(); * const imageRecommendedDriveSize = selectionState.getImageRecommendedDriveSize();
*/ */
this.getImageRecommendedDriveSize = () => { exports.getImageRecommendedDriveSize = () => {
return _.get(Store.getState().toJS(), [ return _.get(Store.getState().toJS(), [
'selection', 'selection',
'image', 'image',
@ -249,12 +240,12 @@ SelectionStateModel.service('SelectionStateModel', function() {
* @returns {Boolean} whether there is a selected drive * @returns {Boolean} whether there is a selected drive
* *
* @example * @example
* if (SelectionStateModel.hasDrive()) { * if (selectionState.hasDrive()) {
* console.log('There is a drive!'); * console.log('There is a drive!');
* } * }
*/ */
this.hasDrive = () => { exports.hasDrive = () => {
return Boolean(this.getDrive()); return Boolean(exports.getDrive());
}; };
/** /**
@ -265,12 +256,12 @@ SelectionStateModel.service('SelectionStateModel', function() {
* @returns {Boolean} whether there is a selected image * @returns {Boolean} whether there is a selected image
* *
* @example * @example
* if (SelectionStateModel.hasImage()) { * if (selectionState.hasImage()) {
* console.log('There is an image!'); * console.log('There is an image!');
* } * }
*/ */
this.hasImage = () => { exports.hasImage = () => {
return Boolean(this.getImage()); return Boolean(exports.getImage());
}; };
/** /**
@ -279,9 +270,9 @@ SelectionStateModel.service('SelectionStateModel', function() {
* @public * @public
* *
* @example * @example
* SelectionStateModel.removeDrive(); * selectionState.removeDrive();
*/ */
this.removeDrive = () => { exports.removeDrive = () => {
Store.dispatch({ Store.dispatch({
type: Store.Actions.REMOVE_DRIVE type: Store.Actions.REMOVE_DRIVE
}); });
@ -293,9 +284,9 @@ SelectionStateModel.service('SelectionStateModel', function() {
* @public * @public
* *
* @example * @example
* SelectionStateModel.removeImage(); * selectionState.removeImage();
*/ */
this.removeImage = () => { exports.removeImage = () => {
Store.dispatch({ Store.dispatch({
type: Store.Actions.REMOVE_IMAGE type: Store.Actions.REMOVE_IMAGE
}); });
@ -310,12 +301,12 @@ SelectionStateModel.service('SelectionStateModel', function() {
* @param {Boolean} [options.preserveImage] - preserve image * @param {Boolean} [options.preserveImage] - preserve image
* *
* @example * @example
* SelectionStateModel.clear(); * selectionState.clear();
* *
* @example * @example
* SelectionStateModel.clear({ preserveImage: true }); * selectionState.clear({ preserveImage: true });
*/ */
this.clear = (options = {}) => { exports.clear = (options = {}) => {
if (!options.preserveImage) { if (!options.preserveImage) {
Store.dispatch({ Store.dispatch({
type: Store.Actions.REMOVE_IMAGE type: Store.Actions.REMOVE_IMAGE
@ -336,18 +327,14 @@ SelectionStateModel.service('SelectionStateModel', function() {
* @returns {Boolean} whether the drive is the current drive * @returns {Boolean} whether the drive is the current drive
* *
* @example * @example
* if (SelectionStateModel.isCurrentDrive('/dev/sdb')) { * if (selectionState.isCurrentDrive('/dev/sdb')) {
* console.log('This is the current drive!'); * console.log('This is the current drive!');
* } * }
*/ */
this.isCurrentDrive = (drive) => { exports.isCurrentDrive = (drive) => {
if (!drive) { if (!drive) {
return false; return false;
} }
return drive === _.get(this.getDrive(), [ 'device' ]); return drive === _.get(exports.getDrive(), [ 'device' ]);
}; };
});
module.exports = MODULE_NAME;

View File

@ -18,9 +18,10 @@
const settings = require('../../../models/settings'); const settings = require('../../../models/settings');
const flashState = require('../../../models/flash-state'); const flashState = require('../../../models/flash-state');
const selectionState = require('../../../models/selection-state');
const analytics = require('../../../modules/analytics'); const analytics = require('../../../modules/analytics');
module.exports = function($state, SelectionStateModel) { module.exports = function($state) {
/** /**
* @summary Settings model * @summary Settings model
@ -48,7 +49,7 @@ module.exports = function($state, SelectionStateModel) {
* FinishController.restart({ preserveImage: true }); * FinishController.restart({ preserveImage: true });
*/ */
this.restart = (options) => { this.restart = (options) => {
SelectionStateModel.clear(options); selectionState.clear(options);
analytics.logEvent('Restart', options); analytics.logEvent('Restart', options);
$state.go('main'); $state.go('main');
}; };

View File

@ -29,8 +29,7 @@
const angular = require('angular'); const angular = require('angular');
const MODULE_NAME = 'Etcher.Pages.Finish'; const MODULE_NAME = 'Etcher.Pages.Finish';
const FinishPage = angular.module(MODULE_NAME, [ const FinishPage = angular.module(MODULE_NAME, [
require('angular-ui-router'), require('angular-ui-router')
require('../../models/selection-state')
]); ]);
FinishPage.controller('FinishController', require('./controllers/finish')); FinishPage.controller('FinishController', require('./controllers/finish'));

View File

@ -17,9 +17,10 @@
'use strict'; 'use strict';
const settings = require('../../../models/settings'); const settings = require('../../../models/settings');
const selectionState = require('../../../models/selection-state');
const analytics = require('../../../modules/analytics'); const analytics = require('../../../modules/analytics');
module.exports = function(SelectionStateModel, ErrorService, DriveSelectorService) { module.exports = function(ErrorService, DriveSelectorService) {
/** /**
* @summary Open drive selector * @summary Open drive selector
@ -35,7 +36,7 @@ module.exports = function(SelectionStateModel, ErrorService, DriveSelectorServic
return; return;
} }
SelectionStateModel.setDrive(drive.device); selectionState.setDrive(drive.device);
analytics.logEvent('Select drive', { analytics.logEvent('Select drive', {
device: drive.device, device: drive.device,

View File

@ -24,9 +24,9 @@ const errors = require('../../../../shared/errors');
const imageStream = require('../../../../image-stream'); const imageStream = require('../../../../image-stream');
const supportedFormats = require('../../../../shared/supported-formats'); const supportedFormats = require('../../../../shared/supported-formats');
const analytics = require('../../../modules/analytics'); const analytics = require('../../../modules/analytics');
const selectionState = require('../../../models/selection-state');
module.exports = function( module.exports = function(
SelectionStateModel,
ErrorService, ErrorService,
OSDialogService, OSDialogService,
WarningModalService WarningModalService
@ -100,7 +100,7 @@ module.exports = function(
return this.reselectImage(); return this.reselectImage();
} }
SelectionStateModel.setImage(image); selectionState.setImage(image);
// An easy way so we can quickly identify if we're making use of // An easy way so we can quickly identify if we're making use of
// certain features without printing pages of text to DevTools. // certain features without printing pages of text to DevTools.
@ -161,7 +161,7 @@ module.exports = function(
*/ */
this.reselectImage = () => { this.reselectImage = () => {
analytics.logEvent('Reselect image', { analytics.logEvent('Reselect image', {
previousImage: SelectionStateModel.getImage() previousImage: selectionState.getImage()
}); });
this.openImageSelector(); this.openImageSelector();
@ -178,11 +178,11 @@ module.exports = function(
* const imageBasename = ImageSelectionController.getImageBasename(); * const imageBasename = ImageSelectionController.getImageBasename();
*/ */
this.getImageBasename = () => { this.getImageBasename = () => {
if (!SelectionStateModel.hasImage()) { if (!selectionState.hasImage()) {
return ''; return '';
} }
return path.basename(SelectionStateModel.getImagePath()); return path.basename(selectionState.getImagePath());
}; };
}; };

View File

@ -20,16 +20,16 @@ const settings = require('../../../models/settings');
const flashState = require('../../../models/flash-state'); const flashState = require('../../../models/flash-state');
const analytics = require('../../../modules/analytics'); const analytics = require('../../../modules/analytics');
const availableDrives = require('../../../models/available-drives'); const availableDrives = require('../../../models/available-drives');
const selectionState = require('../../../models/selection-state');
module.exports = function( module.exports = function(
SelectionStateModel,
TooltipModalService, TooltipModalService,
ErrorService, ErrorService,
OSOpenExternalService OSOpenExternalService
) { ) {
// Expose several modules to the template for convenience // Expose several modules to the template for convenience
this.selection = SelectionStateModel; this.selection = selectionState;
this.drives = availableDrives; this.drives = availableDrives;
this.state = flashState; this.state = flashState;
this.settings = settings; this.settings = settings;
@ -48,7 +48,7 @@ module.exports = function(
* } * }
*/ */
this.shouldDriveStepBeDisabled = () => { this.shouldDriveStepBeDisabled = () => {
return !SelectionStateModel.hasImage(); return !selectionState.hasImage();
}; };
/** /**
@ -64,7 +64,7 @@ module.exports = function(
* } * }
*/ */
this.shouldFlashStepBeDisabled = () => { this.shouldFlashStepBeDisabled = () => {
return !SelectionStateModel.hasDrive() || this.shouldDriveStepBeDisabled(); return !selectionState.hasDrive() || this.shouldDriveStepBeDisabled();
}; };
/** /**
@ -79,12 +79,12 @@ module.exports = function(
*/ */
this.showSelectedImageDetails = () => { this.showSelectedImageDetails = () => {
analytics.logEvent('Show selected image tooltip', { analytics.logEvent('Show selected image tooltip', {
imagePath: SelectionStateModel.getImagePath() imagePath: selectionState.getImagePath()
}); });
return TooltipModalService.show({ return TooltipModalService.show({
title: 'Image File Name', title: 'Image File Name',
message: SelectionStateModel.getImagePath() message: selectionState.getImagePath()
}).catch(ErrorService.reportException); }).catch(ErrorService.reportException);
}; };

View File

@ -46,7 +46,6 @@ const MainPage = angular.module(MODULE_NAME, [
require('../../modules/drive-scanner'), require('../../modules/drive-scanner'),
require('../../modules/image-writer'), require('../../modules/image-writer'),
require('../../modules/error'), require('../../modules/error'),
require('../../models/selection-state'),
require('../../utils/byte-size/byte-size') require('../../utils/byte-size/byte-size')
]); ]);

View File

@ -17,7 +17,6 @@ describe('Browser: DriveSelector', function() {
let $rootScope; let $rootScope;
let $q; let $q;
let $uibModalInstance; let $uibModalInstance;
let SelectionStateModel;
let WarningModalService; let WarningModalService;
let controller; let controller;
@ -26,14 +25,12 @@ describe('Browser: DriveSelector', function() {
_$controller_, _$controller_,
_$rootScope_, _$rootScope_,
_$q_, _$q_,
_SelectionStateModel_,
_WarningModalService_ _WarningModalService_
) { ) {
$controller = _$controller_; $controller = _$controller_;
$rootScope = _$rootScope_; $rootScope = _$rootScope_;
$q = _$q_; $q = _$q_;
$uibModalInstance = {}; $uibModalInstance = {};
SelectionStateModel = _SelectionStateModel_;
WarningModalService = _WarningModalService_; WarningModalService = _WarningModalService_;
})); }));
@ -42,7 +39,6 @@ describe('Browser: DriveSelector', function() {
$scope: $rootScope.$new(), $scope: $rootScope.$new(),
$q, $q,
$uibModalInstance, $uibModalInstance,
SelectionStateModel,
WarningModalService WarningModalService
}); });
}); });

View File

@ -2,24 +2,13 @@
const m = require('mochainon'); const m = require('mochainon');
const path = require('path'); const path = require('path');
const angular = require('angular');
const availableDrives = require('../../../lib/gui/models/available-drives'); const availableDrives = require('../../../lib/gui/models/available-drives');
require('angular-mocks'); const selectionState = require('../../../lib/gui/models/selection-state');
describe('Browser: availableDrives', function() { describe('Browser: availableDrives', function() {
beforeEach(angular.mock.module(
require('../../../lib/gui/models/selection-state')
));
describe('availableDrives', function() { describe('availableDrives', function() {
let SelectionStateModel;
beforeEach(angular.mock.inject(function(_SelectionStateModel_) {
SelectionStateModel = _SelectionStateModel_;
}));
it('should have no drives by default', function() { it('should have no drives by default', function() {
m.chai.expect(availableDrives.getDrives()).to.deep.equal([]); m.chai.expect(availableDrives.getDrives()).to.deep.equal([]);
}); });
@ -80,12 +69,12 @@ describe('Browser: availableDrives', function() {
describe('given no selected image and no selected drive', function() { describe('given no selected image and no selected drive', function() {
beforeEach(function() { beforeEach(function() {
SelectionStateModel.removeDrive(); selectionState.removeDrive();
SelectionStateModel.removeImage(); selectionState.removeImage();
}); });
it('should auto-select a single valid available drive', function() { it('should auto-select a single valid available drive', function() {
m.chai.expect(SelectionStateModel.hasDrive()).to.be.false; m.chai.expect(selectionState.hasDrive()).to.be.false;
availableDrives.setDrives([ availableDrives.setDrives([
{ {
@ -98,8 +87,8 @@ describe('Browser: availableDrives', function() {
} }
]); ]);
m.chai.expect(SelectionStateModel.hasDrive()).to.be.true; m.chai.expect(selectionState.hasDrive()).to.be.true;
m.chai.expect(SelectionStateModel.getDrive().device).to.equal('/dev/sdb'); m.chai.expect(selectionState.getDrive().device).to.equal('/dev/sdb');
}); });
}); });
@ -113,8 +102,8 @@ describe('Browser: availableDrives', function() {
this.imagePath = '/mnt/bar/foo.img'; this.imagePath = '/mnt/bar/foo.img';
} }
SelectionStateModel.removeDrive(); selectionState.removeDrive();
SelectionStateModel.setImage({ selectionState.setImage({
path: this.imagePath, path: this.imagePath,
extension: 'img', extension: 'img',
size: { size: {
@ -129,11 +118,11 @@ describe('Browser: availableDrives', function() {
}); });
afterEach(function() { afterEach(function() {
SelectionStateModel.removeImage(); selectionState.removeImage();
}); });
it('should not auto-select when there are multiple valid available drives', function() { it('should not auto-select when there are multiple valid available drives', function() {
m.chai.expect(SelectionStateModel.hasDrive()).to.be.false; m.chai.expect(selectionState.hasDrive()).to.be.false;
availableDrives.setDrives([ availableDrives.setDrives([
{ {
@ -154,11 +143,11 @@ describe('Browser: availableDrives', function() {
} }
]); ]);
m.chai.expect(SelectionStateModel.hasDrive()).to.be.false; m.chai.expect(selectionState.hasDrive()).to.be.false;
}); });
it('should auto-select a single valid available drive', function() { it('should auto-select a single valid available drive', function() {
m.chai.expect(SelectionStateModel.hasDrive()).to.be.false; m.chai.expect(selectionState.hasDrive()).to.be.false;
availableDrives.setDrives([ availableDrives.setDrives([
{ {
@ -171,7 +160,7 @@ describe('Browser: availableDrives', function() {
} }
]); ]);
m.chai.expect(SelectionStateModel.getDrive()).to.deep.equal({ m.chai.expect(selectionState.getDrive()).to.deep.equal({
device: '/dev/sdb', device: '/dev/sdb',
name: 'Foo', name: 'Foo',
size: 2000000000, size: 2000000000,
@ -182,7 +171,7 @@ describe('Browser: availableDrives', function() {
}); });
it('should not auto-select a single too small drive', function() { it('should not auto-select a single too small drive', function() {
m.chai.expect(SelectionStateModel.hasDrive()).to.be.false; m.chai.expect(selectionState.hasDrive()).to.be.false;
availableDrives.setDrives([ availableDrives.setDrives([
{ {
@ -195,11 +184,11 @@ describe('Browser: availableDrives', function() {
} }
]); ]);
m.chai.expect(SelectionStateModel.hasDrive()).to.be.false; m.chai.expect(selectionState.hasDrive()).to.be.false;
}); });
it('should not auto-select a single drive that doesn\'t meet the recommended size', function() { it('should not auto-select a single drive that doesn\'t meet the recommended size', function() {
m.chai.expect(SelectionStateModel.hasDrive()).to.be.false; m.chai.expect(selectionState.hasDrive()).to.be.false;
availableDrives.setDrives([ availableDrives.setDrives([
{ {
@ -212,11 +201,11 @@ describe('Browser: availableDrives', function() {
} }
]); ]);
m.chai.expect(SelectionStateModel.hasDrive()).to.be.false; m.chai.expect(selectionState.hasDrive()).to.be.false;
}); });
it('should not auto-select a single protected drive', function() { it('should not auto-select a single protected drive', function() {
m.chai.expect(SelectionStateModel.hasDrive()).to.be.false; m.chai.expect(selectionState.hasDrive()).to.be.false;
availableDrives.setDrives([ availableDrives.setDrives([
{ {
@ -229,11 +218,11 @@ describe('Browser: availableDrives', function() {
} }
]); ]);
m.chai.expect(SelectionStateModel.hasDrive()).to.be.false; m.chai.expect(selectionState.hasDrive()).to.be.false;
}); });
it('should not auto-select a source drive', function() { it('should not auto-select a source drive', function() {
m.chai.expect(SelectionStateModel.hasDrive()).to.be.false; m.chai.expect(selectionState.hasDrive()).to.be.false;
availableDrives.setDrives([ availableDrives.setDrives([
{ {
@ -250,11 +239,11 @@ describe('Browser: availableDrives', function() {
} }
]); ]);
m.chai.expect(SelectionStateModel.hasDrive()).to.be.false; m.chai.expect(selectionState.hasDrive()).to.be.false;
}); });
it('should not auto-select a single system drive', function() { it('should not auto-select a single system drive', function() {
m.chai.expect(SelectionStateModel.hasDrive()).to.be.false; m.chai.expect(selectionState.hasDrive()).to.be.false;
availableDrives.setDrives([ availableDrives.setDrives([
{ {
@ -267,7 +256,7 @@ describe('Browser: availableDrives', function() {
} }
]); ]);
m.chai.expect(SelectionStateModel.hasDrive()).to.be.false; m.chai.expect(selectionState.hasDrive()).to.be.false;
}); });
}); });
@ -315,15 +304,15 @@ describe('Browser: availableDrives', function() {
} }
]); ]);
SelectionStateModel.setDrive('/dev/sdc'); selectionState.setDrive('/dev/sdc');
}); });
afterEach(function() { afterEach(function() {
SelectionStateModel.removeDrive(); selectionState.removeDrive();
}); });
it('should be deleted if its not contained in the available drives anymore', function() { it('should be deleted if its not contained in the available drives anymore', function() {
m.chai.expect(SelectionStateModel.hasDrive()).to.be.true; m.chai.expect(selectionState.hasDrive()).to.be.true;
// We have to provide at least two drives, otherwise, // We have to provide at least two drives, otherwise,
// if we only provide one, the single drive will be // if we only provide one, the single drive will be
@ -347,7 +336,7 @@ describe('Browser: availableDrives', function() {
} }
]); ]);
m.chai.expect(SelectionStateModel.hasDrive()).to.be.false; m.chai.expect(selectionState.hasDrive()).to.be.false;
}); });
}); });

View File

@ -3,74 +3,61 @@
const m = require('mochainon'); const m = require('mochainon');
const _ = require('lodash'); const _ = require('lodash');
const path = require('path'); const path = require('path');
const angular = require('angular');
const availableDrives = require('../../../lib/gui/models/available-drives'); const availableDrives = require('../../../lib/gui/models/available-drives');
require('angular-mocks'); const selectionState = require('../../../lib/gui/models/selection-state');
describe('Browser: SelectionState', function() { describe('Browser: selectionState', function() {
beforeEach(angular.mock.module(
require('../../../lib/gui/models/selection-state')
));
describe('SelectionStateModel', function() {
let SelectionStateModel;
beforeEach(angular.mock.inject(function(_SelectionStateModel_) {
SelectionStateModel = _SelectionStateModel_;
}));
describe('given a clean state', function() { describe('given a clean state', function() {
beforeEach(function() { beforeEach(function() {
SelectionStateModel.clear(); selectionState.clear();
}); });
it('getDrive() should return undefined', function() { it('getDrive() should return undefined', function() {
const drive = SelectionStateModel.getDrive(); const drive = selectionState.getDrive();
m.chai.expect(drive).to.be.undefined; m.chai.expect(drive).to.be.undefined;
}); });
it('getImage() should return undefined', function() { it('getImage() should return undefined', function() {
m.chai.expect(SelectionStateModel.getImage()).to.be.undefined; m.chai.expect(selectionState.getImage()).to.be.undefined;
}); });
it('getImagePath() should return undefined', function() { it('getImagePath() should return undefined', function() {
m.chai.expect(SelectionStateModel.getImagePath()).to.be.undefined; m.chai.expect(selectionState.getImagePath()).to.be.undefined;
}); });
it('getImageSize() should return undefined', function() { it('getImageSize() should return undefined', function() {
m.chai.expect(SelectionStateModel.getImageSize()).to.be.undefined; m.chai.expect(selectionState.getImageSize()).to.be.undefined;
}); });
it('getImageUrl() should return undefined', function() { it('getImageUrl() should return undefined', function() {
m.chai.expect(SelectionStateModel.getImageUrl()).to.be.undefined; m.chai.expect(selectionState.getImageUrl()).to.be.undefined;
}); });
it('getImageName() should return undefined', function() { it('getImageName() should return undefined', function() {
m.chai.expect(SelectionStateModel.getImageName()).to.be.undefined; m.chai.expect(selectionState.getImageName()).to.be.undefined;
}); });
it('getImageLogo() should return undefined', function() { it('getImageLogo() should return undefined', function() {
m.chai.expect(SelectionStateModel.getImageLogo()).to.be.undefined; m.chai.expect(selectionState.getImageLogo()).to.be.undefined;
}); });
it('getImageSupportUrl() should return undefined', function() { it('getImageSupportUrl() should return undefined', function() {
m.chai.expect(SelectionStateModel.getImageSupportUrl()).to.be.undefined; m.chai.expect(selectionState.getImageSupportUrl()).to.be.undefined;
}); });
it('getImageRecommendedDriveSize() should return undefined', function() { it('getImageRecommendedDriveSize() should return undefined', function() {
m.chai.expect(SelectionStateModel.getImageRecommendedDriveSize()).to.be.undefined; m.chai.expect(selectionState.getImageRecommendedDriveSize()).to.be.undefined;
}); });
it('hasDrive() should return false', function() { it('hasDrive() should return false', function() {
const hasDrive = SelectionStateModel.hasDrive(); const hasDrive = selectionState.hasDrive();
m.chai.expect(hasDrive).to.be.false; m.chai.expect(hasDrive).to.be.false;
}); });
it('hasImage() should return false', function() { it('hasImage() should return false', function() {
const hasImage = SelectionStateModel.hasImage(); const hasImage = selectionState.hasImage();
m.chai.expect(hasImage).to.be.false; m.chai.expect(hasImage).to.be.false;
}); });
@ -94,13 +81,13 @@ describe('Browser: SelectionState', function() {
} }
]); ]);
SelectionStateModel.setDrive('/dev/disk2'); selectionState.setDrive('/dev/disk2');
}); });
describe('.getDrive()', function() { describe('.getDrive()', function() {
it('should return the drive', function() { it('should return the drive', function() {
const drive = SelectionStateModel.getDrive(); const drive = selectionState.getDrive();
m.chai.expect(drive).to.deep.equal({ m.chai.expect(drive).to.deep.equal({
device: '/dev/disk2', device: '/dev/disk2',
name: 'USB Drive', name: 'USB Drive',
@ -114,7 +101,7 @@ describe('Browser: SelectionState', function() {
describe('.hasDrive()', function() { describe('.hasDrive()', function() {
it('should return true', function() { it('should return true', function() {
const hasDrive = SelectionStateModel.hasDrive(); const hasDrive = selectionState.hasDrive();
m.chai.expect(hasDrive).to.be.true; m.chai.expect(hasDrive).to.be.true;
}); });
@ -123,8 +110,8 @@ describe('Browser: SelectionState', function() {
describe('.setDrive()', function() { describe('.setDrive()', function() {
it('should override the drive', function() { it('should override the drive', function() {
SelectionStateModel.setDrive('/dev/disk5'); selectionState.setDrive('/dev/disk5');
const drive = SelectionStateModel.getDrive(); const drive = selectionState.getDrive();
m.chai.expect(drive).to.deep.equal({ m.chai.expect(drive).to.deep.equal({
device: '/dev/disk5', device: '/dev/disk5',
name: 'USB Drive', name: 'USB Drive',
@ -138,8 +125,8 @@ describe('Browser: SelectionState', function() {
describe('.removeDrive()', function() { describe('.removeDrive()', function() {
it('should clear the drive', function() { it('should clear the drive', function() {
SelectionStateModel.removeDrive(); selectionState.removeDrive();
const drive = SelectionStateModel.getDrive(); const drive = selectionState.getDrive();
m.chai.expect(drive).to.be.undefined; m.chai.expect(drive).to.be.undefined;
}); });
@ -161,8 +148,8 @@ describe('Browser: SelectionState', function() {
} }
]); ]);
SelectionStateModel.setDrive('/dev/disk5'); selectionState.setDrive('/dev/disk5');
const drive = SelectionStateModel.getDrive(); const drive = selectionState.getDrive();
m.chai.expect(drive).to.deep.equal({ m.chai.expect(drive).to.deep.equal({
device: '/dev/disk5', device: '/dev/disk5',
name: 'USB Drive', name: 'USB Drive',
@ -182,7 +169,7 @@ describe('Browser: SelectionState', function() {
]); ]);
m.chai.expect(function() { m.chai.expect(function() {
SelectionStateModel.setDrive('/dev/disk1'); selectionState.setDrive('/dev/disk1');
}).to.throw('The drive is write-protected'); }).to.throw('The drive is write-protected');
}); });
@ -197,13 +184,13 @@ describe('Browser: SelectionState', function() {
]); ]);
m.chai.expect(function() { m.chai.expect(function() {
SelectionStateModel.setDrive('/dev/disk5'); selectionState.setDrive('/dev/disk5');
}).to.throw('The drive is not available: /dev/disk5'); }).to.throw('The drive is not available: /dev/disk5');
}); });
it('should throw if device is not a string', function() { it('should throw if device is not a string', function() {
m.chai.expect(function() { m.chai.expect(function() {
SelectionStateModel.setDrive(123); selectionState.setDrive(123);
}).to.throw('Invalid drive: 123'); }).to.throw('Invalid drive: 123');
}); });
@ -231,7 +218,7 @@ describe('Browser: SelectionState', function() {
logo: '<svg><text fill="red">Raspbian</text></svg>' logo: '<svg><text fill="red">Raspbian</text></svg>'
}; };
SelectionStateModel.setImage(this.image); selectionState.setImage(this.image);
}); });
describe('.setDrive()', function() { describe('.setDrive()', function() {
@ -247,7 +234,7 @@ describe('Browser: SelectionState', function() {
]); ]);
m.chai.expect(function() { m.chai.expect(function() {
SelectionStateModel.setDrive('/dev/disk2'); selectionState.setDrive('/dev/disk2');
}).to.throw('The drive is not large enough'); }).to.throw('The drive is not large enough');
}); });
@ -256,7 +243,7 @@ describe('Browser: SelectionState', function() {
describe('.getImage()', function() { describe('.getImage()', function() {
it('should return the image', function() { it('should return the image', function() {
m.chai.expect(SelectionStateModel.getImage()).to.deep.equal(this.image); m.chai.expect(selectionState.getImage()).to.deep.equal(this.image);
}); });
}); });
@ -264,7 +251,7 @@ describe('Browser: SelectionState', function() {
describe('.getImagePath()', function() { describe('.getImagePath()', function() {
it('should return the image path', function() { it('should return the image path', function() {
const imagePath = SelectionStateModel.getImagePath(); const imagePath = selectionState.getImagePath();
m.chai.expect(imagePath).to.equal('foo.img'); m.chai.expect(imagePath).to.equal('foo.img');
}); });
@ -273,7 +260,7 @@ describe('Browser: SelectionState', function() {
describe('.getImageSize()', function() { describe('.getImageSize()', function() {
it('should return the image size', function() { it('should return the image size', function() {
const imageSize = SelectionStateModel.getImageSize(); const imageSize = selectionState.getImageSize();
m.chai.expect(imageSize).to.equal(999999999); m.chai.expect(imageSize).to.equal(999999999);
}); });
@ -282,7 +269,7 @@ describe('Browser: SelectionState', function() {
describe('.getImageUrl()', function() { describe('.getImageUrl()', function() {
it('should return the image url', function() { it('should return the image url', function() {
const imageUrl = SelectionStateModel.getImageUrl(); const imageUrl = selectionState.getImageUrl();
m.chai.expect(imageUrl).to.equal('https://www.raspbian.org'); m.chai.expect(imageUrl).to.equal('https://www.raspbian.org');
}); });
@ -291,7 +278,7 @@ describe('Browser: SelectionState', function() {
describe('.getImageName()', function() { describe('.getImageName()', function() {
it('should return the image name', function() { it('should return the image name', function() {
const imageName = SelectionStateModel.getImageName(); const imageName = selectionState.getImageName();
m.chai.expect(imageName).to.equal('Raspbian'); m.chai.expect(imageName).to.equal('Raspbian');
}); });
@ -300,7 +287,7 @@ describe('Browser: SelectionState', function() {
describe('.getImageLogo()', function() { describe('.getImageLogo()', function() {
it('should return the image logo', function() { it('should return the image logo', function() {
const imageLogo = SelectionStateModel.getImageLogo(); const imageLogo = selectionState.getImageLogo();
m.chai.expect(imageLogo).to.equal('<svg><text fill="red">Raspbian</text></svg>'); m.chai.expect(imageLogo).to.equal('<svg><text fill="red">Raspbian</text></svg>');
}); });
@ -309,7 +296,7 @@ describe('Browser: SelectionState', function() {
describe('.getImageSupportUrl()', function() { describe('.getImageSupportUrl()', function() {
it('should return the image support url', function() { it('should return the image support url', function() {
const imageSupportUrl = SelectionStateModel.getImageSupportUrl(); const imageSupportUrl = selectionState.getImageSupportUrl();
m.chai.expect(imageSupportUrl).to.equal('https://www.raspbian.org/forums/'); m.chai.expect(imageSupportUrl).to.equal('https://www.raspbian.org/forums/');
}); });
@ -318,7 +305,7 @@ describe('Browser: SelectionState', function() {
describe('.getImageRecommendedDriveSize()', function() { describe('.getImageRecommendedDriveSize()', function() {
it('should return the image recommended drive size', function() { it('should return the image recommended drive size', function() {
const imageRecommendedDriveSize = SelectionStateModel.getImageRecommendedDriveSize(); const imageRecommendedDriveSize = selectionState.getImageRecommendedDriveSize();
m.chai.expect(imageRecommendedDriveSize).to.equal(1000000000); m.chai.expect(imageRecommendedDriveSize).to.equal(1000000000);
}); });
@ -327,7 +314,7 @@ describe('Browser: SelectionState', function() {
describe('.hasImage()', function() { describe('.hasImage()', function() {
it('should return true', function() { it('should return true', function() {
const hasImage = SelectionStateModel.hasImage(); const hasImage = selectionState.hasImage();
m.chai.expect(hasImage).to.be.true; m.chai.expect(hasImage).to.be.true;
}); });
@ -336,7 +323,7 @@ describe('Browser: SelectionState', function() {
describe('.setImage()', function() { describe('.setImage()', function() {
it('should override the image', function() { it('should override the image', function() {
SelectionStateModel.setImage({ selectionState.setImage({
path: 'bar.img', path: 'bar.img',
extension: 'img', extension: 'img',
size: { size: {
@ -348,9 +335,9 @@ describe('Browser: SelectionState', function() {
} }
}); });
const imagePath = SelectionStateModel.getImagePath(); const imagePath = selectionState.getImagePath();
m.chai.expect(imagePath).to.equal('bar.img'); m.chai.expect(imagePath).to.equal('bar.img');
const imageSize = SelectionStateModel.getImageSize(); const imageSize = selectionState.getImageSize();
m.chai.expect(imageSize).to.equal(999999999); m.chai.expect(imageSize).to.equal(999999999);
}); });
@ -359,11 +346,11 @@ describe('Browser: SelectionState', function() {
describe('.removeImage()', function() { describe('.removeImage()', function() {
it('should clear the image', function() { it('should clear the image', function() {
SelectionStateModel.removeImage(); selectionState.removeImage();
const imagePath = SelectionStateModel.getImagePath(); const imagePath = selectionState.getImagePath();
m.chai.expect(imagePath).to.be.undefined; m.chai.expect(imagePath).to.be.undefined;
const imageSize = SelectionStateModel.getImageSize(); const imageSize = selectionState.getImageSize();
m.chai.expect(imageSize).to.be.undefined; m.chai.expect(imageSize).to.be.undefined;
}); });
@ -376,7 +363,7 @@ describe('Browser: SelectionState', function() {
describe('.setImage()', function() { describe('.setImage()', function() {
it('should be able to set an image', function() { it('should be able to set an image', function() {
SelectionStateModel.setImage({ selectionState.setImage({
path: 'foo.img', path: 'foo.img',
extension: 'img', extension: 'img',
size: { size: {
@ -388,14 +375,14 @@ describe('Browser: SelectionState', function() {
} }
}); });
const imagePath = SelectionStateModel.getImagePath(); const imagePath = selectionState.getImagePath();
m.chai.expect(imagePath).to.equal('foo.img'); m.chai.expect(imagePath).to.equal('foo.img');
const imageSize = SelectionStateModel.getImageSize(); const imageSize = selectionState.getImageSize();
m.chai.expect(imageSize).to.equal(999999999); m.chai.expect(imageSize).to.equal(999999999);
}); });
it('should be able to set an image with an archive extension', function() { it('should be able to set an image with an archive extension', function() {
SelectionStateModel.setImage({ selectionState.setImage({
path: 'foo.zip', path: 'foo.zip',
extension: 'img', extension: 'img',
archiveExtension: 'zip', archiveExtension: 'zip',
@ -408,13 +395,13 @@ describe('Browser: SelectionState', function() {
} }
}); });
const imagePath = SelectionStateModel.getImagePath(); const imagePath = selectionState.getImagePath();
m.chai.expect(imagePath).to.equal('foo.zip'); m.chai.expect(imagePath).to.equal('foo.zip');
}); });
it('should throw if no path', function() { it('should throw if no path', function() {
m.chai.expect(function() { m.chai.expect(function() {
SelectionStateModel.setImage({ selectionState.setImage({
extension: 'img', extension: 'img',
size: { size: {
original: 999999999, original: 999999999,
@ -429,7 +416,7 @@ describe('Browser: SelectionState', function() {
it('should throw if path is not a string', function() { it('should throw if path is not a string', function() {
m.chai.expect(function() { m.chai.expect(function() {
SelectionStateModel.setImage({ selectionState.setImage({
path: 123, path: 123,
extension: 'img', extension: 'img',
size: { size: {
@ -445,7 +432,7 @@ describe('Browser: SelectionState', function() {
it('should throw if no extension', function() { it('should throw if no extension', function() {
m.chai.expect(function() { m.chai.expect(function() {
SelectionStateModel.setImage({ selectionState.setImage({
path: 'foo.img', path: 'foo.img',
size: { size: {
original: 999999999, original: 999999999,
@ -460,7 +447,7 @@ describe('Browser: SelectionState', function() {
it('should throw if extension is not a string', function() { it('should throw if extension is not a string', function() {
m.chai.expect(function() { m.chai.expect(function() {
SelectionStateModel.setImage({ selectionState.setImage({
path: 'foo.img', path: 'foo.img',
extension: 1, extension: 1,
size: { size: {
@ -476,7 +463,7 @@ describe('Browser: SelectionState', function() {
it('should throw if the extension doesn\'t match the path and there is no archive extension', function() { it('should throw if the extension doesn\'t match the path and there is no archive extension', function() {
m.chai.expect(function() { m.chai.expect(function() {
SelectionStateModel.setImage({ selectionState.setImage({
path: 'foo.img', path: 'foo.img',
extension: 'iso', extension: 'iso',
size: { size: {
@ -492,7 +479,7 @@ describe('Browser: SelectionState', function() {
it('should throw if the extension doesn\'t match the path and the archive extension is not a string', function() { it('should throw if the extension doesn\'t match the path and the archive extension is not a string', function() {
m.chai.expect(function() { m.chai.expect(function() {
SelectionStateModel.setImage({ selectionState.setImage({
path: 'foo.img', path: 'foo.img',
extension: 'iso', extension: 'iso',
archiveExtension: 1, archiveExtension: 1,
@ -509,7 +496,7 @@ describe('Browser: SelectionState', function() {
it('should throw if the archive extension doesn\'t match the last path extension in a compressed image', function() { it('should throw if the archive extension doesn\'t match the last path extension in a compressed image', function() {
m.chai.expect(function() { m.chai.expect(function() {
SelectionStateModel.setImage({ selectionState.setImage({
path: 'foo.img.xz', path: 'foo.img.xz',
extension: 'img', extension: 'img',
archiveExtension: 'gz', archiveExtension: 'gz',
@ -526,7 +513,7 @@ describe('Browser: SelectionState', function() {
it('should throw if the extension is not recognised in an uncompressed image', function() { it('should throw if the extension is not recognised in an uncompressed image', function() {
m.chai.expect(function() { m.chai.expect(function() {
SelectionStateModel.setImage({ selectionState.setImage({
path: 'foo.ifg', path: 'foo.ifg',
extension: 'ifg', extension: 'ifg',
size: { size: {
@ -542,7 +529,7 @@ describe('Browser: SelectionState', function() {
it('should throw if the extension is not recognised in a compressed image', function() { it('should throw if the extension is not recognised in a compressed image', function() {
m.chai.expect(function() { m.chai.expect(function() {
SelectionStateModel.setImage({ selectionState.setImage({
path: 'foo.ifg.gz', path: 'foo.ifg.gz',
extension: 'ifg', extension: 'ifg',
archiveExtension: 'gz', archiveExtension: 'gz',
@ -559,7 +546,7 @@ describe('Browser: SelectionState', function() {
it('should throw if the archive extension is not recognised', function() { it('should throw if the archive extension is not recognised', function() {
m.chai.expect(function() { m.chai.expect(function() {
SelectionStateModel.setImage({ selectionState.setImage({
path: 'foo.img.ifg', path: 'foo.img.ifg',
extension: 'img', extension: 'img',
archiveExtension: 'ifg', archiveExtension: 'ifg',
@ -576,7 +563,7 @@ describe('Browser: SelectionState', function() {
it('should throw if no size', function() { it('should throw if no size', function() {
m.chai.expect(function() { m.chai.expect(function() {
SelectionStateModel.setImage({ selectionState.setImage({
path: 'foo.img', path: 'foo.img',
extension: 'img' extension: 'img'
}); });
@ -585,7 +572,7 @@ describe('Browser: SelectionState', function() {
it('should throw if size is not a plain object', function() { it('should throw if size is not a plain object', function() {
m.chai.expect(function() { m.chai.expect(function() {
SelectionStateModel.setImage({ selectionState.setImage({
path: 'foo.img', path: 'foo.img',
extension: 'img', extension: 'img',
size: 999999999 size: 999999999
@ -595,7 +582,7 @@ describe('Browser: SelectionState', function() {
it('should throw if the original size is not a number', function() { it('should throw if the original size is not a number', function() {
m.chai.expect(function() { m.chai.expect(function() {
SelectionStateModel.setImage({ selectionState.setImage({
path: 'foo.img', path: 'foo.img',
extension: 'img', extension: 'img',
size: { size: {
@ -611,7 +598,7 @@ describe('Browser: SelectionState', function() {
it('should throw if the original size is a float number', function() { it('should throw if the original size is a float number', function() {
m.chai.expect(function() { m.chai.expect(function() {
SelectionStateModel.setImage({ selectionState.setImage({
path: 'foo.img', path: 'foo.img',
extension: 'img', extension: 'img',
size: { size: {
@ -627,7 +614,7 @@ describe('Browser: SelectionState', function() {
it('should throw if the original size is negative', function() { it('should throw if the original size is negative', function() {
m.chai.expect(function() { m.chai.expect(function() {
SelectionStateModel.setImage({ selectionState.setImage({
path: 'foo.img', path: 'foo.img',
extension: 'img', extension: 'img',
size: { size: {
@ -643,7 +630,7 @@ describe('Browser: SelectionState', function() {
it('should throw if the final size is not a number', function() { it('should throw if the final size is not a number', function() {
m.chai.expect(function() { m.chai.expect(function() {
SelectionStateModel.setImage({ selectionState.setImage({
path: 'foo.img', path: 'foo.img',
extension: 'img', extension: 'img',
size: { size: {
@ -659,7 +646,7 @@ describe('Browser: SelectionState', function() {
it('should throw if the final size is a float number', function() { it('should throw if the final size is a float number', function() {
m.chai.expect(function() { m.chai.expect(function() {
SelectionStateModel.setImage({ selectionState.setImage({
path: 'foo.img', path: 'foo.img',
extension: 'img', extension: 'img',
size: { size: {
@ -675,7 +662,7 @@ describe('Browser: SelectionState', function() {
it('should throw if the final size is negative', function() { it('should throw if the final size is negative', function() {
m.chai.expect(function() { m.chai.expect(function() {
SelectionStateModel.setImage({ selectionState.setImage({
path: 'foo.img', path: 'foo.img',
extension: 'img', extension: 'img',
size: { size: {
@ -691,7 +678,7 @@ describe('Browser: SelectionState', function() {
it('should throw if the final size estimation flag is not a boolean', function() { it('should throw if the final size estimation flag is not a boolean', function() {
m.chai.expect(function() { m.chai.expect(function() {
SelectionStateModel.setImage({ selectionState.setImage({
path: 'foo.img', path: 'foo.img',
extension: 'img', extension: 'img',
size: { size: {
@ -707,7 +694,7 @@ describe('Browser: SelectionState', function() {
it('should throw if url is defined but it\'s not a string', function() { it('should throw if url is defined but it\'s not a string', function() {
m.chai.expect(function() { m.chai.expect(function() {
SelectionStateModel.setImage({ selectionState.setImage({
path: 'foo.img', path: 'foo.img',
extension: 'img', extension: 'img',
size: { size: {
@ -724,7 +711,7 @@ describe('Browser: SelectionState', function() {
it('should throw if name is defined but it\'s not a string', function() { it('should throw if name is defined but it\'s not a string', function() {
m.chai.expect(function() { m.chai.expect(function() {
SelectionStateModel.setImage({ selectionState.setImage({
path: 'foo.img', path: 'foo.img',
extension: 'img', extension: 'img',
size: { size: {
@ -741,7 +728,7 @@ describe('Browser: SelectionState', function() {
it('should throw if logo is defined but it\'s not a string', function() { it('should throw if logo is defined but it\'s not a string', function() {
m.chai.expect(function() { m.chai.expect(function() {
SelectionStateModel.setImage({ selectionState.setImage({
path: 'foo.img', path: 'foo.img',
extension: 'img', extension: 'img',
size: { size: {
@ -766,10 +753,10 @@ describe('Browser: SelectionState', function() {
} }
]); ]);
SelectionStateModel.setDrive('/dev/disk1'); selectionState.setDrive('/dev/disk1');
m.chai.expect(SelectionStateModel.hasDrive()).to.be.true; m.chai.expect(selectionState.hasDrive()).to.be.true;
SelectionStateModel.setImage({ selectionState.setImage({
path: 'foo.img', path: 'foo.img',
extension: 'img', extension: 'img',
size: { size: {
@ -781,8 +768,8 @@ describe('Browser: SelectionState', function() {
} }
}); });
m.chai.expect(SelectionStateModel.hasDrive()).to.be.false; m.chai.expect(selectionState.hasDrive()).to.be.false;
SelectionStateModel.removeImage(); selectionState.removeImage();
}); });
it('should de-select a previously selected not-recommended drive', function() { it('should de-select a previously selected not-recommended drive', function() {
@ -795,10 +782,10 @@ describe('Browser: SelectionState', function() {
} }
]); ]);
SelectionStateModel.setDrive('/dev/disk1'); selectionState.setDrive('/dev/disk1');
m.chai.expect(SelectionStateModel.hasDrive()).to.be.true; m.chai.expect(selectionState.hasDrive()).to.be.true;
SelectionStateModel.setImage({ selectionState.setImage({
path: 'foo.img', path: 'foo.img',
extension: 'img', extension: 'img',
size: { size: {
@ -811,8 +798,8 @@ describe('Browser: SelectionState', function() {
recommendedDriveSize: 1500000000 recommendedDriveSize: 1500000000
}); });
m.chai.expect(SelectionStateModel.hasDrive()).to.be.false; m.chai.expect(selectionState.hasDrive()).to.be.false;
SelectionStateModel.removeImage(); selectionState.removeImage();
}); });
it('should de-select a previously selected source drive', function() { it('should de-select a previously selected source drive', function() {
@ -838,10 +825,10 @@ describe('Browser: SelectionState', function() {
} }
]); ]);
SelectionStateModel.setDrive('/dev/disk1'); selectionState.setDrive('/dev/disk1');
m.chai.expect(SelectionStateModel.hasDrive()).to.be.true; m.chai.expect(selectionState.hasDrive()).to.be.true;
SelectionStateModel.setImage({ selectionState.setImage({
path: imagePath, path: imagePath,
extension: 'img', extension: 'img',
size: { size: {
@ -853,8 +840,8 @@ describe('Browser: SelectionState', function() {
} }
}); });
m.chai.expect(SelectionStateModel.hasDrive()).to.be.false; m.chai.expect(selectionState.hasDrive()).to.be.false;
SelectionStateModel.removeImage(); selectionState.removeImage();
}); });
}); });
@ -873,9 +860,9 @@ describe('Browser: SelectionState', function() {
} }
]); ]);
SelectionStateModel.setDrive('/dev/disk1'); selectionState.setDrive('/dev/disk1');
SelectionStateModel.setImage({ selectionState.setImage({
path: 'foo.img', path: 'foo.img',
extension: 'img', extension: 'img',
size: { size: {
@ -891,13 +878,13 @@ describe('Browser: SelectionState', function() {
describe('.clear()', function() { describe('.clear()', function() {
it('should clear all selections', function() { it('should clear all selections', function() {
m.chai.expect(SelectionStateModel.hasDrive()).to.be.true; m.chai.expect(selectionState.hasDrive()).to.be.true;
m.chai.expect(SelectionStateModel.hasImage()).to.be.true; m.chai.expect(selectionState.hasImage()).to.be.true;
SelectionStateModel.clear(); selectionState.clear();
m.chai.expect(SelectionStateModel.hasDrive()).to.be.false; m.chai.expect(selectionState.hasDrive()).to.be.false;
m.chai.expect(SelectionStateModel.hasImage()).to.be.false; m.chai.expect(selectionState.hasImage()).to.be.false;
}); });
}); });
@ -905,33 +892,33 @@ describe('Browser: SelectionState', function() {
describe('given the preserveImage option', function() { describe('given the preserveImage option', function() {
beforeEach(function() { beforeEach(function() {
SelectionStateModel.clear({ selectionState.clear({
preserveImage: true preserveImage: true
}); });
}); });
it('getDrive() should return undefined', function() { it('getDrive() should return undefined', function() {
const drive = SelectionStateModel.getDrive(); const drive = selectionState.getDrive();
m.chai.expect(drive).to.be.undefined; m.chai.expect(drive).to.be.undefined;
}); });
it('getImagePath() should return the image path', function() { it('getImagePath() should return the image path', function() {
const imagePath = SelectionStateModel.getImagePath(); const imagePath = selectionState.getImagePath();
m.chai.expect(imagePath).to.equal('foo.img'); m.chai.expect(imagePath).to.equal('foo.img');
}); });
it('getImageSize() should return the image size', function() { it('getImageSize() should return the image size', function() {
const imageSize = SelectionStateModel.getImageSize(); const imageSize = selectionState.getImageSize();
m.chai.expect(imageSize).to.equal(999999999); m.chai.expect(imageSize).to.equal(999999999);
}); });
it('hasDrive() should return false', function() { it('hasDrive() should return false', function() {
const hasDrive = SelectionStateModel.hasDrive(); const hasDrive = selectionState.hasDrive();
m.chai.expect(hasDrive).to.be.false; m.chai.expect(hasDrive).to.be.false;
}); });
it('hasImage() should return true', function() { it('hasImage() should return true', function() {
const hasImage = SelectionStateModel.hasImage(); const hasImage = selectionState.hasImage();
m.chai.expect(hasImage).to.be.true; m.chai.expect(hasImage).to.be.true;
}); });
@ -956,19 +943,19 @@ describe('Browser: SelectionState', function() {
} }
]); ]);
SelectionStateModel.setDrive('/dev/sdb'); selectionState.setDrive('/dev/sdb');
}); });
it('should return false if an undefined value is passed', function() { it('should return false if an undefined value is passed', function() {
m.chai.expect(SelectionStateModel.isCurrentDrive()).to.be.false; m.chai.expect(selectionState.isCurrentDrive()).to.be.false;
}); });
it('should return true given the exact same drive', function() { it('should return true given the exact same drive', function() {
m.chai.expect(SelectionStateModel.isCurrentDrive('/dev/sdb')).to.be.true; m.chai.expect(selectionState.isCurrentDrive('/dev/sdb')).to.be.true;
}); });
it('should return false if it is not the current drive', function() { it('should return false if it is not the current drive', function() {
m.chai.expect(SelectionStateModel.isCurrentDrive('/dev/sdc')).to.be.false; m.chai.expect(selectionState.isCurrentDrive('/dev/sdc')).to.be.false;
}); });
}); });
@ -976,15 +963,15 @@ describe('Browser: SelectionState', function() {
describe('given no selected drive', function() { describe('given no selected drive', function() {
beforeEach(function() { beforeEach(function() {
SelectionStateModel.removeDrive(); selectionState.removeDrive();
}); });
it('should return false if an undefined value is passed', function() { it('should return false if an undefined value is passed', function() {
m.chai.expect(SelectionStateModel.isCurrentDrive()).to.be.false; m.chai.expect(selectionState.isCurrentDrive()).to.be.false;
}); });
it('should return false for anything', function() { it('should return false for anything', function() {
m.chai.expect(SelectionStateModel.isCurrentDrive('/dev/sdb')).to.be.false; m.chai.expect(selectionState.isCurrentDrive('/dev/sdb')).to.be.false;
}); });
}); });
@ -1016,13 +1003,13 @@ describe('Browser: SelectionState', function() {
} }
]); ]);
SelectionStateModel.setDrive(this.drive.device); selectionState.setDrive(this.drive.device);
}); });
it('should be able to remove the drive', function() { it('should be able to remove the drive', function() {
m.chai.expect(SelectionStateModel.hasDrive()).to.be.true; m.chai.expect(selectionState.hasDrive()).to.be.true;
SelectionStateModel.toggleSetDrive(this.drive.device); selectionState.toggleSetDrive(this.drive.device);
m.chai.expect(SelectionStateModel.hasDrive()).to.be.false; m.chai.expect(selectionState.hasDrive()).to.be.false;
}); });
it('should be able to replace the drive', function() { it('should be able to replace the drive', function() {
@ -1033,10 +1020,10 @@ describe('Browser: SelectionState', function() {
protected: false protected: false
}; };
m.chai.expect(SelectionStateModel.getDrive()).to.deep.equal(this.drive); m.chai.expect(selectionState.getDrive()).to.deep.equal(this.drive);
SelectionStateModel.toggleSetDrive(drive.device); selectionState.toggleSetDrive(drive.device);
m.chai.expect(SelectionStateModel.getDrive()).to.deep.equal(drive); m.chai.expect(selectionState.getDrive()).to.deep.equal(drive);
m.chai.expect(SelectionStateModel.getDrive()).to.not.deep.equal(this.drive); m.chai.expect(selectionState.getDrive()).to.not.deep.equal(this.drive);
}); });
}); });
@ -1044,7 +1031,7 @@ describe('Browser: SelectionState', function() {
describe('given no selected drive', function() { describe('given no selected drive', function() {
beforeEach(function() { beforeEach(function() {
SelectionStateModel.removeDrive(); selectionState.removeDrive();
}); });
it('should set the drive', function() { it('should set the drive', function() {
@ -1055,11 +1042,9 @@ describe('Browser: SelectionState', function() {
protected: false protected: false
}; };
m.chai.expect(SelectionStateModel.hasDrive()).to.be.false; m.chai.expect(selectionState.hasDrive()).to.be.false;
SelectionStateModel.toggleSetDrive(drive.device); selectionState.toggleSetDrive(drive.device);
m.chai.expect(SelectionStateModel.getDrive()).to.deep.equal(drive); m.chai.expect(selectionState.getDrive()).to.deep.equal(drive);
});
}); });
}); });

View File

@ -8,6 +8,7 @@ const angular = require('angular');
const settings = require('../../../lib/gui/models/settings'); const settings = require('../../../lib/gui/models/settings');
const flashState = require('../../../lib/gui/models/flash-state'); const flashState = require('../../../lib/gui/models/flash-state');
const availableDrives = require('../../../lib/gui/models/available-drives'); const availableDrives = require('../../../lib/gui/models/available-drives');
const selectionState = require('../../../lib/gui/models/selection-state');
require('angular-mocks'); require('angular-mocks');
describe('Browser: MainPage', function() { describe('Browser: MainPage', function() {
@ -19,11 +20,9 @@ describe('Browser: MainPage', function() {
describe('MainController', function() { describe('MainController', function() {
let $controller; let $controller;
let SelectionStateModel;
beforeEach(angular.mock.inject(function(_$controller_, _SelectionStateModel_) { beforeEach(angular.mock.inject(function(_$controller_) {
$controller = _$controller_; $controller = _$controller_;
SelectionStateModel = _SelectionStateModel_;
})); }));
describe('.shouldDriveStepBeDisabled()', function() { describe('.shouldDriveStepBeDisabled()', function() {
@ -33,7 +32,7 @@ describe('Browser: MainPage', function() {
$scope: {} $scope: {}
}); });
SelectionStateModel.clear(); selectionState.clear();
m.chai.expect(controller.shouldDriveStepBeDisabled()).to.be.true; m.chai.expect(controller.shouldDriveStepBeDisabled()).to.be.true;
}); });
@ -43,7 +42,7 @@ describe('Browser: MainPage', function() {
$scope: {} $scope: {}
}); });
SelectionStateModel.setImage({ selectionState.setImage({
path: 'rpi.img', path: 'rpi.img',
extension: 'img', extension: 'img',
size: { size: {
@ -67,7 +66,7 @@ describe('Browser: MainPage', function() {
$scope: {} $scope: {}
}); });
SelectionStateModel.clear(); selectionState.clear();
m.chai.expect(controller.shouldFlashStepBeDisabled()).to.be.true; m.chai.expect(controller.shouldFlashStepBeDisabled()).to.be.true;
}); });
@ -77,8 +76,8 @@ describe('Browser: MainPage', function() {
$scope: {} $scope: {}
}); });
SelectionStateModel.clear(); selectionState.clear();
SelectionStateModel.setImage({ selectionState.setImage({
path: 'rpi.img', path: 'rpi.img',
extension: 'img', extension: 'img',
size: { size: {
@ -108,8 +107,8 @@ describe('Browser: MainPage', function() {
} }
]); ]);
SelectionStateModel.clear(); selectionState.clear();
SelectionStateModel.setDrive('/dev/disk2'); selectionState.setDrive('/dev/disk2');
m.chai.expect(controller.shouldFlashStepBeDisabled()).to.be.true; m.chai.expect(controller.shouldFlashStepBeDisabled()).to.be.true;
}); });
@ -129,10 +128,10 @@ describe('Browser: MainPage', function() {
} }
]); ]);
SelectionStateModel.clear(); selectionState.clear();
SelectionStateModel.setDrive('/dev/disk2'); selectionState.setDrive('/dev/disk2');
SelectionStateModel.setImage({ selectionState.setImage({
path: 'rpi.img', path: 'rpi.img',
extension: 'img', extension: 'img',
size: { size: {
@ -154,11 +153,9 @@ describe('Browser: MainPage', function() {
describe('ImageSelectionController', function() { describe('ImageSelectionController', function() {
let $controller; let $controller;
let SelectionStateModel;
beforeEach(angular.mock.inject(function(_$controller_, _SelectionStateModel_) { beforeEach(angular.mock.inject(function(_$controller_) {
$controller = _$controller_; $controller = _$controller_;
SelectionStateModel = _SelectionStateModel_;
})); }));
it('should contain all available extensions in mainSupportedExtensions and extraSupportedExtensions', function() { it('should contain all available extensions in mainSupportedExtensions and extraSupportedExtensions', function() {
@ -178,7 +175,7 @@ describe('Browser: MainPage', function() {
$scope: {} $scope: {}
}); });
SelectionStateModel.setImage({ selectionState.setImage({
path: path.join(__dirname, 'foo', 'bar.img'), path: path.join(__dirname, 'foo', 'bar.img'),
extension: 'img', extension: 'img',
size: { size: {
@ -191,7 +188,7 @@ describe('Browser: MainPage', function() {
}); });
m.chai.expect(controller.getImageBasename()).to.equal('bar.img'); m.chai.expect(controller.getImageBasename()).to.equal('bar.img');
SelectionStateModel.removeImage(); selectionState.removeImage();
}); });
it('should return an empty string if no selected image', function() { it('should return an empty string if no selected image', function() {
@ -199,7 +196,7 @@ describe('Browser: MainPage', function() {
$scope: {} $scope: {}
}); });
SelectionStateModel.removeImage(); selectionState.removeImage();
m.chai.expect(controller.getImageBasename()).to.equal(''); m.chai.expect(controller.getImageBasename()).to.equal('');
}); });