Encapsulate DB.js in angular constants to help testing

This commit is contained in:
Colin McLeod
2015-05-04 21:54:36 -07:00
parent c297bb6e8e
commit bf04a2cc19
8 changed files with 14 additions and 12 deletions

View File

@@ -13,7 +13,6 @@ angular.module('app', ['ui.router', 'shipyard', 'ngLodash', 'app.templates'])
$rootScope.HPC = hpc;
$rootScope.igMap = igMap;
$rootScope.hgMap = hgMap;
$rootScope.ships = DB.ships;
$rootScope.title = 'Coriolis';
// Formatters

View File

@@ -1,7 +1,7 @@
/**
* Sets up the routes and handlers before the Angular app is kicked off.
*/
angular.module('app').config(['$provide','$stateProvider', '$urlRouterProvider', '$locationProvider', function ($provide, $stateProvider, $urlRouterProvider, $locationProvider) {
angular.module('app').config(['$provide','$stateProvider', '$urlRouterProvider', '$locationProvider', 'ShipsDB', function ($provide, $stateProvider, $urlRouterProvider, $locationProvider, ships) {
// Use HTML5 push and replace state if possible
$locationProvider.html5Mode(true);
/**
@@ -19,7 +19,7 @@ angular.module('app').config(['$provide','$stateProvider', '$urlRouterProvider',
controller: 'OutfitController',
resolve: {
shipId: ['$stateParams',function ($p) { // Ensure ship exists before loading controller
if (!DB.ships[$p.shipId]) {
if (!ships[$p.shipId]) {
throw { type: 'no-ship', message: $p.shipId };
}
}]

View File

@@ -1,5 +1,5 @@
angular.module('app').controller('OutfitController', ['$rootScope','$scope', '$state', '$stateParams', 'Ship', 'Components', 'Serializer', 'Persist', function ($rootScope, $scope, $state, $p, Ship, Components, Serializer, Persist) {
var data = DB.ships[$p.shipId]; // Retrieve the basic ship properties, slots and defaults
angular.module('app').controller('OutfitController', ['$rootScope','$scope', '$state', '$stateParams', 'ShipsDB', 'Ship', 'Components', 'Serializer', 'Persist', function ($rootScope, $scope, $state, $p, Ships, Ship, Components, Serializer, Persist) {
var data = Ships[$p.shipId]; // Retrieve the basic ship properties, slots and defaults
var ship = new Ship($p.shipId, data.properties, data.slots); // Create a new Ship instance
// Update the ship instance with the code (if provided) or the 'factory' defaults.

View File

@@ -1,4 +1,5 @@
angular.module('app').controller('ShipyardController', ['$rootScope', function ($rootScope) {
angular.module('app').controller('ShipyardController', ['$rootScope', 'ShipsDB', function ($rootScope, ships) {
$rootScope.title = 'Coriolis';
$rootScope.bodyClass = 'docking-bay';
$rootScope.ships = ships;
}]);

View File

@@ -1,4 +1,4 @@
angular.module('app').directive('shipyardHeader', ['lodash','$rootScope', 'Persist', function (_, $rootScope, Persist) {
angular.module('app').directive('shipyardHeader', ['lodash','$rootScope', 'Persist', 'ShipsDB', function (_, $rootScope, Persist, ships) {
return {
restrict: 'E',
@@ -6,7 +6,7 @@ angular.module('app').directive('shipyardHeader', ['lodash','$rootScope', 'Persi
scope: true,
link: function (scope) {
scope.openedMenu = null;
scope.ships = DB.ships;
scope.ships = ships;
scope.allBuilds = Persist.builds;
scope.bs = Persist.state;

View File

@@ -117,7 +117,7 @@ angular.module('shipyard').factory('Ship', ['Components', 'calcShieldStrength',
Ship.prototype.useBulkhead = function(index) {
this.bulkheads.id = index;
this.bulkheads.c = DB.components.bulkheads[this.id][index];
this.bulkheads.c = Components.bulkheads(this.id, index);
this.updateTotals(); // Update mass, range, shield strength, armor
}

View File

@@ -7,6 +7,9 @@
* @requires ngLodash is a dependency of this module.
*/
angular.module('shipyard', ['ngLodash'])
// Create 'angularized' references to DB.This will aid testing
.constant('ShipsDB', DB.ships)
.constant('ComponentsDB', DB.components)
.value('commonArray', [
'Power Plant',
'Thrusters',

View File

@@ -1,5 +1,4 @@
angular.module('shipyard').service('Components', ['lodash', 'ComponentSet', function (_, ComponentSet) {
var C = DB.components;
angular.module('shipyard').service('Components', ['lodash', 'ComponentsDB', 'ShipsDB', 'ComponentSet', function (_, C, Ships, ComponentSet) {
this.cargoScoop = function() {
return { name: 'Cargo Scoop', class: 1, rating: 'H', power: 0.6};
@@ -38,7 +37,7 @@ angular.module('shipyard').service('Components', ['lodash', 'ComponentSet', func
};
this.forShip = function (shipId) {
var ship = DB.ships[shipId];
var ship = Ships[shipId];
return new ComponentSet(C, ship.properties.mass, ship.slots.common, ship.slots.internal[0], ship.slots.hardpoints[0]);
};