Karma to Jest: history_ctrl. .gitingore: .vs/

This commit is contained in:
Tobias Skarhed 2018-06-21 11:54:47 +02:00
parent d00e2c20cf
commit 4c31173853
3 changed files with 45 additions and 91 deletions

1
.gitignore vendored
View File

@ -33,6 +33,7 @@ public/css/*.min.css
*.tmp *.tmp
.DS_Store .DS_Store
.vscode/ .vscode/
.vs/
/data/* /data/*
/bin/* /bin/*

View File

@ -113,7 +113,6 @@ export class HistoryListCtrl {
.calculateDiff(options) .calculateDiff(options)
.then(response => { .then(response => {
this.delta[this.diff] = response; this.delta[this.diff] = response;
console.log('SUCCESSS!!!');
}) })
.catch(() => { .catch(() => {
this.mode = 'list'; this.mode = 'list';
@ -160,12 +159,10 @@ export class HistoryListCtrl {
this.delta = { basic: '', json: '' }; this.delta = { basic: '', json: '' };
this.diff = 'basic'; this.diff = 'basic';
this.mode = 'list'; this.mode = 'list';
//console.log(this.revisions);
this.revisions = _.map(this.revisions, rev => _.extend({}, rev, { checked: false })); this.revisions = _.map(this.revisions, rev => _.extend({}, rev, { checked: false }));
this.canCompare = false; this.canCompare = false;
this.start = 0; this.start = 0;
this.isNewLatest = false; this.isNewLatest = false;
//console.log(this.revisions);
} }
resetFromSource() { resetFromSource() {

View File

@ -1,5 +1,3 @@
//import { describe, beforeEach, it, sinon, expect, angularMocks } from 'test/lib/common';
import _ from 'lodash'; import _ from 'lodash';
import { HistoryListCtrl } from 'app/features/dashboard/history/history'; import { HistoryListCtrl } from 'app/features/dashboard/history/history';
import { versions, compare, restore } from './history_mocks'; import { versions, compare, restore } from './history_mocks';
@ -12,20 +10,11 @@ describe('HistoryListCtrl', () => {
restore(7, RESTORE_ID); restore(7, RESTORE_ID);
//beforeEach(angularMocks.module('grafana.core'));
//beforeEach(angularMocks.module('grafana.services'));
// beforeEach(
// angularMocks.inject($rootScope => {
// ctx.scope = $rootScope.$new();
// })
// );
let historySrv; let historySrv;
let $rootScope; let $rootScope;
let historyListCtrl; let historyListCtrl;
beforeEach(() => { beforeEach(() => {
historySrv = { historySrv = {
// getHistoryList: jest.fn( ()=> $q.when(versionsResponse)),
calculateDiff: jest.fn(), calculateDiff: jest.fn(),
restoreDashboard: jest.fn(() => $q.when({})), restoreDashboard: jest.fn(() => $q.when({})),
}; };
@ -33,8 +22,6 @@ describe('HistoryListCtrl', () => {
appEvent: jest.fn(), appEvent: jest.fn(),
onAppEvent: jest.fn(), onAppEvent: jest.fn(),
}; };
// historyListCtrl = new HistoryListCtrl({},$rootScope,{},$q,historySrv, {});
}); });
describe('when the history list component is loaded', () => { describe('when the history list component is loaded', () => {
@ -59,8 +46,6 @@ describe('HistoryListCtrl', () => {
describe('and the history list is successfully fetched', () => { describe('and the history list is successfully fetched', () => {
beforeEach(async () => { beforeEach(async () => {
//deferred.resolve(versionsResponse);
//historyListCtrl.$scope.$apply();
deferred.resolve(versionsResponse); deferred.resolve(versionsResponse);
await historyListCtrl.getLog(); await historyListCtrl.getLog();
}); });
@ -73,27 +58,27 @@ describe('HistoryListCtrl', () => {
expect(_.find(historyListCtrl.revisions, rev => rev.checked)).toBe(undefined); expect(_.find(historyListCtrl.revisions, rev => rev.checked)).toBe(undefined);
}); });
it('should indicate loading has finished', function() { it('should indicate loading has finished', () => {
expect(historyListCtrl.loading).toBe(false); expect(historyListCtrl.loading).toBe(false);
}); });
it('should store the revisions sorted desc by version id', function() { it('should store the revisions sorted desc by version id', () => {
expect(historyListCtrl.revisions[0].version).toBe(4); expect(historyListCtrl.revisions[0].version).toBe(4);
expect(historyListCtrl.revisions[1].version).toBe(3); expect(historyListCtrl.revisions[1].version).toBe(3);
expect(historyListCtrl.revisions[2].version).toBe(2); expect(historyListCtrl.revisions[2].version).toBe(2);
expect(historyListCtrl.revisions[3].version).toBe(1); expect(historyListCtrl.revisions[3].version).toBe(1);
}); });
it('should add a checked property to each revision', function() { it('should add a checked property to each revision', () => {
var actual = _.filter(historyListCtrl.revisions, rev => rev.hasOwnProperty('checked')); let actual = _.filter(historyListCtrl.revisions, rev => rev.hasOwnProperty('checked'));
expect(actual.length).toBe(4); expect(actual.length).toBe(4);
}); });
it('should set all checked properties to false on reset', function() { it('should set all checked properties to false on reset', () => {
historyListCtrl.revisions[0].checked = true; historyListCtrl.revisions[0].checked = true;
historyListCtrl.revisions[2].checked = true; historyListCtrl.revisions[2].checked = true;
historyListCtrl.reset(); historyListCtrl.reset();
var actual = _.filter(historyListCtrl.revisions, rev => !rev.checked); let actual = _.filter(historyListCtrl.revisions, rev => !rev.checked);
expect(actual.length).toBe(4); expect(actual.length).toBe(4);
}); });
}); });
@ -107,71 +92,45 @@ describe('HistoryListCtrl', () => {
historyListCtrl = new HistoryListCtrl({}, $rootScope, {}, $q, historySrv, {}); historyListCtrl = new HistoryListCtrl({}, $rootScope, {}, $q, historySrv, {});
deferred.reject(new Error('HistoryListError')); deferred.reject(new Error('HistoryListError'));
//historyListCtrl.$scope.$apply();
await historyListCtrl.getLog(); await historyListCtrl.getLog();
}); });
it("should reset the controller's state", function() { it("should reset the controller's state", () => {
expect(historyListCtrl.mode).toBe('list'); expect(historyListCtrl.mode).toBe('list');
expect(historyListCtrl.delta).toEqual({ basic: '', json: '' }); expect(historyListCtrl.delta).toEqual({ basic: '', json: '' });
expect(_.find(historyListCtrl.revisions, rev => rev.checked)).toBe(undefined); expect(_.find(historyListCtrl.revisions, rev => rev.checked)).toBe(undefined);
}); });
it('should indicate loading has finished', function() { it('should indicate loading has finished', () => {
expect(historyListCtrl.loading).toBe(false); expect(historyListCtrl.loading).toBe(false);
}); });
it('should have an empty revisions list', function() { it('should have an empty revisions list', () => {
expect(historyListCtrl.revisions).toEqual([]); expect(historyListCtrl.revisions).toEqual([]);
}); });
}); });
describe('should update the history list when the dashboard is saved', function() { describe('should update the history list when the dashboard is saved', () => {
beforeEach(() => { beforeEach(() => {
historyListCtrl.dashboard = { version: 3 }; historyListCtrl.dashboard = { version: 3 };
historyListCtrl.resetFromSource = jest.fn(); historyListCtrl.resetFromSource = jest.fn();
}); });
it('should listen for the `dashboard-saved` appEvent', function() { it('should listen for the `dashboard-saved` appEvent', () => {
expect($rootScope.onAppEvent).toHaveBeenCalledTimes(1); expect($rootScope.onAppEvent).toHaveBeenCalledTimes(1);
expect($rootScope.onAppEvent.mock.calls[0][0]).toBe('dashboard-saved'); expect($rootScope.onAppEvent.mock.calls[0][0]).toBe('dashboard-saved');
}); });
it('should call `onDashboardSaved` when the appEvent is received', function() { it('should call `onDashboardSaved` when the appEvent is received', () => {
expect($rootScope.onAppEvent.mock.calls[0][1]).not.toBe(historyListCtrl.onDashboardSaved); expect($rootScope.onAppEvent.mock.calls[0][1]).not.toBe(historyListCtrl.onDashboardSaved);
expect($rootScope.onAppEvent.mock.calls[0][1].toString).toBe(historyListCtrl.onDashboardSaved.toString); expect($rootScope.onAppEvent.mock.calls[0][1].toString).toBe(historyListCtrl.onDashboardSaved.toString);
}); });
}); });
}); });
describe('when the user wants to compare two revisions', function() { describe('when the user wants to compare two revisions', () => {
var deferred; let deferred;
// beforeEach(
// angularMocks.inject(($controller, $q) => {
// deferred = $q.defer();
// historySrv.getHistoryList.returns($q.when(versionsResponse));
// historySrv.calculateDiff.returns(deferred.promise);
// historyListCtrl = $controller(
// HistoryListCtrl,
// {
// historySrv,
// $rootScope,
// $scope: ctx.scope,
// },
// {
// dashboard: {
// id: 2,
// version: 3,
// formatDate: sinon.stub().returns('date'),
// },
// }
// );
// historyListCtrl.$scope.onDashboardSaved = sinon.spy();
// historyListCtrl.$scope.$apply();
// })
// );
beforeEach(async () => { beforeEach(async () => {
deferred = $q.defer({}); deferred = $q.defer({});
@ -190,12 +149,12 @@ describe('HistoryListCtrl', () => {
await historyListCtrl.getLog(); await historyListCtrl.getLog();
}); });
it('should have already fetched the history list', function() { it('should have already fetched the history list', () => {
expect(historySrv.getHistoryList).toHaveBeenCalledTimes(1); expect(historySrv.getHistoryList).toHaveBeenCalled();
expect(historyListCtrl.revisions.length).toBeGreaterThan(0); expect(historyListCtrl.revisions.length).toBeGreaterThan(0);
}); });
it('should check that two valid versions are selected', function() { it('should check that two valid versions are selected', () => {
// [] // []
expect(historyListCtrl.canCompare).toBe(false); expect(historyListCtrl.canCompare).toBe(false);
@ -210,58 +169,59 @@ describe('HistoryListCtrl', () => {
expect(historyListCtrl.canCompare).toBe(true); expect(historyListCtrl.canCompare).toBe(true);
}); });
describe('and the basic diff is successfully fetched', function() { describe('and the basic diff is successfully fetched', () => {
beforeEach(() => { beforeEach(async () => {
//deferred = $q.defer(); deferred = $q.defer({});
historySrv.calculateDiff = jest.fn(() => deferred.promise);
deferred.resolve(compare('basic')); deferred.resolve(compare('basic'));
historyListCtrl.revisions[1].checked = true; historyListCtrl.revisions[1].checked = true;
historyListCtrl.revisions[3].checked = true; historyListCtrl.revisions[3].checked = true;
historyListCtrl.getDiff('basic'); await historyListCtrl.getDiff('basic');
//historyListCtrl.$scope.$apply();
}); });
it('should fetch the basic diff if two valid versions are selected', function() { it('should fetch the basic diff if two valid versions are selected', () => {
expect(historySrv.calculateDiff).toHaveBeenCalledTimes(1); expect(historySrv.calculateDiff).toHaveBeenCalledTimes(1);
expect(historyListCtrl.delta.basic).toBe('<div></div>'); expect(historyListCtrl.delta.basic).toBe('<div></div>');
expect(historyListCtrl.delta.json).toBe(''); expect(historyListCtrl.delta.json).toBe('');
}); });
it('should set the basic diff view as active', function() { it('should set the basic diff view as active', () => {
expect(historyListCtrl.mode).toBe('compare'); expect(historyListCtrl.mode).toBe('compare');
expect(historyListCtrl.diff).toBe('basic'); expect(historyListCtrl.diff).toBe('basic');
}); });
it('should indicate loading has finished', function() { it('should indicate loading has finished', () => {
expect(historyListCtrl.loading).toBe(false); expect(historyListCtrl.loading).toBe(false);
}); });
}); });
describe('and the json diff is successfully fetched', function() { describe('and the json diff is successfully fetched', () => {
beforeEach(() => { beforeEach(async () => {
deferred = $q.defer({});
historySrv.calculateDiff = jest.fn(() => deferred.promise);
deferred.resolve(compare('json')); deferred.resolve(compare('json'));
historyListCtrl.revisions[1].checked = true; historyListCtrl.revisions[1].checked = true;
historyListCtrl.revisions[3].checked = true; historyListCtrl.revisions[3].checked = true;
historyListCtrl.getDiff('json'); await historyListCtrl.getDiff('json');
//historyListCtrl.$scope.$apply();
}); });
it('should fetch the json diff if two valid versions are selected', function() { it('should fetch the json diff if two valid versions are selected', () => {
expect(historySrv.calculateDiff.calledOnce).toBe(true); expect(historySrv.calculateDiff).toHaveBeenCalledTimes(1);
expect(historyListCtrl.delta.basic).toBe(''); expect(historyListCtrl.delta.basic).toBe('');
expect(historyListCtrl.delta.json).toBe('<pre><code></code></pre>'); expect(historyListCtrl.delta.json).toBe('<pre><code></code></pre>');
}); });
it('should set the json diff view as active', function() { it('should set the json diff view as active', () => {
expect(historyListCtrl.mode).toBe('compare'); expect(historyListCtrl.mode).toBe('compare');
expect(historyListCtrl.diff).toBe('json'); expect(historyListCtrl.diff).toBe('json');
}); });
it('should indicate loading has finished', function() { it('should indicate loading has finished', () => {
expect(historyListCtrl.loading).toBe(false); expect(historyListCtrl.loading).toBe(false);
}); });
}); });
describe('and diffs have already been fetched', function() { describe('and diffs have already been fetched', () => {
beforeEach(async () => { beforeEach(async () => {
deferred.resolve(compare('basic')); deferred.resolve(compare('basic'));
@ -269,14 +229,15 @@ describe('HistoryListCtrl', () => {
historyListCtrl.revisions[1].checked = true; historyListCtrl.revisions[1].checked = true;
historyListCtrl.delta.basic = 'cached basic'; historyListCtrl.delta.basic = 'cached basic';
historyListCtrl.getDiff('basic'); historyListCtrl.getDiff('basic');
await historySrv.calculateDiff();
}); });
it('should use the cached diffs instead of fetching', function() { it('should use the cached diffs instead of fetching', () => {
expect(historySrv.calculateDiff.calledOnce).toBe(false); expect(historySrv.calculateDiff).toHaveBeenCalledTimes(1);
expect(historyListCtrl.delta.basic).toBe('cached basic'); expect(historyListCtrl.delta.basic).toBe('cached basic');
}); });
it('should indicate loading has finished', function() { it('should indicate loading has finished', () => {
expect(historyListCtrl.loading).toBe(false); expect(historyListCtrl.loading).toBe(false);
}); });
}); });
@ -286,13 +247,10 @@ describe('HistoryListCtrl', () => {
deferred = $q.defer({}); deferred = $q.defer({});
historySrv.calculateDiff = jest.fn(() => deferred.promise); historySrv.calculateDiff = jest.fn(() => deferred.promise);
historyListCtrl = new HistoryListCtrl({}, $rootScope, {}, $q, historySrv, {});
//await historyListCtrl.getLog();
historyListCtrl.revisions[3].checked = true; historyListCtrl.revisions[3].checked = true;
historyListCtrl.revisions[1].checked = true; historyListCtrl.revisions[1].checked = true;
historyListCtrl.getDiff('basic'); deferred.reject();
deferred.reject(new Error('DiffError')); await historyListCtrl.getDiff('basic');
await historySrv.calculateDiff();
}); });
it('should fetch the diff if two valid versions are selected', () => { it('should fetch the diff if two valid versions are selected', () => {
@ -300,12 +258,10 @@ describe('HistoryListCtrl', () => {
}); });
it('should return to the history list view', () => { it('should return to the history list view', () => {
//FAILS
expect(historyListCtrl.mode).toBe('list'); expect(historyListCtrl.mode).toBe('list');
}); });
it('should indicate loading has finished', () => { it('should indicate loading has finished', () => {
//FAILS
expect(historyListCtrl.loading).toBe(false); expect(historyListCtrl.loading).toBe(false);
}); });
@ -315,8 +271,8 @@ describe('HistoryListCtrl', () => {
}); });
}); });
describe('when the user wants to restore a revision', function() { describe('when the user wants to restore a revision', () => {
var deferred; let deferred;
beforeEach(async () => { beforeEach(async () => {
deferred = $q.defer(); deferred = $q.defer();