Ver Fonte

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

Tobias Skarhed há 7 anos atrás
pai
commit
4c31173853

+ 1 - 0
.gitignore

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

+ 0 - 3
public/app/features/dashboard/history/history.ts

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

+ 44 - 88
public/app/features/dashboard/specs/history_ctrl.jest.ts

@@ -1,5 +1,3 @@
-//import { describe, beforeEach, it, sinon, expect, angularMocks } from 'test/lib/common';
-
 import _ from 'lodash';
 import { HistoryListCtrl } from 'app/features/dashboard/history/history';
 import { versions, compare, restore } from './history_mocks';
@@ -12,20 +10,11 @@ describe('HistoryListCtrl', () => {
 
   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 $rootScope;
   let historyListCtrl;
   beforeEach(() => {
     historySrv = {
-      // getHistoryList: jest.fn( ()=> $q.when(versionsResponse)),
       calculateDiff: jest.fn(),
       restoreDashboard: jest.fn(() => $q.when({})),
     };
@@ -33,8 +22,6 @@ describe('HistoryListCtrl', () => {
       appEvent: jest.fn(),
       onAppEvent: jest.fn(),
     };
-
-    // historyListCtrl = new HistoryListCtrl({},$rootScope,{},$q,historySrv, {});
   });
 
   describe('when the history list component is loaded', () => {
@@ -59,8 +46,6 @@ describe('HistoryListCtrl', () => {
 
     describe('and the history list is successfully fetched', () => {
       beforeEach(async () => {
-        //deferred.resolve(versionsResponse);
-        //historyListCtrl.$scope.$apply();
         deferred.resolve(versionsResponse);
         await historyListCtrl.getLog();
       });
@@ -73,27 +58,27 @@ describe('HistoryListCtrl', () => {
         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);
       });
 
-      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[1].version).toBe(3);
         expect(historyListCtrl.revisions[2].version).toBe(2);
         expect(historyListCtrl.revisions[3].version).toBe(1);
       });
 
-      it('should add a checked property to each revision', function() {
-        var actual = _.filter(historyListCtrl.revisions, rev => rev.hasOwnProperty('checked'));
+      it('should add a checked property to each revision', () => {
+        let actual = _.filter(historyListCtrl.revisions, rev => rev.hasOwnProperty('checked'));
         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[2].checked = true;
         historyListCtrl.reset();
-        var actual = _.filter(historyListCtrl.revisions, rev => !rev.checked);
+        let actual = _.filter(historyListCtrl.revisions, rev => !rev.checked);
         expect(actual.length).toBe(4);
       });
     });
@@ -107,71 +92,45 @@ describe('HistoryListCtrl', () => {
         historyListCtrl = new HistoryListCtrl({}, $rootScope, {}, $q, historySrv, {});
 
         deferred.reject(new Error('HistoryListError'));
-        //historyListCtrl.$scope.$apply();
+
         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.delta).toEqual({ basic: '', json: '' });
         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);
       });
 
-      it('should have an empty revisions list', function() {
+      it('should have an empty revisions list', () => {
         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(() => {
         historyListCtrl.dashboard = { version: 3 };
         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.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].toString).toBe(historyListCtrl.onDashboardSaved.toString);
       });
     });
   });
 
-  describe('when the user wants to compare two revisions', function() {
-    var 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();
-    //   })
-    // );
+  describe('when the user wants to compare two revisions', () => {
+    let deferred;
 
     beforeEach(async () => {
       deferred = $q.defer({});
@@ -190,12 +149,12 @@ describe('HistoryListCtrl', () => {
       await historyListCtrl.getLog();
     });
 
-    it('should have already fetched the history list', function() {
-      expect(historySrv.getHistoryList).toHaveBeenCalledTimes(1);
+    it('should have already fetched the history list', () => {
+      expect(historySrv.getHistoryList).toHaveBeenCalled();
       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);
 
@@ -210,58 +169,59 @@ describe('HistoryListCtrl', () => {
       expect(historyListCtrl.canCompare).toBe(true);
     });
 
-    describe('and the basic diff is successfully fetched', function() {
-      beforeEach(() => {
-        //deferred = $q.defer();
+    describe('and the basic diff is successfully fetched', () => {
+      beforeEach(async () => {
+        deferred = $q.defer({});
+        historySrv.calculateDiff = jest.fn(() => deferred.promise);
         deferred.resolve(compare('basic'));
         historyListCtrl.revisions[1].checked = true;
         historyListCtrl.revisions[3].checked = true;
-        historyListCtrl.getDiff('basic');
-        //historyListCtrl.$scope.$apply();
+        await historyListCtrl.getDiff('basic');
       });
 
-      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(historyListCtrl.delta.basic).toBe('<div></div>');
         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.diff).toBe('basic');
       });
 
-      it('should indicate loading has finished', function() {
+      it('should indicate loading has finished', () => {
         expect(historyListCtrl.loading).toBe(false);
       });
     });
 
-    describe('and the json diff is successfully fetched', function() {
-      beforeEach(() => {
+    describe('and the json diff is successfully fetched', () => {
+      beforeEach(async () => {
+        deferred = $q.defer({});
+        historySrv.calculateDiff = jest.fn(() => deferred.promise);
         deferred.resolve(compare('json'));
         historyListCtrl.revisions[1].checked = true;
         historyListCtrl.revisions[3].checked = true;
-        historyListCtrl.getDiff('json');
-        //historyListCtrl.$scope.$apply();
+        await historyListCtrl.getDiff('json');
       });
 
-      it('should fetch the json diff if two valid versions are selected', function() {
-        expect(historySrv.calculateDiff.calledOnce).toBe(true);
+      it('should fetch the json diff if two valid versions are selected', () => {
+        expect(historySrv.calculateDiff).toHaveBeenCalledTimes(1);
         expect(historyListCtrl.delta.basic).toBe('');
         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.diff).toBe('json');
       });
 
-      it('should indicate loading has finished', function() {
+      it('should indicate loading has finished', () => {
         expect(historyListCtrl.loading).toBe(false);
       });
     });
 
-    describe('and diffs have already been fetched', function() {
+    describe('and diffs have already been fetched', () => {
       beforeEach(async () => {
         deferred.resolve(compare('basic'));
 
@@ -269,14 +229,15 @@ describe('HistoryListCtrl', () => {
         historyListCtrl.revisions[1].checked = true;
         historyListCtrl.delta.basic = 'cached basic';
         historyListCtrl.getDiff('basic');
+        await historySrv.calculateDiff();
       });
 
-      it('should use the cached diffs instead of fetching', function() {
-        expect(historySrv.calculateDiff.calledOnce).toBe(false);
+      it('should use the cached diffs instead of fetching', () => {
+        expect(historySrv.calculateDiff).toHaveBeenCalledTimes(1);
         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);
       });
     });
@@ -286,13 +247,10 @@ describe('HistoryListCtrl', () => {
         deferred = $q.defer({});
         historySrv.calculateDiff = jest.fn(() => deferred.promise);
 
-        historyListCtrl = new HistoryListCtrl({}, $rootScope, {}, $q, historySrv, {});
-        //await historyListCtrl.getLog();
         historyListCtrl.revisions[3].checked = true;
         historyListCtrl.revisions[1].checked = true;
-        historyListCtrl.getDiff('basic');
-        deferred.reject(new Error('DiffError'));
-        await historySrv.calculateDiff();
+        deferred.reject();
+        await historyListCtrl.getDiff('basic');
       });
 
       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', () => {
-        //FAILS
         expect(historyListCtrl.mode).toBe('list');
       });
 
       it('should indicate loading has finished', () => {
-        //FAILS
         expect(historyListCtrl.loading).toBe(false);
       });
 
@@ -315,8 +271,8 @@ describe('HistoryListCtrl', () => {
     });
   });
 
-  describe('when the user wants to restore a revision', function() {
-    var deferred;
+  describe('when the user wants to restore a revision', () => {
+    let deferred;
 
     beforeEach(async () => {
       deferred = $q.defer();