Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix registry tests #150

Merged
merged 1 commit into from
Apr 20, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions tests/unit/registry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import * as registry from "../../src/registry";
import { Container } from "../../src/container";
import { DefaultContainer } from "../../src/Default/default";

function registerCallback(): boolean {
return true;
}
class TestContainer extends DefaultContainer {
static condition(options?: any): boolean {
return options && options.hostType === "Test";
}

function createCallback(): DefaultContainer {
return new TestContainer();
}
static create(): Container {
return new TestContainer();
}

class TestContainer extends DefaultContainer {
constructor() {
super();
this.hostType = "Test";
Expand All @@ -19,34 +19,35 @@ class TestContainer extends DefaultContainer {

describe("registry", () => {
describe("resolveContainer", () => {
afterEach(() => {
beforeEach(() => {
registry.clearRegistry();
});

it("No matching condition resolves default", () => {
let container: Container = registry.resolveContainer();
let container: Container = registry.resolveContainer(true);
expect(container).toBeDefined();
expect(container.hostType).toEqual("Default");
});

it("Subsequent call returns from cache", () => {
let container: Container = registry.resolveContainer();
let container2: Container = registry.resolveContainer();
let container: Container = registry.resolveContainer(true);
spyOn(registry, "container").and.returnValue(container);
let container2: Container = registry.resolveContainer(); // Specifically testing cached value
expect(container2).toBeDefined();
expect(container2).toEqual(container);
expect(container2.uuid).toEqual(container.uuid);
});

it("Resolves registered test container", () => {
registry.registerContainer("Test", { condition: registerCallback, create: createCallback });
registry.registerContainer("Test", { condition: TestContainer.condition, create: TestContainer.create });

let container: Container = registry.resolveContainer(true);
let container: Container = registry.resolveContainer(true, {hostType: "Test"});
expect(container).toBeDefined();
expect(container.hostType).toEqual("Test");
});

it("Error on resolve logs to console", () => {
spyOn(console, "error");
registry.registerContainer("Test", { condition: () => { throw new Error("Forced Error") }, create: createCallback });
registry.registerContainer("Test", { condition: () => { throw new Error("Forced Error") }, create: TestContainer.create });
let container: Container = registry.resolveContainer(true);
expect(console.error).toHaveBeenCalledWith("Error resolving container 'Test': Error: Forced Error");
});
Expand Down