Skip to content

Commit

Permalink
[ML] Wrap controller initialization in assertions. (#26265) (#26273)
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
walterra authored and peteharverson committed Nov 29, 2018
1 parent ce1fe02 commit 73387d2
Show file tree
Hide file tree
Showing 20 changed files with 134 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 73387d2

Please sign in to comment.