From 73387d22f739a7239c9d8e890a4702cda353aea6 Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Thu, 29 Nov 2018 14:25:15 +0100 Subject: [PATCH] [ML] Wrap controller initialization in assertions. (#26265) (#26273) - 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 6b70395153598..a130512216d04 100644 --- a/x-pack/plugins/ml/public/datavisualizer/__tests__/datavisualizer_controller.js +++ b/x-pack/plugins/ml/public/datavisualizer/__tests__/datavisualizer_controller.js @@ -36,7 +36,10 @@ describe('ML - Data Visualizer View Fields Controller', () => { }; const scope = $rootScope.$new(); - $controller('MlDataVisualizerViewFields', { $scope: scope }); + + expect(() => { + $controller('MlDataVisualizerViewFields', { $scope: scope }); + }).to.not.throwError(); expect(scope.metricCards).to.eql([]); stub.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 618823a01e919..704f1466b405a 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 @@ -24,7 +24,10 @@ describe('ML - Advanced Job Wizard - New Job 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 07bb4ce902dc2..448d1bfa600dd 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 @@ -32,7 +32,10 @@ describe('ML - Multi Metric Wizard - Create Job 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 b0987aada5bd9..430be2c927376 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 @@ -32,7 +32,10 @@ describe('ML - Population Wizard - Create Job 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 c52a06219b0a6..2ea1913461ca7 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', () => { }; 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); done(); 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 0658c723126e0..ea7622a08af2c 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 @@ -30,14 +30,17 @@ describe('ML - Single Metric Wizard - Create Job 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); stub.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 6bc48a5c60247..33500fd199512 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 @@ -33,7 +33,10 @@ describe('ML - Job Type Controller', () => { }; const scope = $rootScope.$new(); - $controller('MlNewJobStepJobType', { $scope: scope }); + + expect(() => { + $controller('MlNewJobStepJobType', { $scope: scope }); + }).to.not.throwError(); expect(scope.indexWarningTitle).to.eql('Index pattern test_pattern is not time based'); stub.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();