elasticsearch/kibana — pull request #2361 by spenceralger (2014-12-17 13:52:49+00:00)
Title: Switch Private server to a provider

Making this switch allows us to call `Private.stub()` and the new `Private.swap()` before the application starts, ensuring that we can substitute any private module. Hopefully the examples can explain this decently.

### `Private.stub(provider, newInstance)`
`Private.stub()` replaces the instance of a module with another value. This is all we have needed until now.

```js
beforeEach(inject(function ($injector, Private) {
Private.stub(
// since this module just exports a function, we need to change
// what Private returns in order to modify it's behavior
require('components/agg_response/hierarchical/_build_split'),
sinon.stub().returns(fakeSplit)
);
}));
```

### `Private.swap(oldProvider, newProvider)`
This new method does an 1-for-1 swap of module providers, unlike `stub()` which replaces a modules instance. Pass the module you want to swap out, and the one it should be replaced with, then profit.

Note: even though this example shows `swap()` being called in a config function, it can be called from anywhere. It is particularly useful in this scenario though.

```js
beforeEach(module('kibana', function (PrivateProvider) {
PrivateProvider.swap(
// since the courier is required automatically before the tests are loaded,
// we can't stub it's internal components unless we do so before the
// application starts. This is why angular has config functions
require('components/courier/_redirect_when_missing'),
function StubbedRedirectProvider($decorate) {
// $decorate is a function that will instantiate the original module when called
return sinon.spy($decorate());
}
);
}));
```