From 14e8c15a3abffa1d52236d8fc5665d3c0fa73cdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Tue, 28 Apr 2015 16:42:40 +0200 Subject: [PATCH] Lots of new unit tests for unsaved changes service --- ' | 47 --------------- .../features/dashboard/unsavedChangesSrv.js | 59 ++++++++----------- public/test/specs/unsavedChangesSrv-specs.js | 38 +++++++++++- 3 files changed, 60 insertions(+), 84 deletions(-) delete mode 100644 ' diff --git a/' b/' deleted file mode 100644 index 2b78b586722..00000000000 --- a/' +++ /dev/null @@ -1,47 +0,0 @@ -define([ - 'features/dashboard/unsavedChangesSrv', - 'features/dashboard/dashboardSrv' -], function() { - 'use strict'; - - describe("unsavedChangesSrv", function() { - var _unsavedChangesSrv; - var _dashboardSrv; - var _location; - var _contextSrvStub = { - isEditor: true - }; - var _rootScope; - var tracker; - - beforeEach(module('grafana.services')); - beforeEach(module(function($provide) { - $provide.value('contextSrv', _contextSrvStub); - })); - - beforeEach(inject(function(unsavedChangesSrv, $location, $rootScope, dashboardSrv) { - _unsavedChangesSrv = unsavedChangesSrv; - _dashboardSrv = dashboardSrv; - _location = $location; - _rootScope = $rootScope; - })); - - describe('when dashboard is modified and route changes', function() { - - beforeEach(function() { - var dash = _dashboardSrv.create({}); - var scope = _rootScope.$new(); - scope.appEvent = sinon.spy(); - scope.onAppEvent = sinon.spy(); - tracker = _unsavedChangesSrv.constructor(dash, scope); - }); - - it('No changes should not have changes', function() { - expect(tracker.hasChanges()).to.be(false); - }); - - }); - - }); - -}); diff --git a/public/app/features/dashboard/unsavedChangesSrv.js b/public/app/features/dashboard/unsavedChangesSrv.js index 8c1862674e7..fe45fbe48ba 100644 --- a/public/app/features/dashboard/unsavedChangesSrv.js +++ b/public/app/features/dashboard/unsavedChangesSrv.js @@ -61,6 +61,12 @@ function(angular, _) { // remove stuff that should not count in diff p.cleanDashboardFromIgnoredChanges = function(dash) { + // ignore time and refresh + dash.time = 0; + dash.refresh = 0; + dash.version = 0; + + // filter row and panels properties that should be ignored dash.rows = _.filter(dash.rows, function(row) { if (row.repeatRowId) { return false; @@ -70,58 +76,39 @@ function(angular, _) { if (panel.repeatPanelId) { return false; } + // remove scopedVars panel.scopedVars = null; + + // ignore panel legend sort + if (panel.legend) { + delete panel.legend.sort; + delete panel.legend.sortDesc; + } + return true; }); + // ignore collapse state + row.collapse = false; return true; }); + + // ignore template variable values + _.each(dash.templating.list, function(value) { + value.current = null; + value.options = null; + }); + }; p.hasChanges = function() { var current = this.current.getSaveModelClone(); var original = this.original; - // ignore timespan changes - current.time = original.time = {}; - current.refresh = original.refresh; - // ignore version - current.version = original.version; - - // ignore template variable values - _.each(current.templating.list, function(value, index) { - value.current = null; - value.options = null; - - if (original.templating.list.length > index) { - original.templating.list[index].current = null; - original.templating.list[index].options = null; - } - }); - this.cleanDashboardFromIgnoredChanges(current); this.cleanDashboardFromIgnoredChanges(original); - // ignore some panel and row stuff - current.forEachPanel(function(panel, panelIndex, row, rowIndex) { - var originalRow = original.rows[rowIndex]; - var originalPanel = original.getPanelById(panel.id); - // ignore row collapse state - if (originalRow) { - row.collapse = originalRow.collapse; - } - if (originalPanel) { - // ignore graph legend sort - if (originalPanel.legend && panel.legend) { - delete originalPanel.legend.sortDesc; - delete originalPanel.legend.sort; - delete panel.legend.sort; - delete panel.legend.sortDesc; - } - } - }); - var currentTimepicker = _.findWhere(current.nav, { type: 'timepicker' }); var originalTimepicker = _.findWhere(original.nav, { type: 'timepicker' }); diff --git a/public/test/specs/unsavedChangesSrv-specs.js b/public/test/specs/unsavedChangesSrv-specs.js index 33691e17a0b..43cb3ab0e63 100644 --- a/public/test/specs/unsavedChangesSrv-specs.js +++ b/public/test/specs/unsavedChangesSrv-specs.js @@ -17,6 +17,7 @@ define([ beforeEach(module('grafana.services')); beforeEach(module(function($provide) { $provide.value('contextSrv', _contextSrvStub); + $provide.value('$window', {}); })); beforeEach(inject(function(unsavedChangesSrv, $location, $rootScope, dashboardSrv) { @@ -27,7 +28,13 @@ define([ })); beforeEach(function() { - dash = _dashboardSrv.create({}); + dash = _dashboardSrv.create({ + rows: [ + { + panels: [{ test: "asd", legend: { } }] + } + ] + }); scope = _rootScope.$new(); scope.appEvent = sinon.spy(); scope.onAppEvent = sinon.spy(); @@ -44,5 +51,34 @@ define([ expect(tracker.hasChanges()).to.be(true); }); + it('Should ignore a lot of changes', function() { + dash.time = {from: '1h'}; + dash.refresh = true; + dash.version = 10; + dash.rows[0].collapse = true; + expect(tracker.hasChanges()).to.be(false); + }); + + it('Should ignore row collapse change', function() { + dash.rows[0].collapse = true; + expect(tracker.hasChanges()).to.be(false); + }); + + it('Should ignore panel legend changes', function() { + dash.rows[0].panels[0].legend.sortDesc = true; + dash.rows[0].panels[0].legend.sort = "avg"; + expect(tracker.hasChanges()).to.be(false); + }); + + it('Should ignore panel repeats', function() { + dash.rows[0].panels.push({repeatPanelId: 10}); + expect(tracker.hasChanges()).to.be(false); + }); + + it('Should ignore row repeats', function() { + dash.rows.push({repeatRowId: 10}); + expect(tracker.hasChanges()).to.be(false); + }); + }); });