From d00e2c20cf3b7b54d5b619dfd2e6744a3657913d Mon Sep 17 00:00:00 2001 From: Tobias Skarhed Date: Wed, 20 Jun 2018 15:25:57 +0200 Subject: [PATCH 1/2] Karma to Jest: history_ctrl. Update version: ts-jest --- package.json | 2 +- .../app/features/dashboard/history/history.ts | 3 + .../dashboard/specs/history_ctrl.jest.ts | 357 ++++++++++++++++++ .../dashboard/specs/history_ctrl_specs.ts | 329 ---------------- yarn.lock | 82 +++- 5 files changed, 423 insertions(+), 350 deletions(-) create mode 100644 public/app/features/dashboard/specs/history_ctrl.jest.ts delete mode 100644 public/app/features/dashboard/specs/history_ctrl_specs.ts diff --git a/package.json b/package.json index df3da5812c1..c00494d452e 100644 --- a/package.json +++ b/package.json @@ -89,7 +89,7 @@ "style-loader": "^0.21.0", "systemjs": "0.20.19", "systemjs-plugin-css": "^0.1.36", - "ts-jest": "^22.0.0", + "ts-jest": "^22.4.6", "tslint": "^5.8.0", "tslint-loader": "^3.5.3", "typescript": "^2.6.2", diff --git a/public/app/features/dashboard/history/history.ts b/public/app/features/dashboard/history/history.ts index be6ad5af1ba..05a1e676b29 100644 --- a/public/app/features/dashboard/history/history.ts +++ b/public/app/features/dashboard/history/history.ts @@ -113,6 +113,7 @@ export class HistoryListCtrl { .calculateDiff(options) .then(response => { this.delta[this.diff] = response; + console.log('SUCCESSS!!!'); }) .catch(() => { this.mode = 'list'; @@ -159,10 +160,12 @@ 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() { diff --git a/public/app/features/dashboard/specs/history_ctrl.jest.ts b/public/app/features/dashboard/specs/history_ctrl.jest.ts new file mode 100644 index 00000000000..534416d9e2a --- /dev/null +++ b/public/app/features/dashboard/specs/history_ctrl.jest.ts @@ -0,0 +1,357 @@ +//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'; +import $q from 'q'; + +describe('HistoryListCtrl', () => { + const RESTORE_ID = 4; + + const versionsResponse: any = versions(); + + 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({})), + }; + $rootScope = { + appEvent: jest.fn(), + onAppEvent: jest.fn(), + }; + + // historyListCtrl = new HistoryListCtrl({},$rootScope,{},$q,historySrv, {}); + }); + + describe('when the history list component is loaded', () => { + let deferred; + + beforeEach(() => { + deferred = $q.defer({}); + historySrv.getHistoryList = jest.fn(() => deferred.promise); + + historyListCtrl = new HistoryListCtrl({}, $rootScope, {}, $q, historySrv, {}); + + historyListCtrl.dashboard = { + id: 2, + version: 3, + formatDate: jest.fn(() => 'date'), + }; + }); + + it('should immediately attempt to fetch the history list', () => { + expect(historySrv.getHistoryList).toHaveBeenCalledTimes(1); + }); + + describe('and the history list is successfully fetched', () => { + beforeEach(async () => { + //deferred.resolve(versionsResponse); + //historyListCtrl.$scope.$apply(); + deferred.resolve(versionsResponse); + await historyListCtrl.getLog(); + }); + + it("should reset the controller's state", async () => { + expect(historyListCtrl.mode).toBe('list'); + expect(historyListCtrl.delta).toEqual({ basic: '', json: '' }); + + expect(historyListCtrl.canCompare).toBe(false); + expect(_.find(historyListCtrl.revisions, rev => rev.checked)).toBe(undefined); + }); + + it('should indicate loading has finished', function() { + expect(historyListCtrl.loading).toBe(false); + }); + + it('should store the revisions sorted desc by version id', function() { + 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')); + expect(actual.length).toBe(4); + }); + + it('should set all checked properties to false on reset', function() { + historyListCtrl.revisions[0].checked = true; + historyListCtrl.revisions[2].checked = true; + historyListCtrl.reset(); + var actual = _.filter(historyListCtrl.revisions, rev => !rev.checked); + expect(actual.length).toBe(4); + }); + }); + + describe('and fetching the history list fails', () => { + beforeEach(async () => { + deferred = $q.defer(); + + historySrv.getHistoryList = jest.fn(() => deferred.promise); + + 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() { + 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() { + expect(historyListCtrl.loading).toBe(false); + }); + + it('should have an empty revisions list', function() { + expect(historyListCtrl.revisions).toEqual([]); + }); + }); + + describe('should update the history list when the dashboard is saved', function() { + beforeEach(() => { + historyListCtrl.dashboard = { version: 3 }; + historyListCtrl.resetFromSource = jest.fn(); + }); + + it('should listen for the `dashboard-saved` appEvent', function() { + 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() { + 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(); + // }) + // ); + + beforeEach(async () => { + deferred = $q.defer({}); + historySrv.getHistoryList = jest.fn(() => $q.when(versionsResponse)); + historySrv.calculateDiff = jest.fn(() => deferred.promise); + + historyListCtrl = new HistoryListCtrl({}, $rootScope, {}, $q, historySrv, {}); + + historyListCtrl.dashboard = { + id: 2, + version: 3, + formatDate: jest.fn(() => 'date'), + }; + + deferred.resolve(versionsResponse); + await historyListCtrl.getLog(); + }); + + it('should have already fetched the history list', function() { + expect(historySrv.getHistoryList).toHaveBeenCalledTimes(1); + expect(historyListCtrl.revisions.length).toBeGreaterThan(0); + }); + + it('should check that two valid versions are selected', function() { + // [] + expect(historyListCtrl.canCompare).toBe(false); + + // single value + historyListCtrl.revisions = [{ checked: true }]; + historyListCtrl.revisionSelectionChanged(); + expect(historyListCtrl.canCompare).toBe(false); + + // both values in range + historyListCtrl.revisions = [{ checked: true }, { checked: true }]; + historyListCtrl.revisionSelectionChanged(); + expect(historyListCtrl.canCompare).toBe(true); + }); + + describe('and the basic diff is successfully fetched', function() { + beforeEach(() => { + //deferred = $q.defer(); + deferred.resolve(compare('basic')); + historyListCtrl.revisions[1].checked = true; + historyListCtrl.revisions[3].checked = true; + historyListCtrl.getDiff('basic'); + //historyListCtrl.$scope.$apply(); + }); + + it('should fetch the basic diff if two valid versions are selected', function() { + expect(historySrv.calculateDiff).toHaveBeenCalledTimes(1); + expect(historyListCtrl.delta.basic).toBe('
'); + expect(historyListCtrl.delta.json).toBe(''); + }); + + it('should set the basic diff view as active', function() { + expect(historyListCtrl.mode).toBe('compare'); + expect(historyListCtrl.diff).toBe('basic'); + }); + + it('should indicate loading has finished', function() { + expect(historyListCtrl.loading).toBe(false); + }); + }); + + describe('and the json diff is successfully fetched', function() { + beforeEach(() => { + deferred.resolve(compare('json')); + historyListCtrl.revisions[1].checked = true; + historyListCtrl.revisions[3].checked = true; + historyListCtrl.getDiff('json'); + //historyListCtrl.$scope.$apply(); + }); + + it('should fetch the json diff if two valid versions are selected', function() { + expect(historySrv.calculateDiff.calledOnce).toBe(true); + expect(historyListCtrl.delta.basic).toBe(''); + expect(historyListCtrl.delta.json).toBe('
'); + }); + + it('should set the json diff view as active', function() { + expect(historyListCtrl.mode).toBe('compare'); + expect(historyListCtrl.diff).toBe('json'); + }); + + it('should indicate loading has finished', function() { + expect(historyListCtrl.loading).toBe(false); + }); + }); + + describe('and diffs have already been fetched', function() { + beforeEach(async () => { + deferred.resolve(compare('basic')); + + historyListCtrl.revisions[3].checked = true; + historyListCtrl.revisions[1].checked = true; + historyListCtrl.delta.basic = 'cached basic'; + historyListCtrl.getDiff('basic'); + }); + + it('should use the cached diffs instead of fetching', function() { + expect(historySrv.calculateDiff.calledOnce).toBe(false); + expect(historyListCtrl.delta.basic).toBe('cached basic'); + }); + + it('should indicate loading has finished', function() { + expect(historyListCtrl.loading).toBe(false); + }); + }); + + describe('and fetching the diff fails', () => { + beforeEach(async () => { + 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(); + }); + + it('should fetch the diff if two valid versions are selected', () => { + expect(historySrv.calculateDiff).toHaveBeenCalledTimes(1); + }); + + 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); + }); + + it('should have an empty delta/changeset', () => { + expect(historyListCtrl.delta).toEqual({ basic: '', json: '' }); + }); + }); + }); + + describe('when the user wants to restore a revision', function() { + var deferred; + + beforeEach(async () => { + deferred = $q.defer(); + historySrv.getHistoryList = jest.fn(() => $q.when(versionsResponse)); + historySrv.restoreDashboard = jest.fn(() => deferred.promise); + + historyListCtrl = new HistoryListCtrl({}, $rootScope, {}, $q, historySrv, {}); + + historyListCtrl.dashboard = { + id: 1, + }; + historyListCtrl.restore(); + deferred.resolve(versionsResponse); + await historyListCtrl.getLog(); + }); + + it('should display a modal allowing the user to restore or cancel', () => { + expect($rootScope.appEvent).toHaveBeenCalledTimes(1); + expect($rootScope.appEvent.mock.calls[0][0]).toBe('confirm-modal'); + }); + + describe('and restore fails to fetch', () => { + beforeEach(async () => { + deferred = $q.defer(); + historySrv.getHistoryList = jest.fn(() => $q.when(versionsResponse)); + historySrv.restoreDashboard = jest.fn(() => deferred.promise); + historyListCtrl = new HistoryListCtrl({}, $rootScope, {}, $q, historySrv, {}); + deferred.reject(new Error('RestoreError')); + historyListCtrl.restoreConfirm(RESTORE_ID); + await historyListCtrl.getLog(); + }); + + it('should indicate loading has finished', () => { + expect(historyListCtrl.loading).toBe(false); + }); + }); + }); +}); diff --git a/public/app/features/dashboard/specs/history_ctrl_specs.ts b/public/app/features/dashboard/specs/history_ctrl_specs.ts deleted file mode 100644 index cf5fb13b6c1..00000000000 --- a/public/app/features/dashboard/specs/history_ctrl_specs.ts +++ /dev/null @@ -1,329 +0,0 @@ -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'; - -describe('HistoryListCtrl', function() { - var RESTORE_ID = 4; - - var ctx: any = {}; - var versionsResponse: any = versions(); - - restore(7, RESTORE_ID); - - beforeEach(angularMocks.module('grafana.core')); - beforeEach(angularMocks.module('grafana.services')); - beforeEach( - angularMocks.inject($rootScope => { - ctx.scope = $rootScope.$new(); - }) - ); - - var historySrv; - var $rootScope; - beforeEach(function() { - historySrv = { - getHistoryList: sinon.stub(), - calculateDiff: sinon.stub(), - restoreDashboard: sinon.stub(), - }; - $rootScope = { - appEvent: sinon.spy(), - onAppEvent: sinon.spy(), - }; - }); - - describe('when the history list component is loaded', function() { - var deferred; - - beforeEach( - angularMocks.inject(($controller, $q) => { - deferred = $q.defer(); - historySrv.getHistoryList.returns(deferred.promise); - ctx.ctrl = $controller( - HistoryListCtrl, - { - historySrv, - $rootScope, - $scope: ctx.scope, - }, - { - dashboard: { - id: 2, - version: 3, - formatDate: sinon.stub().returns('date'), - }, - } - ); - }) - ); - - it('should immediately attempt to fetch the history list', function() { - expect(historySrv.getHistoryList.calledOnce).to.be(true); - }); - - describe('and the history list is successfully fetched', function() { - beforeEach(function() { - deferred.resolve(versionsResponse); - ctx.ctrl.$scope.$apply(); - }); - - it("should reset the controller's state", function() { - expect(ctx.ctrl.mode).to.be('list'); - expect(ctx.ctrl.delta).to.eql({ basic: '', json: '' }); - expect(ctx.ctrl.canCompare).to.be(false); - expect(_.find(ctx.ctrl.revisions, rev => rev.checked)).to.be(undefined); - }); - - it('should indicate loading has finished', function() { - expect(ctx.ctrl.loading).to.be(false); - }); - - it('should store the revisions sorted desc by version id', function() { - expect(ctx.ctrl.revisions[0].version).to.be(4); - expect(ctx.ctrl.revisions[1].version).to.be(3); - expect(ctx.ctrl.revisions[2].version).to.be(2); - expect(ctx.ctrl.revisions[3].version).to.be(1); - }); - - it('should add a checked property to each revision', function() { - var actual = _.filter(ctx.ctrl.revisions, rev => rev.hasOwnProperty('checked')); - expect(actual.length).to.be(4); - }); - - it('should set all checked properties to false on reset', function() { - ctx.ctrl.revisions[0].checked = true; - ctx.ctrl.revisions[2].checked = true; - ctx.ctrl.reset(); - var actual = _.filter(ctx.ctrl.revisions, rev => !rev.checked); - expect(actual.length).to.be(4); - }); - }); - - describe('and fetching the history list fails', function() { - beforeEach(function() { - deferred.reject(new Error('HistoryListError')); - ctx.ctrl.$scope.$apply(); - }); - - it("should reset the controller's state", function() { - expect(ctx.ctrl.mode).to.be('list'); - expect(ctx.ctrl.delta).to.eql({ basic: '', json: '' }); - expect(_.find(ctx.ctrl.revisions, rev => rev.checked)).to.be(undefined); - }); - - it('should indicate loading has finished', function() { - expect(ctx.ctrl.loading).to.be(false); - }); - - it('should have an empty revisions list', function() { - expect(ctx.ctrl.revisions).to.eql([]); - }); - }); - - describe('should update the history list when the dashboard is saved', function() { - beforeEach(function() { - ctx.ctrl.dashboard = { version: 3 }; - ctx.ctrl.resetFromSource = sinon.spy(); - }); - - it('should listen for the `dashboard-saved` appEvent', function() { - expect($rootScope.onAppEvent.calledOnce).to.be(true); - expect($rootScope.onAppEvent.getCall(0).args[0]).to.be('dashboard-saved'); - }); - - it('should call `onDashboardSaved` when the appEvent is received', function() { - expect($rootScope.onAppEvent.getCall(0).args[1]).to.not.be(ctx.ctrl.onDashboardSaved); - expect($rootScope.onAppEvent.getCall(0).args[1].toString).to.be(ctx.ctrl.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); - ctx.ctrl = $controller( - HistoryListCtrl, - { - historySrv, - $rootScope, - $scope: ctx.scope, - }, - { - dashboard: { - id: 2, - version: 3, - formatDate: sinon.stub().returns('date'), - }, - } - ); - - ctx.ctrl.$scope.onDashboardSaved = sinon.spy(); - ctx.ctrl.$scope.$apply(); - }) - ); - - it('should have already fetched the history list', function() { - expect(historySrv.getHistoryList.calledOnce).to.be(true); - expect(ctx.ctrl.revisions.length).to.be.above(0); - }); - - it('should check that two valid versions are selected', function() { - // [] - expect(ctx.ctrl.canCompare).to.be(false); - - // single value - ctx.ctrl.revisions = [{ checked: true }]; - ctx.ctrl.revisionSelectionChanged(); - expect(ctx.ctrl.canCompare).to.be(false); - - // both values in range - ctx.ctrl.revisions = [{ checked: true }, { checked: true }]; - ctx.ctrl.revisionSelectionChanged(); - expect(ctx.ctrl.canCompare).to.be(true); - }); - - describe('and the basic diff is successfully fetched', function() { - beforeEach(function() { - deferred.resolve(compare('basic')); - ctx.ctrl.revisions[1].checked = true; - ctx.ctrl.revisions[3].checked = true; - ctx.ctrl.getDiff('basic'); - ctx.ctrl.$scope.$apply(); - }); - - it('should fetch the basic diff if two valid versions are selected', function() { - expect(historySrv.calculateDiff.calledOnce).to.be(true); - expect(ctx.ctrl.delta.basic).to.be('
'); - expect(ctx.ctrl.delta.json).to.be(''); - }); - - it('should set the basic diff view as active', function() { - expect(ctx.ctrl.mode).to.be('compare'); - expect(ctx.ctrl.diff).to.be('basic'); - }); - - it('should indicate loading has finished', function() { - expect(ctx.ctrl.loading).to.be(false); - }); - }); - - describe('and the json diff is successfully fetched', function() { - beforeEach(function() { - deferred.resolve(compare('json')); - ctx.ctrl.revisions[1].checked = true; - ctx.ctrl.revisions[3].checked = true; - ctx.ctrl.getDiff('json'); - ctx.ctrl.$scope.$apply(); - }); - - it('should fetch the json diff if two valid versions are selected', function() { - expect(historySrv.calculateDiff.calledOnce).to.be(true); - expect(ctx.ctrl.delta.basic).to.be(''); - expect(ctx.ctrl.delta.json).to.be('
'); - }); - - it('should set the json diff view as active', function() { - expect(ctx.ctrl.mode).to.be('compare'); - expect(ctx.ctrl.diff).to.be('json'); - }); - - it('should indicate loading has finished', function() { - expect(ctx.ctrl.loading).to.be(false); - }); - }); - - describe('and diffs have already been fetched', function() { - beforeEach(function() { - deferred.resolve(compare('basic')); - ctx.ctrl.revisions[3].checked = true; - ctx.ctrl.revisions[1].checked = true; - ctx.ctrl.delta.basic = 'cached basic'; - ctx.ctrl.getDiff('basic'); - ctx.ctrl.$scope.$apply(); - }); - - it('should use the cached diffs instead of fetching', function() { - expect(historySrv.calculateDiff.calledOnce).to.be(false); - expect(ctx.ctrl.delta.basic).to.be('cached basic'); - }); - - it('should indicate loading has finished', function() { - expect(ctx.ctrl.loading).to.be(false); - }); - }); - - describe('and fetching the diff fails', function() { - beforeEach(function() { - deferred.reject(new Error('DiffError')); - ctx.ctrl.revisions[3].checked = true; - ctx.ctrl.revisions[1].checked = true; - ctx.ctrl.getDiff('basic'); - ctx.ctrl.$scope.$apply(); - }); - - it('should fetch the diff if two valid versions are selected', function() { - expect(historySrv.calculateDiff.calledOnce).to.be(true); - }); - - it('should return to the history list view', function() { - expect(ctx.ctrl.mode).to.be('list'); - }); - - it('should indicate loading has finished', function() { - expect(ctx.ctrl.loading).to.be(false); - }); - - it('should have an empty delta/changeset', function() { - expect(ctx.ctrl.delta).to.eql({ basic: '', json: '' }); - }); - }); - }); - - describe('when the user wants to restore a revision', function() { - var deferred; - - beforeEach( - angularMocks.inject(($controller, $q) => { - deferred = $q.defer(); - historySrv.getHistoryList.returns($q.when(versionsResponse)); - historySrv.restoreDashboard.returns(deferred.promise); - ctx.ctrl = $controller(HistoryListCtrl, { - historySrv, - contextSrv: { user: { name: 'Carlos' } }, - $rootScope, - $scope: ctx.scope, - }); - ctx.ctrl.dashboard = { id: 1 }; - ctx.ctrl.restore(); - ctx.ctrl.$scope.$apply(); - }) - ); - - it('should display a modal allowing the user to restore or cancel', function() { - expect($rootScope.appEvent.calledOnce).to.be(true); - expect($rootScope.appEvent.calledWith('confirm-modal')).to.be(true); - }); - - describe('and restore fails to fetch', function() { - beforeEach(function() { - deferred.reject(new Error('RestoreError')); - ctx.ctrl.restoreConfirm(RESTORE_ID); - try { - // this throws error, due to promise rejection - ctx.ctrl.$scope.$apply(); - } catch (e) {} - }); - - it('should indicate loading has finished', function() { - expect(ctx.ctrl.loading).to.be(false); - }); - }); - }); -}); diff --git a/yarn.lock b/yarn.lock index f58731040c6..2cd40a4f56c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -791,6 +791,30 @@ babel-core@^6.0.0, babel-core@^6.26.0: slash "^1.0.0" source-map "^0.5.6" +babel-core@^6.26.3: + version "6.26.3" + resolved "https://registry.yarnpkg.com/babel-core/-/babel-core-6.26.3.tgz#b2e2f09e342d0f0c88e2f02e067794125e75c207" + dependencies: + babel-code-frame "^6.26.0" + babel-generator "^6.26.0" + babel-helpers "^6.24.1" + babel-messages "^6.23.0" + babel-register "^6.26.0" + babel-runtime "^6.26.0" + babel-template "^6.26.0" + babel-traverse "^6.26.0" + babel-types "^6.26.0" + babylon "^6.18.0" + convert-source-map "^1.5.1" + debug "^2.6.9" + json5 "^0.5.1" + lodash "^4.17.4" + minimatch "^3.0.4" + path-is-absolute "^1.0.1" + private "^0.1.8" + slash "^1.0.0" + source-map "^0.5.7" + babel-generator@^6.18.0, babel-generator@^6.26.0: version "6.26.1" resolved "https://registry.yarnpkg.com/babel-generator/-/babel-generator-6.26.1.tgz#1844408d3b8f0d35a404ea7ac180f087a601bd90" @@ -898,7 +922,7 @@ babel-plugin-check-es2015-constants@^6.22.0: dependencies: babel-runtime "^6.22.0" -babel-plugin-istanbul@^4.1.4, babel-plugin-istanbul@^4.1.5: +babel-plugin-istanbul@^4.1.5, babel-plugin-istanbul@^4.1.6: version "4.1.6" resolved "https://registry.yarnpkg.com/babel-plugin-istanbul/-/babel-plugin-istanbul-4.1.6.tgz#36c59b2192efce81c5b378321b74175add1c9a45" dependencies: @@ -1003,7 +1027,7 @@ babel-plugin-transform-es2015-modules-amd@^6.24.1: babel-runtime "^6.22.0" babel-template "^6.24.1" -babel-plugin-transform-es2015-modules-commonjs@^6.24.1, babel-plugin-transform-es2015-modules-commonjs@^6.26.0: +babel-plugin-transform-es2015-modules-commonjs@^6.24.1: version "6.26.0" resolved "https://registry.yarnpkg.com/babel-plugin-transform-es2015-modules-commonjs/-/babel-plugin-transform-es2015-modules-commonjs-6.26.0.tgz#0d8394029b7dc6abe1a97ef181e00758dd2e5d8a" dependencies: @@ -1012,6 +1036,15 @@ babel-plugin-transform-es2015-modules-commonjs@^6.24.1, babel-plugin-transform-e babel-template "^6.26.0" babel-types "^6.26.0" +babel-plugin-transform-es2015-modules-commonjs@^6.26.2: + version "6.26.2" + resolved "https://registry.yarnpkg.com/babel-plugin-transform-es2015-modules-commonjs/-/babel-plugin-transform-es2015-modules-commonjs-6.26.2.tgz#58a793863a9e7ca870bdc5a881117ffac27db6f3" + dependencies: + babel-plugin-transform-strict-mode "^6.24.1" + babel-runtime "^6.26.0" + babel-template "^6.26.0" + babel-types "^6.26.0" + babel-plugin-transform-es2015-modules-systemjs@^6.24.1: version "6.24.1" resolved "https://registry.yarnpkg.com/babel-plugin-transform-es2015-modules-systemjs/-/babel-plugin-transform-es2015-modules-systemjs-6.24.1.tgz#ff89a142b9119a906195f5f106ecf305d9407d23" @@ -1137,7 +1170,7 @@ babel-preset-es2015@^6.24.1: babel-plugin-transform-es2015-unicode-regex "^6.24.1" babel-plugin-transform-regenerator "^6.24.1" -babel-preset-jest@^22.4.0, babel-preset-jest@^22.4.3: +babel-preset-jest@^22.4.3: version "22.4.3" resolved "https://registry.yarnpkg.com/babel-preset-jest/-/babel-preset-jest-22.4.3.tgz#e92eef9813b7026ab4ca675799f37419b5a44156" dependencies: @@ -2189,7 +2222,7 @@ content-type@~1.0.4: version "1.0.4" resolved "https://registry.yarnpkg.com/content-type/-/content-type-1.0.4.tgz#e138cc75e040c727b1966fe5e5f8c9aee256fe3b" -convert-source-map@^1.4.0, convert-source-map@^1.5.0: +convert-source-map@^1.4.0, convert-source-map@^1.5.0, convert-source-map@^1.5.1: version "1.5.1" resolved "https://registry.yarnpkg.com/convert-source-map/-/convert-source-map-1.5.1.tgz#b8278097b9bc229365de5c62cf5fcaed8b5599e5" @@ -2761,7 +2794,7 @@ debug@2.3.3: dependencies: ms "0.7.2" -debug@2.6.9, debug@^2.1.1, debug@^2.1.2, debug@^2.1.3, debug@^2.2.0, debug@^2.3.2, debug@^2.3.3, debug@^2.6.6, debug@^2.6.8: +debug@2.6.9, debug@^2.1.1, debug@^2.1.2, debug@^2.1.3, debug@^2.2.0, debug@^2.3.2, debug@^2.3.3, debug@^2.6.6, debug@^2.6.8, debug@^2.6.9: version "2.6.9" resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f" dependencies: @@ -4078,9 +4111,9 @@ fs-access@^1.0.0: dependencies: null-check "^1.0.0" -fs-extra@4.0.3: - version "4.0.3" - resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-4.0.3.tgz#0d852122e5bc5beb453fb028e9c0c9bf36340c94" +fs-extra@6.0.0: + version "6.0.0" + resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-6.0.0.tgz#0f0afb290bb3deb87978da816fcd3c7797f3a817" dependencies: graceful-fs "^4.1.2" jsonfile "^4.0.0" @@ -5689,7 +5722,7 @@ jest-cli@^22.4.3: which "^1.2.12" yargs "^10.0.3" -jest-config@^22.4.2, jest-config@^22.4.3: +jest-config@^22.4.3: version "22.4.3" resolved "https://registry.yarnpkg.com/jest-config/-/jest-config-22.4.3.tgz#0e9d57db267839ea31309119b41dc2fa31b76403" dependencies: @@ -6578,7 +6611,7 @@ lodash@^3.10.1, lodash@^3.5.0, lodash@^3.6.0, lodash@^3.7.0, lodash@^3.8.0, loda version "3.10.1" resolved "https://registry.yarnpkg.com/lodash/-/lodash-3.10.1.tgz#5bf45e8e49ba4189e17d482789dfd15bd140b7b6" -lodash@^4.0.0, lodash@^4.0.1, lodash@^4.1.1, lodash@^4.13.1, lodash@^4.14.0, lodash@^4.15.0, lodash@^4.17.2, lodash@^4.17.3, lodash@^4.17.4, lodash@^4.17.5, lodash@^4.3.0, lodash@^4.5.0, lodash@^4.7.0, lodash@^4.8.0, lodash@~4.17.4, lodash@~4.17.5: +lodash@^4.0.0, lodash@^4.0.1, lodash@^4.1.1, lodash@^4.13.1, lodash@^4.14.0, lodash@^4.15.0, lodash@^4.17.10, lodash@^4.17.2, lodash@^4.17.3, lodash@^4.17.4, lodash@^4.17.5, lodash@^4.3.0, lodash@^4.5.0, lodash@^4.7.0, lodash@^4.8.0, lodash@~4.17.4, lodash@~4.17.5: version "4.17.10" resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.10.tgz#1b7793cf7259ea38fb3661d4d38b3260af8ae4e7" @@ -8638,7 +8671,7 @@ prismjs@^1.6.0: optionalDependencies: clipboard "^2.0.0" -private@^0.1.6, private@^0.1.7, private@~0.1.5: +private@^0.1.6, private@^0.1.7, private@^0.1.8, private@~0.1.5: version "0.1.8" resolved "https://registry.yarnpkg.com/private/-/private-0.1.8.tgz#2381edb3689f7a53d653190060fcf822d2f368ff" @@ -10138,6 +10171,13 @@ source-map-support@^0.5.0, source-map-support@^0.5.3: buffer-from "^1.0.0" source-map "^0.6.0" +source-map-support@^0.5.5: + version "0.5.6" + resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.6.tgz#4435cee46b1aab62b8e8610ce60f788091c51c13" + dependencies: + buffer-from "^1.0.0" + source-map "^0.6.0" + source-map-url@^0.4.0: version "0.4.0" resolved "https://registry.yarnpkg.com/source-map-url/-/source-map-url-0.4.0.tgz#3e935d7ddd73631b97659956d55128e87b5084a3" @@ -10766,18 +10806,20 @@ tryor@~0.1.2: version "0.1.2" resolved "https://registry.yarnpkg.com/tryor/-/tryor-0.1.2.tgz#8145e4ca7caff40acde3ccf946e8b8bb75b4172b" -ts-jest@^22.0.0: - version "22.4.4" - resolved "https://registry.yarnpkg.com/ts-jest/-/ts-jest-22.4.4.tgz#7b5c0abb2188fe7170840df9f80e78659aaf8a24" +ts-jest@^22.4.6: + version "22.4.6" + resolved "https://registry.yarnpkg.com/ts-jest/-/ts-jest-22.4.6.tgz#a5d7f5e8b809626d1f4143209d301287472ec344" dependencies: - babel-core "^6.26.0" - babel-plugin-istanbul "^4.1.4" - babel-plugin-transform-es2015-modules-commonjs "^6.26.0" - babel-preset-jest "^22.4.0" + babel-core "^6.26.3" + babel-plugin-istanbul "^4.1.6" + babel-plugin-transform-es2015-modules-commonjs "^6.26.2" + babel-preset-jest "^22.4.3" cpx "^1.5.0" - fs-extra "4.0.3" - jest-config "^22.4.2" + fs-extra "6.0.0" + jest-config "^22.4.3" + lodash "^4.17.10" pkg-dir "^2.0.0" + source-map-support "^0.5.5" yargs "^11.0.0" tslib@^1.8.0, tslib@^1.8.1: From 4c31173853aed1f1826855b25e8d316b602f1597 Mon Sep 17 00:00:00 2001 From: Tobias Skarhed Date: Thu, 21 Jun 2018 11:54:47 +0200 Subject: [PATCH 2/2] Karma to Jest: history_ctrl. .gitingore: .vs/ --- .gitignore | 1 + .../app/features/dashboard/history/history.ts | 3 - .../dashboard/specs/history_ctrl.jest.ts | 132 ++++++------------ 3 files changed, 45 insertions(+), 91 deletions(-) diff --git a/.gitignore b/.gitignore index 45dcb52e8d8..25325b37890 100644 --- a/.gitignore +++ b/.gitignore @@ -33,6 +33,7 @@ public/css/*.min.css *.tmp .DS_Store .vscode/ +.vs/ /data/* /bin/* diff --git a/public/app/features/dashboard/history/history.ts b/public/app/features/dashboard/history/history.ts index 05a1e676b29..be6ad5af1ba 100644 --- a/public/app/features/dashboard/history/history.ts +++ b/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() { diff --git a/public/app/features/dashboard/specs/history_ctrl.jest.ts b/public/app/features/dashboard/specs/history_ctrl.jest.ts index 534416d9e2a..991ecb2c60d 100644 --- a/public/app/features/dashboard/specs/history_ctrl.jest.ts +++ b/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('
'); 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('
'); }); - 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();