Skip to content

Commit

Permalink
test: add unit test for BootMixin
Browse files Browse the repository at this point in the history
  • Loading branch information
virkt25 committed Feb 20, 2018
1 parent 540c998 commit 8576b37
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions packages/boot/test/unit/boot.mixin.unit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright IBM Corp. 2018. All Rights Reserved.
// Node module: @loopback/boot
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {expect} from '@loopback/testlab';
import {BootMixin, Booter, BootBindings} from '../../';
import {Application} from '@loopback/core';

describe('BootMxiin unit tests', () => {
let app: AppWithBootMixin;

beforeEach(getApp);

it('mixes into the target class', () => {
expect(app.boot).to.be.a.Function();
expect(app.booters).to.be.a.Function();
});

it('adds BootComponent to target class', () => {
const boundComponent = app.find('components.*').map(b => b.key);
expect(boundComponent).to.containEql('components.BootComponent');
});

it('binds booter from app.booters()', async () => {
app.booters(TestBooter);
const booter = await app.get(`${BootBindings.BOOTER_PREFIX}.TestBooter`);
expect(booter).to.be.an.instanceOf(TestBooter);
});

it('binds multiple booter classes from app.booters()', async () => {
app.booters(TestBooter, AnotherTestBooter);
const booter = await app.get(`${BootBindings.BOOTER_PREFIX}.TestBooter`);
expect(booter).to.be.an.instanceOf(TestBooter);

const anotherBooter = await app.get(
`${BootBindings.BOOTER_PREFIX}.AnotherTestBooter`,
);
expect(anotherBooter).to.be.an.instanceOf(AnotherTestBooter);
});

it('binds user defined component without a booter', async () => {
class EmptyTestComponent {}

app.component(EmptyTestComponent);
const compInstance = await app.get('components.EmptyTestComponent');
expect(compInstance).to.be.an.instanceOf(EmptyTestComponent);
});

it('binds a user defined component with a booter from .component()', async () => {
class TestComponent {
booters = [TestBooter];
}

app.component(TestComponent);
const compInstance = await app.get('components.TestComponent');
expect(compInstance).to.be.an.instanceOf(TestComponent);
const booterInst = await app.get(
`${BootBindings.BOOTER_PREFIX}.TestBooter`,
);
expect(booterInst).to.be.an.instanceOf(TestBooter);
});

class TestBooter implements Booter {
configured = false;

async configure() {
this.configured = true;
}
}

class AnotherTestBooter implements Booter {
discovered = false;

async discover() {
this.discovered = true;
}
}

class AppWithBootMixin extends BootMixin(Application) {}

function getApp() {
app = new AppWithBootMixin();
}
});

0 comments on commit 8576b37

Please sign in to comment.