From 5e9938ea643da8447165289ed853a5bb797b7c44 Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Tue, 27 Nov 2018 17:03:04 +0100 Subject: [PATCH] [ML] Wrap controller initialization in assertions. (#26265) - The controller tests introduced in #25382 had a flaw: If a controller initialization would fail and throw an error, that test suite wouldn't be able to clean up any stubs. So tests using the same stubs would report and error because the stubs couldn't be wrapped again. - This PR wraps every controller initialization inside an assertion and catches those errors properly as part of the test. --- .../__tests__/confirm_modal_controller.js | 13 ++++++++----- .../messagebar/__tests__/messagebar.js | 5 ++++- .../__tests__/datavisualizer_controller.js | 5 ++++- .../advanced/__tests__/new_job_controller.js | 5 ++++- .../detector_filter_modal_controller.js | 13 ++++++++----- .../__tests__/detector_modal_controller.js | 13 ++++++++----- .../__tests__/save_status_modal_controller.js | 13 ++++++++----- .../__tests__/create_job_controller.js | 5 ++++- .../__tests__/create_job_controller.js | 5 ++++- .../__tests__/create_job_controller.js | 19 +++++++++++-------- .../__tests__/create_job_controller.js | 19 +++++++++++-------- .../__tests__/index_or_search_controller.js | 19 +++++++++++-------- .../job_type/__tests__/job_type_controller.js | 5 ++++- .../lib/__tests__/angular_bootstrap_patch.js | 10 ++++++---- .../settings/__tests__/settings_controller.js | 5 ++++- .../__tests__/calendars_list_controller.js | 5 ++++- .../import_events_modal_controller.js | 13 ++++++++----- .../__tests__/new_event_modal_controller.js | 13 ++++++++----- .../__tests__/create_calendar_controller.js | 19 +++++++++++-------- .../timeseriesexplorer_controller.js | 5 ++++- 20 files changed, 134 insertions(+), 75 deletions(-) diff --git a/x-pack/plugins/ml/public/components/confirm_modal/__tests__/confirm_modal_controller.js b/x-pack/plugins/ml/public/components/confirm_modal/__tests__/confirm_modal_controller.js index c8cbe26632501..764b6ca2eaf06 100644 --- a/x-pack/plugins/ml/public/components/confirm_modal/__tests__/confirm_modal_controller.js +++ b/x-pack/plugins/ml/public/components/confirm_modal/__tests__/confirm_modal_controller.js @@ -19,11 +19,14 @@ describe('ML - Confirm Modal Controller', () => { it('Initialize Confirm Modal Controller', (done) => { ngMock.inject(function ($rootScope, $controller) { const scope = $rootScope.$new(); - $controller('MlConfirmModal', { - $scope: scope, - $modalInstance: mockModalInstance, - params: {} - }); + + expect(() => { + $controller('MlConfirmModal', { + $scope: scope, + $modalInstance: mockModalInstance, + params: {} + }); + }).to.not.throwError(); expect(scope.okLabel).to.be('OK'); done(); diff --git a/x-pack/plugins/ml/public/components/messagebar/__tests__/messagebar.js b/x-pack/plugins/ml/public/components/messagebar/__tests__/messagebar.js index b39cc57757eb9..2727d69e5ef3d 100644 --- a/x-pack/plugins/ml/public/components/messagebar/__tests__/messagebar.js +++ b/x-pack/plugins/ml/public/components/messagebar/__tests__/messagebar.js @@ -17,7 +17,10 @@ describe('ML - Message Bar Controller', () => { it('Initialize Message Bar Controller', (done) => { ngMock.inject(function ($rootScope, $controller) { const scope = $rootScope.$new(); - $controller('MlMessageBarController', { $scope: scope }); + + expect(() => { + $controller('MlMessageBarController', { $scope: scope }); + }).to.not.throwError(); expect(scope.messages).to.eql([]); done(); diff --git a/x-pack/plugins/ml/public/datavisualizer/__tests__/datavisualizer_controller.js b/x-pack/plugins/ml/public/datavisualizer/__tests__/datavisualizer_controller.js index 0a12a574f7949..8c7346b30f362 100644 --- a/x-pack/plugins/ml/public/datavisualizer/__tests__/datavisualizer_controller.js +++ b/x-pack/plugins/ml/public/datavisualizer/__tests__/datavisualizer_controller.js @@ -28,7 +28,10 @@ describe('ML - Data Visualizer View Fields Controller', () => { const stub2 = sinon.stub(indexUtils, 'timeBasedIndexCheck').callsFake(() => false); ngMock.inject(function ($rootScope, $controller) { const scope = $rootScope.$new(); - $controller('MlDataVisualizerViewFields', { $scope: scope }); + + expect(() => { + $controller('MlDataVisualizerViewFields', { $scope: scope }); + }).to.not.throwError(); expect(scope.metricCards).to.eql([]); stub1.restore(); diff --git a/x-pack/plugins/ml/public/jobs/new_job/advanced/__tests__/new_job_controller.js b/x-pack/plugins/ml/public/jobs/new_job/advanced/__tests__/new_job_controller.js index 8f4f41968ab0f..b06591ee7baa0 100644 --- a/x-pack/plugins/ml/public/jobs/new_job/advanced/__tests__/new_job_controller.js +++ b/x-pack/plugins/ml/public/jobs/new_job/advanced/__tests__/new_job_controller.js @@ -26,7 +26,10 @@ describe('ML - Advanced Job Wizard - New Job Controller', () => { ngMock.inject(function ($rootScope, $controller) { const scope = $rootScope.$new(); - $controller('MlNewJob', { $scope: scope }); + + expect(() => { + $controller('MlNewJob', { $scope: scope }); + }).to.not.throwError(); // This is just about initializing the controller and making sure // all angularjs based dependencies get loaded without error. diff --git a/x-pack/plugins/ml/public/jobs/new_job/advanced/detector_filter_modal/__tests__/detector_filter_modal_controller.js b/x-pack/plugins/ml/public/jobs/new_job/advanced/detector_filter_modal/__tests__/detector_filter_modal_controller.js index d617c0b2d2e47..8e7e31ac5afda 100644 --- a/x-pack/plugins/ml/public/jobs/new_job/advanced/detector_filter_modal/__tests__/detector_filter_modal_controller.js +++ b/x-pack/plugins/ml/public/jobs/new_job/advanced/detector_filter_modal/__tests__/detector_filter_modal_controller.js @@ -19,11 +19,14 @@ describe('ML - Detector Filter Modal Controller', () => { it('Initialize Detector Filter Modal Controller', (done) => { ngMock.inject(function ($rootScope, $controller) { const scope = $rootScope.$new(); - $controller('MlDetectorFilterModal', { - $scope: scope, - $modalInstance: mockModalInstance, - params: { detector: {} } - }); + + expect(() => { + $controller('MlDetectorFilterModal', { + $scope: scope, + $modalInstance: mockModalInstance, + params: { detector: {} } + }); + }).to.not.throwError(); expect(scope.title).to.eql('Add new filter'); done(); diff --git a/x-pack/plugins/ml/public/jobs/new_job/advanced/detector_modal/__tests__/detector_modal_controller.js b/x-pack/plugins/ml/public/jobs/new_job/advanced/detector_modal/__tests__/detector_modal_controller.js index 30226dc304f8c..235a8ba321d4c 100644 --- a/x-pack/plugins/ml/public/jobs/new_job/advanced/detector_modal/__tests__/detector_modal_controller.js +++ b/x-pack/plugins/ml/public/jobs/new_job/advanced/detector_modal/__tests__/detector_modal_controller.js @@ -19,11 +19,14 @@ describe('ML - Detector Modal Controller', () => { it('Initialize Detector Modal Controller', (done) => { ngMock.inject(function ($rootScope, $controller) { const scope = $rootScope.$new(); - $controller('MlDetectorModal', { - $scope: scope, - $modalInstance: mockModalInstance, - params: {} - }); + + expect(() => { + $controller('MlDetectorModal', { + $scope: scope, + $modalInstance: mockModalInstance, + params: {} + }); + }).to.not.throwError(); expect(scope.title).to.eql('Add new detector'); done(); diff --git a/x-pack/plugins/ml/public/jobs/new_job/advanced/save_status_modal/__tests__/save_status_modal_controller.js b/x-pack/plugins/ml/public/jobs/new_job/advanced/save_status_modal/__tests__/save_status_modal_controller.js index 7d0ec43015b37..97411067f41e1 100644 --- a/x-pack/plugins/ml/public/jobs/new_job/advanced/save_status_modal/__tests__/save_status_modal_controller.js +++ b/x-pack/plugins/ml/public/jobs/new_job/advanced/save_status_modal/__tests__/save_status_modal_controller.js @@ -19,11 +19,14 @@ describe('ML - Save Status Modal Controller', () => { it('Initialize Save Status Modal Controller', (done) => { ngMock.inject(function ($rootScope, $controller) { const scope = $rootScope.$new(); - $controller('MlSaveStatusModal', { - $scope: scope, - $modalInstance: mockModalInstance, - params: {} - }); + + expect(() => { + $controller('MlSaveStatusModal', { + $scope: scope, + $modalInstance: mockModalInstance, + params: {} + }); + }).to.not.throwError(); expect(scope.ui.showTimepicker).to.eql(false); done(); diff --git a/x-pack/plugins/ml/public/jobs/new_job/simple/multi_metric/create_job/__tests__/create_job_controller.js b/x-pack/plugins/ml/public/jobs/new_job/simple/multi_metric/create_job/__tests__/create_job_controller.js index 74031afc7a715..e00be354aaa72 100644 --- a/x-pack/plugins/ml/public/jobs/new_job/simple/multi_metric/create_job/__tests__/create_job_controller.js +++ b/x-pack/plugins/ml/public/jobs/new_job/simple/multi_metric/create_job/__tests__/create_job_controller.js @@ -30,7 +30,10 @@ describe('ML - Multi Metric Wizard - Create Job Controller', () => { const stub3 = sinon.stub(utils, 'createFields').callsFake(() => false); ngMock.inject(function ($rootScope, $controller) { const scope = $rootScope.$new(); - $controller('MlCreateMultiMetricJob', { $scope: scope }); + + expect(() => { + $controller('MlCreateMultiMetricJob', { $scope: scope }); + }).to.not.throwError(); expect(typeof scope.ui).to.eql('object'); stub1.restore(); diff --git a/x-pack/plugins/ml/public/jobs/new_job/simple/population/create_job/__tests__/create_job_controller.js b/x-pack/plugins/ml/public/jobs/new_job/simple/population/create_job/__tests__/create_job_controller.js index 9cedc38106e9c..07a65c51024a8 100644 --- a/x-pack/plugins/ml/public/jobs/new_job/simple/population/create_job/__tests__/create_job_controller.js +++ b/x-pack/plugins/ml/public/jobs/new_job/simple/population/create_job/__tests__/create_job_controller.js @@ -30,7 +30,10 @@ describe('ML - Population Wizard - Create Job Controller', () => { const stub3 = sinon.stub(utils, 'createFields').callsFake(() => false); ngMock.inject(function ($rootScope, $controller) { const scope = $rootScope.$new(); - $controller('MlCreatePopulationJob', { $scope: scope }); + + expect(() => { + $controller('MlCreatePopulationJob', { $scope: scope }); + }).to.not.throwError(); expect(typeof scope.ui).to.eql('object'); stub1.restore(); diff --git a/x-pack/plugins/ml/public/jobs/new_job/simple/recognize/create_job/__tests__/create_job_controller.js b/x-pack/plugins/ml/public/jobs/new_job/simple/recognize/create_job/__tests__/create_job_controller.js index 786d06afbc40e..60d99021044e2 100644 --- a/x-pack/plugins/ml/public/jobs/new_job/simple/recognize/create_job/__tests__/create_job_controller.js +++ b/x-pack/plugins/ml/public/jobs/new_job/simple/recognize/create_job/__tests__/create_job_controller.js @@ -26,14 +26,17 @@ describe('ML - Recognize Wizard - Create Job Controller', () => { })); ngMock.inject(function ($rootScope, $controller) { const scope = $rootScope.$new(); - $controller('MlCreateRecognizerJobs', { - $route: { - current: { - params: {} - } - }, - $scope: scope - }); + + expect(() => { + $controller('MlCreateRecognizerJobs', { + $route: { + current: { + params: {} + } + }, + $scope: scope + }); + }).to.not.throwError(); expect(scope.ui.formValid).to.eql(true); stub.restore(); diff --git a/x-pack/plugins/ml/public/jobs/new_job/simple/single_metric/create_job/__tests__/create_job_controller.js b/x-pack/plugins/ml/public/jobs/new_job/simple/single_metric/create_job/__tests__/create_job_controller.js index bc9ba3a250262..817b3ffe9ced2 100644 --- a/x-pack/plugins/ml/public/jobs/new_job/simple/single_metric/create_job/__tests__/create_job_controller.js +++ b/x-pack/plugins/ml/public/jobs/new_job/simple/single_metric/create_job/__tests__/create_job_controller.js @@ -28,14 +28,17 @@ describe('ML - Single Metric Wizard - Create Job Controller', () => { const stub2 = sinon.stub(indexUtils, 'timeBasedIndexCheck').callsFake(() => false); ngMock.inject(function ($rootScope, $controller) { const scope = $rootScope.$new(); - $controller('MlCreateSingleMetricJob', { - $route: { - current: { - params: {} - } - }, - $scope: scope - }); + + expect(() => { + $controller('MlCreateSingleMetricJob', { + $route: { + current: { + params: {} + } + }, + $scope: scope + }); + }).to.not.throwError(); expect(scope.ui.showJobInput).to.eql(false); stub1.restore(); diff --git a/x-pack/plugins/ml/public/jobs/new_job/wizard/steps/index_or_search/__tests__/index_or_search_controller.js b/x-pack/plugins/ml/public/jobs/new_job/wizard/steps/index_or_search/__tests__/index_or_search_controller.js index c596567a9e84a..d06a045b61479 100644 --- a/x-pack/plugins/ml/public/jobs/new_job/wizard/steps/index_or_search/__tests__/index_or_search_controller.js +++ b/x-pack/plugins/ml/public/jobs/new_job/wizard/steps/index_or_search/__tests__/index_or_search_controller.js @@ -17,14 +17,17 @@ describe('ML - Index Or Search Controller', () => { it('Initialize Index Or Search Controller', (done) => { ngMock.inject(function ($rootScope, $controller) { const scope = $rootScope.$new(); - $controller('MlNewJobStepIndexOrSearch', { - $route: { - current: { - locals: {} - } - }, - $scope: scope - }); + + expect(() => { + $controller('MlNewJobStepIndexOrSearch', { + $route: { + current: { + locals: {} + } + }, + $scope: scope + }); + }).to.not.throwError(); expect(scope.indexPatterns).to.eql([]); done(); diff --git a/x-pack/plugins/ml/public/jobs/new_job/wizard/steps/job_type/__tests__/job_type_controller.js b/x-pack/plugins/ml/public/jobs/new_job/wizard/steps/job_type/__tests__/job_type_controller.js index 9786df1976c7c..060f10fd88a82 100644 --- a/x-pack/plugins/ml/public/jobs/new_job/wizard/steps/job_type/__tests__/job_type_controller.js +++ b/x-pack/plugins/ml/public/jobs/new_job/wizard/steps/job_type/__tests__/job_type_controller.js @@ -28,7 +28,10 @@ describe('ML - Job Type Controller', () => { const stub2 = sinon.stub(indexUtils, 'timeBasedIndexCheck').callsFake(() => false); ngMock.inject(function ($rootScope, $controller) { const scope = $rootScope.$new(); - $controller('MlNewJobStepJobType', { $scope: scope }); + + expect(() => { + $controller('MlNewJobStepJobType', { $scope: scope }); + }).to.not.throwError(); expect(scope.indexWarningTitle).to.eql('Index pattern undefined is not time based'); stub1.restore(); diff --git a/x-pack/plugins/ml/public/lib/__tests__/angular_bootstrap_patch.js b/x-pack/plugins/ml/public/lib/__tests__/angular_bootstrap_patch.js index 2a6603f3cc343..9fbf202caebb4 100644 --- a/x-pack/plugins/ml/public/lib/__tests__/angular_bootstrap_patch.js +++ b/x-pack/plugins/ml/public/lib/__tests__/angular_bootstrap_patch.js @@ -20,10 +20,12 @@ describe('ML - Angular Bootstrap Patch - Dropdown Controller', () => { expect(scope.$$watchersCount).to.eql(0); - $controller('DropdownController', { - $attrs: [], - $scope: scope - }); + expect(() => { + $controller('DropdownController', { + $attrs: [], + $scope: scope + }); + }).to.not.throwError(); expect(scope.$$watchersCount).to.eql(1); done(); diff --git a/x-pack/plugins/ml/public/settings/__tests__/settings_controller.js b/x-pack/plugins/ml/public/settings/__tests__/settings_controller.js index cd5a6b1a921e9..5f7e1aec5da6a 100644 --- a/x-pack/plugins/ml/public/settings/__tests__/settings_controller.js +++ b/x-pack/plugins/ml/public/settings/__tests__/settings_controller.js @@ -17,7 +17,10 @@ describe('ML - Settings Controller', () => { it('Initialize Settings Controller', (done) => { ngMock.inject(function ($rootScope, $controller) { const scope = $rootScope.$new(); - $controller('MlSettings', { $scope: scope }); + + expect(() => { + $controller('MlSettings', { $scope: scope }); + }).to.not.throwError(); expect(scope.canCreateFilter).to.eql(false); done(); diff --git a/x-pack/plugins/ml/public/settings/scheduled_events/calendars_list/__tests__/calendars_list_controller.js b/x-pack/plugins/ml/public/settings/scheduled_events/calendars_list/__tests__/calendars_list_controller.js index 9875172b76208..30121de63ac92 100644 --- a/x-pack/plugins/ml/public/settings/scheduled_events/calendars_list/__tests__/calendars_list_controller.js +++ b/x-pack/plugins/ml/public/settings/scheduled_events/calendars_list/__tests__/calendars_list_controller.js @@ -17,7 +17,10 @@ describe('ML - Calendars List Controller', () => { it('Initialize Calendars List Controller', (done) => { ngMock.inject(function ($rootScope, $controller) { const scope = $rootScope.$new(); - $controller('MlCalendarsList', { $scope: scope }); + + expect(() => { + $controller('MlCalendarsList', { $scope: scope }); + }).to.not.throwError(); expect(scope.permissions.canCreateCalendar).to.eql(false); done(); diff --git a/x-pack/plugins/ml/public/settings/scheduled_events/components/import_events_modal/__tests__/import_events_modal_controller.js b/x-pack/plugins/ml/public/settings/scheduled_events/components/import_events_modal/__tests__/import_events_modal_controller.js index 073a3c1db9469..5db68ce991f42 100644 --- a/x-pack/plugins/ml/public/settings/scheduled_events/components/import_events_modal/__tests__/import_events_modal_controller.js +++ b/x-pack/plugins/ml/public/settings/scheduled_events/components/import_events_modal/__tests__/import_events_modal_controller.js @@ -19,11 +19,14 @@ describe('ML - Import Events Modal Controller', () => { it('Initialize Import Events Modal Controller', (done) => { ngMock.inject(function ($rootScope, $controller) { const scope = $rootScope.$new(); - $controller('MlImportEventsModal', { - $scope: scope, - $modalInstance: mockModalInstance, - params: {} - }); + + expect(() => { + $controller('MlImportEventsModal', { + $scope: scope, + $modalInstance: mockModalInstance, + params: {} + }); + }).to.not.throwError(); expect(scope.loadingLock).to.be(false); done(); diff --git a/x-pack/plugins/ml/public/settings/scheduled_events/components/new_event_modal/__tests__/new_event_modal_controller.js b/x-pack/plugins/ml/public/settings/scheduled_events/components/new_event_modal/__tests__/new_event_modal_controller.js index 312fb31a7101c..6c7b3cb85ba23 100644 --- a/x-pack/plugins/ml/public/settings/scheduled_events/components/new_event_modal/__tests__/new_event_modal_controller.js +++ b/x-pack/plugins/ml/public/settings/scheduled_events/components/new_event_modal/__tests__/new_event_modal_controller.js @@ -19,11 +19,14 @@ describe('ML - New Event Modal Controller', () => { it('Initialize New Event Modal Controller', (done) => { ngMock.inject(function ($rootScope, $controller) { const scope = $rootScope.$new(); - $controller('MlNewEventModal', { - $scope: scope, - $modalInstance: mockModalInstance, - params: {} - }); + + expect(() => { + $controller('MlNewEventModal', { + $scope: scope, + $modalInstance: mockModalInstance, + params: {} + }); + }).to.not.throwError(); expect(scope.event.description).to.be(''); done(); diff --git a/x-pack/plugins/ml/public/settings/scheduled_events/new_calendar/__tests__/create_calendar_controller.js b/x-pack/plugins/ml/public/settings/scheduled_events/new_calendar/__tests__/create_calendar_controller.js index 8827152b85b56..a8605d5f8d359 100644 --- a/x-pack/plugins/ml/public/settings/scheduled_events/new_calendar/__tests__/create_calendar_controller.js +++ b/x-pack/plugins/ml/public/settings/scheduled_events/new_calendar/__tests__/create_calendar_controller.js @@ -17,14 +17,17 @@ describe('ML - Create Calendar Controller', () => { it('Initialize Create Calendar Controller', (done) => { ngMock.inject(function ($rootScope, $controller) { const scope = $rootScope.$new(); - $controller('MlCreateCalendar', { - $route: { - current: { - params: {} - } - }, - $scope: scope - }); + + expect(() => { + $controller('MlCreateCalendar', { + $route: { + current: { + params: {} + } + }, + $scope: scope + }); + }).to.not.throwError(); expect(scope.isNewCalendar).to.eql(true); done(); diff --git a/x-pack/plugins/ml/public/timeseriesexplorer/__tests__/timeseriesexplorer_controller.js b/x-pack/plugins/ml/public/timeseriesexplorer/__tests__/timeseriesexplorer_controller.js index ab8105b9ef8e6..6c3bb0a9a3252 100644 --- a/x-pack/plugins/ml/public/timeseriesexplorer/__tests__/timeseriesexplorer_controller.js +++ b/x-pack/plugins/ml/public/timeseriesexplorer/__tests__/timeseriesexplorer_controller.js @@ -17,7 +17,10 @@ describe('ML - Time Series Explorer Controller', () => { it('Initialize Time Series Explorer Controller', (done) => { ngMock.inject(function ($rootScope, $controller) { const scope = $rootScope.$new(); - $controller('MlTimeSeriesExplorerController', { $scope: scope }); + + expect(() => { + $controller('MlTimeSeriesExplorerController', { $scope: scope }); + }).to.not.throwError(); expect(scope.timeFieldName).to.eql('timestamp'); done();