Give access to angular $injector via chrome (#15267)

* Implement getActiveInjector

* Fix typo in comments

* Rename method to sound more frightening

* Move test utils

* Add more documentation

* Fix typo in comment

* Fix name of method in comments
This commit is contained in:
Tim Roes 2017-11-30 15:00:11 +01:00 committed by GitHub
parent 0317cc6bd6
commit a6b41838b5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 74 additions and 3 deletions

View file

@ -0,0 +1,44 @@
/**
* This test file contains stubs for chrome.dangerouslyGetActiveInjector, that you will
* need to load if any part of the code you are testing relies on that method.
* You will need to call setupInjectorStub and teardownInjectorStub in specific
* places inside your test file to setup and teardown the stub.
* If you can call both of them at the same place you can also use the shortcut
* setupAndTeardownInjectorStub instead.
*/
import ngMock from 'ng_mock';
import chrome from 'ui/chrome';
import sinon from 'sinon';
/**
* This method setups the stub for chrome.dangerouslyGetActiveInjector. You must call it in
* a place where beforeEach is allowed to be called (read: inside your describe)
* method. You must call this AFTER you've called `ngMock.module` to setup the modules,
* but BEFORE you first execute code, that uses chrome.getActiveInjector.
*/
export function setupInjectorStub() {
beforeEach(ngMock.inject(($injector) => {
sinon.stub(chrome, 'dangerouslyGetActiveInjector').returns(Promise.resolve($injector));
}));
}
/**
* This methods tears down the stub for chrome.dangerouslyGetActiveInjector. You must call it
* in a place where afterEach is allowed to be called.
*/
export function teardownInjectorStub() {
afterEach(() => {
chrome.getActiveInjector.restore();
});
}
/**
* This method combines setupInjectorStub and teardownInjectorStub in one method.
* It can be used if you can call the other two methods directly behind each other.
*/
export function setupAndTeardownInjectorStub() {
setupInjectorStub();
teardownInjectorStub();
}

View file

@ -46,7 +46,34 @@ templateApi(chrome, internals);
themeApi(chrome, internals);
translationsApi(chrome, internals);
chrome.bootstrap = function () {
chrome.setupAngular();
angular.bootstrap(document.body, ['kibana']);
const waitForBootstrap = new Promise(resolve => {
chrome.bootstrap = function () {
chrome.setupAngular();
angular.bootstrap(document.body, ['kibana']);
resolve();
};
});
/**
* ---- ATTENTION: Read documentation carefully before using this! ----
*
* Returns a promise, that resolves with an instance of the currently used Angular
* $injector service for usage outside of Angular.
* You can use this injector to get access to any other injectable component (service,
* constant, etc.) by using its get method.
*
* If you ever use Angular outside of an Angular context via this method, you should
* be really sure you know what you are doing!
*
* When using this method inside your code, you will need to stub it while running
* tests. Look into 'src/test_utils/public/stub_get_active_injector' for more information.
*/
chrome.dangerouslyGetActiveInjector = () => {
return waitForBootstrap.then(() => {
const $injector = angular.element(document.body).injector();
if (!$injector) {
return Promise.reject('document.body had no angular context after bootstrapping');
}
return $injector;
});
};