Skip to content

Commit

Permalink
feat(plugin): Allow registration by ES6 class, JS constructor fn, JS …
Browse files Browse the repository at this point in the history
…factory fn
  • Loading branch information
christopherthielen committed Dec 1, 2016
1 parent 241d972 commit b9f4541
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
21 changes: 15 additions & 6 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ export class UIRouter {
*
* #### Example:
* ```js
* export class MyAuthPlugin {
* export class MyAuthPlugin implements UIRouterPlugin {
* constructor(router: UIRouter, options: any) {
* this.name = "MyAuthPlugin";
* let $transitions = router.transitionService;
* let $state = router.stateService;
*
Expand All @@ -90,12 +91,20 @@ export class UIRouter {
* }
* ```
*
* @param pluginFactory a function which accepts a [[UIRouter]] instance and returns a UI-Router Plugin instance
* @param options options to pass to the plugin
* @returns {T}
* @param plugin one of:
* - a plugin class which implements [[UIRouterPlugin]]
* - a constructor function for a [[UIRouterPlugin]] which accepts a [[UIRouter]] instance
* - a factory function which accepts a [[UIRouter]] instance and returns a [[UIRouterPlugin]] instance
* @param options options to pass to the plugin class/factory
* @returns the registered plugin instance
*/
plugin<T extends UIRouterPlugin>(pluginFactory: PluginFactory<T>, options: any = {}): T {
let pluginInstance = pluginFactory(this, options);
plugin<T extends UIRouterPlugin>(plugin: { new(router: UIRouter, options?: any): T }, options?: any): T;
/** Allow javascript constructor function */
plugin<T extends UIRouterPlugin>(plugin: { (router: UIRouter, options?: any): void }, options?: any): T;
/** Allow javascript factory function */
plugin<T extends UIRouterPlugin>(plugin: PluginFactory<T>, options?: any): T;
plugin<T extends UIRouterPlugin>(plugin: any, options: any = {}): T {
let pluginInstance = new plugin(this, options);
if (!pluginInstance.name) throw new Error("Required property `name` missing on plugin: " + pluginInstance);
return this._plugins[pluginInstance.name] = pluginInstance;
}
Expand Down
32 changes: 27 additions & 5 deletions test/pluginSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,37 @@ describe('plugin api', function () {
router.stateRegistry.stateQueue.autoFlush($state);
});

class FancyPlugin implements UIRouterPlugin {
class FancyPluginClass implements UIRouterPlugin {
constructor(public router: UIRouter) { }
name = "fancyplugin"
}

function FancyPluginConstructor(router: UIRouter, options: any) {
this.name = "fancyplugin";
}

describe('initialization', () => {
it('should accept a plugin class', () => {
let plugin = router.plugin(FancyPluginClass);
expect(plugin instanceof FancyPluginClass).toBeTruthy();
});

it('should accept a constructor function', () => {
let plugin = router.plugin(FancyPluginConstructor);
expect(plugin instanceof FancyPluginConstructor).toBeTruthy();
});

it('should accept a factory function', () => {
function factoryFn(router: UIRouter, options: any) {
return new FancyPluginClass(router);
}
let plugin = router.plugin(factoryFn);
expect(plugin instanceof FancyPluginClass).toBeTruthy();
});

it('should return an instance of the plugin', () => {
let plugin = router.plugin(() => new FancyPlugin(router));
expect(plugin instanceof FancyPlugin).toBeTruthy();
let plugin = router.plugin(() => new FancyPluginClass(router));
expect(plugin instanceof FancyPluginClass).toBeTruthy();
});

it('should pass the router instance to the plugin constructor', () => {
Expand All @@ -55,9 +77,9 @@ describe('plugin api', function () {

describe('getPlugin', () => {
it('should return the plugin instance', () => {
router.plugin(() => new FancyPlugin(router));
router.plugin(() => new FancyPluginClass(router));
let plugin = router.getPlugin('fancyplugin');
expect(plugin instanceof FancyPlugin).toBeTruthy();
expect(plugin instanceof FancyPluginClass).toBeTruthy();
});
})
});

0 comments on commit b9f4541

Please sign in to comment.