From e4f24e951f64339048fd1222ab35837614f948a8 Mon Sep 17 00:00:00 2001 From: Hannu Pelkonen Date: Thu, 4 Dec 2014 16:46:27 +0200 Subject: [PATCH] Fix issues when styleVariables is undefined --- lib/app/js/services/Variables.js | 74 +++++++++++++------------ test/angular/services/Variables.test.js | 16 ++++++ 2 files changed, 56 insertions(+), 34 deletions(-) diff --git a/lib/app/js/services/Variables.js b/lib/app/js/services/Variables.js index 60869127..2ecbef00 100644 --- a/lib/app/js/services/Variables.js +++ b/lib/app/js/services/Variables.js @@ -54,38 +54,42 @@ }; this.refreshValues = function() { - for (var i = 0; i < serverData.length; i++) { - var oldIndex; - if (this.variables[i] && this.variables[i].name !== serverData[i].name) { - if (!this.getServerVarByName(this.variables[i].name)) { - // This variable does not exists anymore on the server. Remove it - this.variables.splice(i, 1); - } else if (this.getLocalVarByName(serverData[i].name) && !this.getLocalVarByName(serverData[i].name).dirty) { - // The variable already exists but in another position - // It is not changed so we can just remove it - oldIndex = this.getLocalIndexByName(serverData[i].name); - this.variables.splice(oldIndex, 1); - this.variables.splice(i, 0, {name: serverData[i].name, value: serverData[i].value}); - } else if (this.getLocalVarByName(serverData[i].name)) { - // The variable already exists but in another position - // It is changed so we need to keep the old values - oldIndex = this.getLocalIndexByName(serverData[i].name); - var oldValue = this.variables[oldIndex].value; - this.variables.splice(oldIndex, 1); - this.variables.splice(i, 0, {name: serverData[i].name, value: oldValue}); - } else { - // The variable does not exists anywhere else. Just add it - this.variables.splice(i, 0, {name: serverData[i].name, value: serverData[i].value}); - } - } else if (this.variables[i] && this.variables[i].name === serverData[i].name) { - // Variable exists already locally - // Update value if variable does not have any local changes - if (!this.variables[i].dirty) { - this.variables[i].value = serverData[i].value; + if (serverData.length === 0) { + this.variables = []; + } else { + for (var i = 0; i < serverData.length; i++) { + var oldIndex; + if (this.variables[i] && this.variables[i].name !== serverData[i].name) { + if (!this.getServerVarByName(this.variables[i].name)) { + // This variable does not exists anymore on the server. Remove it + this.variables.splice(i, 1); + } else if (this.getLocalVarByName(serverData[i].name) && !this.getLocalVarByName(serverData[i].name).dirty) { + // The variable already exists but in another position + // It is not changed so we can just remove it + oldIndex = this.getLocalIndexByName(serverData[i].name); + this.variables.splice(oldIndex, 1); + this.variables.splice(i, 0, {name: serverData[i].name, value: serverData[i].value}); + } else if (this.getLocalVarByName(serverData[i].name)) { + // The variable already exists but in another position + // It is changed so we need to keep the old values + oldIndex = this.getLocalIndexByName(serverData[i].name); + var oldValue = this.variables[oldIndex].value; + this.variables.splice(oldIndex, 1); + this.variables.splice(i, 0, {name: serverData[i].name, value: oldValue}); + } else { + // The variable does not exists anywhere else. Just add it + this.variables.splice(i, 0, {name: serverData[i].name, value: serverData[i].value}); + } + } else if (this.variables[i] && this.variables[i].name === serverData[i].name) { + // Variable exists already locally + // Update value if variable does not have any local changes + if (!this.variables[i].dirty) { + this.variables[i].value = serverData[i].value; + } + } else if (!this.variables[i]) { + // Add new local variable + this.variables.push({name: serverData[i].name, value: serverData[i].value}); } - } else if (!this.variables[i]) { - // Add new local variable - this.variables.push({name: serverData[i].name, value: serverData[i].value}); } } }; @@ -150,11 +154,13 @@ $rootScope.$watch(function() { return Styleguide.config.data; }, function(newValue) { - if (newValue) { + if (newValue && newValue.settings) { serverData = newValue.settings; - _this.refreshValues(); - _this.refreshDirtyStates(); + } else { + serverData = []; } + _this.refreshValues(); + _this.refreshDirtyStates(); }); }; diff --git a/test/angular/services/Variables.test.js b/test/angular/services/Variables.test.js index e8f01ff7..a996b038 100644 --- a/test/angular/services/Variables.test.js +++ b/test/angular/services/Variables.test.js @@ -48,6 +48,22 @@ describe('Service: Variables', function() { ]); }); + it('should set empty array as server data if variables do not exists', function() { + rootScope.$digest(); + styleguideMock.config.data = {}; + rootScope.$digest(); + expect(Variables.variables).to.eql([]); + }); + + it('should set empty array as server data no vairables are found', function() { + rootScope.$digest(); + styleguideMock.config.data = { + settings: [] + }; + rootScope.$digest(); + expect(Variables.variables).to.eql([]); + }); + it('should not mark server side changes as dirty', function() { rootScope.$digest(); styleguideMock.config.data = {