Skip to content

Commit

Permalink
revert(core): undo style changes made in this PR
Browse files Browse the repository at this point in the history
  • Loading branch information
virkt25 committed Feb 20, 2018
1 parent eb3e42d commit 3289733
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 19 deletions.
1 change: 0 additions & 1 deletion packages/core/docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"content": [
"index.ts",
"src/application.ts",
"src/booter.ts",
"src/component.ts",
"src/index.ts",
"src/keys.ts",
Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ export class Application extends Context {
/**
* Register a controller class with this application.
*
* @param {Function} controllerCtor The controller class
* (constructor function)
* @param controllerCtor {Function} The controller class
* (constructor function).
* @param {string=} name Optional controller name, default to the class name
* @returns {Binding} The newly created binding, you can use the reference to
* @return {Binding} The newly created binding, you can use the reference to
* further modify the binding, e.g. lock the value to prevent further
* modifications.
*
Expand All @@ -43,9 +43,9 @@ export class Application extends Context {
*/
controller(controllerCtor: ControllerClass, name?: string): Binding {
name = name || controllerCtor.name;
return this.bind(`${CoreBindings.CONTROLLERS_PREFIX}.${name}`)
return this.bind(`controllers.${name}`)
.toClass(controllerCtor)
.tag(CoreBindings.CONTROLLERS_TAG);
.tag('controller');
}

/**
Expand Down
4 changes: 0 additions & 4 deletions packages/core/src/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ export namespace CoreBindings {
*/
export const SERVERS = 'servers';

// Binding Constant prefixes / tags
export const CONTROLLERS_PREFIX = 'controllers';
export const CONTROLLERS_TAG = 'controller';

// controller
/**
* Binding key for the controller class resolved in the current request
Expand Down
29 changes: 20 additions & 9 deletions packages/core/test/unit/application.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@
// License text available at https://opensource.org/licenses/MIT

import {expect} from '@loopback/testlab';
import {Application, Server, Component} from '../..';
import {Application, Server, Component} from '../../index';
import {Context, Constructor} from '@loopback/context';

describe('Application', () => {
let app: Application;

beforeEach(givenApp);

describe('controller binding', () => {
let app: Application;
class MyController {}

beforeEach(givenApp);

it('binds a controller', () => {
const binding = app.controller(MyController);
expect(Array.from(binding.tags)).to.containEql('controller');
Expand All @@ -28,14 +27,21 @@ describe('Application', () => {
expect(binding.key).to.equal('controllers.my-controller');
expect(findKeysByTag(app, 'controller')).to.containEql(binding.key);
});

function givenApp() {
app = new Application();
}
});

describe('component binding', () => {
let app: Application;
class MyController {}
class MyComponent implements Component {
controllers = [MyController];
}

beforeEach(givenApp);

it('binds a component', () => {
app.component(MyComponent);
expect(findKeysByTag(app, 'component')).to.containEql(
Expand All @@ -49,24 +55,31 @@ describe('Application', () => {
'components.my-component',
);
});

function givenApp() {
app = new Application();
}
});

describe('server binding', () => {
it('defaults to constructor name', async () => {
const app = new Application();
const binding = app.server(FakeServer);
expect(Array.from(binding.tags)).to.containEql('server');
const result = await app.getServer(FakeServer.name);
expect(result.constructor.name).to.equal(FakeServer.name);
});

it('allows custom name', async () => {
const app = new Application();
const name = 'customName';
app.server(FakeServer, name);
const result = await app.getServer(name);
expect(result.constructor.name).to.equal(FakeServer.name);
});

it('allows binding of multiple servers as an array', async () => {
const app = new Application();
const bindings = app.servers([FakeServer, AnotherServer]);
expect(Array.from(bindings[0].tags)).to.containEql('server');
expect(Array.from(bindings[1].tags)).to.containEql('server');
Expand All @@ -79,6 +92,7 @@ describe('Application', () => {

describe('start', () => {
it('starts all injected servers', async () => {
const app = new Application();
app.component(FakeComponent);

await app.start();
Expand All @@ -89,6 +103,7 @@ describe('Application', () => {
});

it('does not attempt to start poorly named bindings', async () => {
const app = new Application();
app.component(FakeComponent);

// The app.start should not attempt to start this binding.
Expand All @@ -98,10 +113,6 @@ describe('Application', () => {
});
});

function givenApp() {
app = new Application();
}

function findKeysByTag(ctx: Context, tag: string | RegExp) {
return ctx.findByTag(tag).map(binding => binding.key);
}
Expand Down

0 comments on commit 3289733

Please sign in to comment.