Skip to content

Commit

Permalink
feat(plugin): Allow all plugins to be gotted.
Browse files Browse the repository at this point in the history
docs(plugin): Update getPlugin docs
  • Loading branch information
christopherthielen committed Dec 1, 2016
1 parent b9f4541 commit e324973
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 15 deletions.
35 changes: 24 additions & 11 deletions src/router.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/** @coreapi @module core */ /** */
import {UrlMatcherFactory} from "./url/urlMatcherFactory";
import {UrlRouterProvider} from "./url/urlRouter";
import {UrlRouter} from "./url/urlRouter";
import {TransitionService} from "./transition/transitionService";
import {ViewService} from "./view/view";
import {StateRegistry} from "./state/stateRegistry";
import {StateService} from "./state/stateService";
import {UIRouterGlobals, Globals} from "./globals";
import {UIRouterPlugin} from "./interface";
import { UrlMatcherFactory } from "./url/urlMatcherFactory";
import { UrlRouterProvider } from "./url/urlRouter";
import { UrlRouter } from "./url/urlRouter";
import { TransitionService } from "./transition/transitionService";
import { ViewService } from "./view/view";
import { StateRegistry } from "./state/stateRegistry";
import { StateService } from "./state/stateService";
import { UIRouterGlobals, Globals } from "./globals";
import { UIRouterPlugin } from "./interface";
import { values } from "./common/common";

/**
* The master class used to instantiate an instance of UI-Router.
Expand Down Expand Up @@ -103,14 +104,26 @@ export class UIRouter {
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;
/** Allow javascript factory function */
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;
}

getPlugin(pluginName: string): UIRouterPlugin {
return this._plugins[pluginName];
/**
* Returns registered plugins
*
* Returns the registered plugin of the given `pluginName`.
* If no `pluginName` is given, returns all registered plugins
*
* @param pluginName (optional) the name of the plugin to get
* @return the named plugin (undefined if not found), or all plugins (if `pluginName` is omitted)
*/
getPlugin(): UIRouterPlugin[];
getPlugin(pluginName?: string): UIRouterPlugin;
getPlugin(pluginName?: string): UIRouterPlugin|UIRouterPlugin[] {
return pluginName ? this._plugins[pluginName] : values(this._plugins);
}
}

Expand Down
27 changes: 23 additions & 4 deletions test/pluginSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as vanilla from "../src/vanilla";
import { StateRegistry } from "../src/state/stateRegistry";
import { UrlRouter } from "../src/url/urlRouter";
import {UIRouterPlugin} from "../src/interface";
import { isArray } from "../src/common/predicates";

describe('plugin api', function () {
let router: UIRouter;
Expand All @@ -23,23 +24,25 @@ describe('plugin api', function () {
});

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

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

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

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

it('should accept a factory function', () => {
Expand All @@ -48,6 +51,7 @@ describe('plugin api', function () {
}
let plugin = router.plugin(factoryFn);
expect(plugin instanceof FancyPluginClass).toBeTruthy();
expect(plugin.name).toBe('fancypluginclass');
});

it('should return an instance of the plugin', () => {
Expand Down Expand Up @@ -77,9 +81,24 @@ describe('plugin api', function () {

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

it('should return undefined if no pluginName is registered', () => {
router.plugin(FancyPluginClass);
let plugin = router.getPlugin('notexists');
expect(plugin).toBeUndefined();
});

it('should return all registered plugins when no pluginName is specified', () => {
router.plugin(FancyPluginClass);
router.plugin(FancyPluginConstructor);
let plugins = router.getPlugin();
expect(isArray(plugins)).toBeTruthy();
expect(plugins.pop() instanceof FancyPluginConstructor).toBeTruthy();
expect(plugins.pop() instanceof FancyPluginClass).toBeTruthy();
});
})
});

0 comments on commit e324973

Please sign in to comment.