segmentio/analytics.js — pull request #374 by lancejpollard (2014-06-20 21:02:34+00:00)
Title: allow adding integrations in a test-friendly way

@yields @ianstormtaylor this would makes things easier to test, so we can instantiate the integration and use it directly in tests without any tricky workarounds.

The test before-blocks would look something like this:

```js
describe('before loading', function(){
beforeEach(function(){
analytics = new Analytics;
mixpanel = new Mixpanel.Integration(options);
analytics.use(plugin);
analytics.use(tester);
analytics.spy(mixpanel, 'load');
analytics.add(mixpanel);
});
});

describe('after loading', function(){
before(function(done){
analytics = new Analytics;
mixpanel = new Mixpanel.Integration(options);
analytics.use(plugin);
analytics.use(tester);
analytics.once('ready', done);
analytics.add(mixpanel);
analytics.initialize();
analytics.page();
});
});
```

instead of this:

```js
describe('before loading', function(){
beforeEach(function(){
analytics = new Analytics;
analytics.use(plugin);
analytics.use(tester);
analytics.spy(Mixpanel.Integration.prototype, 'load');
mixpanel = analytics.integration('Mixpanel');
});
});

describe('after loading', function(){
before(function(done){
analytics = new Analytics;
analytics.use(plugin);
analytics.use(tester);
analytics.once('ready', done);
analytics.initialize(settings);
analytics.page();
mixpanel = analytics.integration('Mixpanel');
});
});
```

All tests pass. The only thing that I changed was removing a test that `this._integrations` got reset. I only case I can seeing that being desired behavior is so you can call `analytics.initialize(settings)` multiple times, but does this ever happen? I can see us wanting to reset that perhaps for tests, but it seems like that type of thing would go into a `.reset()` method or something. If this is desired behavior, what is it used for exactly? That way I can try to figure out a way to still get this `.add` functionality in there to simplify the tests :D

/cc @calvinfo