Skip to content

Commit

Permalink
overload register to accept an anonymous factory, #120
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffdutton committed Sep 12, 2019
1 parent 1468484 commit 552f0b7
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/laconia-core/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ declare namespace laconia {

interface LaconiaHandler extends Handler {
register(
factory: LaconiaFactory | LaconiaFactory[],
factory: string | LaconiaFactory | LaconiaFactory[],
optionsOrFactory?: LaconiaFactory | FactoryOptions,
options?: FactoryOptions
): LaconiaHandler;
}
Expand Down
12 changes: 11 additions & 1 deletion packages/laconia-core/src/laconia.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,21 @@ module.exports = app => {
};

return Object.assign(laconia, {
register: (factory, options = {}) => {
register: (factory, optionsOrFactory, options = {}) => {
if (typeof optionsOrFactory !== "function") {
options = optionsOrFactory || {};
}
if (Array.isArray(factory)) {
factory.forEach(f => checkFunction("register", f));
laconiaContext.registerFactories(factory, options.cache);
} else {
if (typeof factory === "string") {
checkFunction("register", optionsOrFactory);
const factoryKey = factory;
factory = async laconiaContext => ({
[factoryKey]: await optionsOrFactory(laconiaContext)
});
}
checkFunction("register", factory);
laconiaContext.registerFactory(factory, options.cache);
}
Expand Down
64 changes: 64 additions & 0 deletions packages/laconia-core/test/laconia.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@ describe("laconia", () => {
);
});

it("should be able to add instances by calling 'register' with a string", async () => {
const app = jest.fn();
await laconia(app)
.register("foo", lc => "bar")
.register("boo", lc => "baz")(...handlerArgs);

expect(app).toBeCalledWith(
expect.any(Object),
expect.objectContaining({
foo: "bar",
boo: "baz"
})
);
});

it("should throw an error when the required dependency is not available", async () => {
const handler = laconia((event, { fooo }) => {}).register(lc => ({
foo: "bar"
Expand All @@ -88,6 +103,18 @@ describe("laconia", () => {
);
});

it("should be able to add async instances by calling 'register' with a string", async () => {
const app = jest.fn();
await laconia(app).register("foo", async lc => "bar")(...handlerArgs);

expect(app).toBeCalledWith(
expect.any(Object),
expect.objectContaining({
foo: "bar"
})
);
});

it("should cache factory by default", async () => {
const factory = jest.fn().mockImplementation(() => ({}));
const handler = await laconia(jest.fn()).register(factory);
Expand All @@ -97,6 +124,18 @@ describe("laconia", () => {
expect(factory).toHaveBeenCalledTimes(1);
});

it("should cache factory by default with a string", async () => {
const factory = jest.fn().mockImplementation(() => ({}));
const handler = await laconia(jest.fn()).register(
"someFactory",
factory
);
await handler(...handlerArgs);
await handler(...handlerArgs);

expect(factory).toHaveBeenCalledTimes(1);
});

it("should be able to turn off caching", async () => {
const factory = jest.fn().mockImplementation(() => ({}));
const handler = await laconia(jest.fn()).register(factory, {
Expand All @@ -110,13 +149,38 @@ describe("laconia", () => {
expect(factory).toHaveBeenCalledTimes(2);
});

it("should be able to turn off caching with a string", async () => {
const factory = jest.fn().mockImplementation(() => ({}));
const handler = await laconia(jest.fn()).register(
"someFactory",
factory,
{
cache: {
enabled: false
}
}
);
await handler(...handlerArgs);
await handler(...handlerArgs);

expect(factory).toHaveBeenCalledTimes(2);
});

it("should throw an error when the factory is not a function", async () => {
expect(() => laconia(jest.fn()).register({ foo: "bar" })).toThrow(
new TypeError(
'register() expects to be passed a function, you passed: {"foo":"bar"}'
)
);
});

it("should throw an error when the factory is not a function with a string", async () => {
expect(() => laconia(jest.fn()).register("foo", "bar")).toThrow(
new TypeError(
'register() expects to be passed a function, you passed: "bar"'
)
);
});
});

describe("when registering an array", () => {
Expand Down
8 changes: 8 additions & 0 deletions packages/laconia-core/test/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,25 @@ const adapter = (app: Application) => async (

laconia(app)
.register(() => ({ someKey: "value" }))
.register("someKey", () => "value")
.register([() => ({ someKey: "value" }), () => ({ someKey: "value" })])
.register(lc => {
return { otherKey: lc.something + 1 };
})
.register(() => ({ someKey: "value" }), {})
.register(() => ({ someKey: "value" }), {})
.register("someKey", () => "value", {})
.register(() => ({ someKey: "value" }), {
cache: {
enabled: false,
maxAge: 1000
}
})
.register("someKey", () => "value", {
cache: {
enabled: false,
maxAge: 1000
}
});

laconia(adapter(app)).register(() => ({ someKey: "value" }));

0 comments on commit 552f0b7

Please sign in to comment.