Lots of new unit tests for unsaved changes service

This commit is contained in:
Torkel Ödegaard 2015-04-28 16:42:40 +02:00
parent aaea80e053
commit 14e8c15a3a
3 changed files with 60 additions and 84 deletions

47
'
View File

@ -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);
});
});
});
});

View File

@ -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' });

View File

@ -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);
});
});
});