Skip to content

Commit

Permalink
tests: add tests for middleware extension
Browse files Browse the repository at this point in the history
  • Loading branch information
fdionisi committed Jun 7, 2024
1 parent 6d0ef78 commit 99d65bb
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 39 deletions.
58 changes: 20 additions & 38 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ import { Addresses } from "./entities";
import { ManagementController } from "./facades/management";
import { Inner } from "./inner";

type EdcControllerClass = Class<
EdcController,
[Inner, EdcConnectorClientContext]
>;

export type EdcConnectorClientType<
T extends Record<string, EdcController>,
> = EdcConnectorClient & T;
Expand Down Expand Up @@ -63,11 +58,10 @@ export class EdcConnectorClient {
static Builder = class Builder<
Controllers extends Record<
string,
EdcControllerClass
EdcController
> = {},
> {
#instance = new EdcConnectorClient();
#controllers: Partial<Controllers> = {};

apiToken(apiToken: string): this {
this.#instance.#apiToken = apiToken;
Expand Down Expand Up @@ -101,43 +95,31 @@ export class EdcConnectorClient {

use<K extends string, C extends EdcController>(
key: K,
controller: EdcControllerClass,
Controller: Class<C>,
): Builder<Controllers & Record<K, C>> {
const b = new Builder<Controllers & Record<K, C>>();
b.#instance = this.#instance;

b.#controllers = {
...this.#controllers,
[key]: controller,
} as Controllers & Record<K, C>;

return b;
Object.defineProperty(this.#instance, key, {
get() {
return new Controller(
this.#inner,
this.createContext(
this.#apiToken!,
this.#addresses,
),
);
},
enumerable: true,
configurable: false,
});

return this as any;
}

build<K extends keyof Controllers>(): EdcConnectorClientType<
Record<K, InstanceType<Controllers[K]>>
build(): EdcConnectorClientType<
Controllers
> {
for (const key in this.#controllers) {
if (this.#controllers.hasOwnProperty(key)) {
const Controller = this.#controllers[key]!;
Object.defineProperty(this.#instance, key, {
get: () =>
new Controller(
this.#instance.#inner,
this.#instance.createContext(
this.#instance.#apiToken!,
this.#instance.#addresses,
),
),
enumerable: true,
configurable: false,
});
}
}

return this.#instance as
& EdcConnectorClient
& { [K in keyof Controllers]: InstanceType<Controllers[K]> };
& Controllers;
}
};
}
43 changes: 42 additions & 1 deletion tests/edc-client.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { EdcConnectorClient, EdcConnectorClientContext } from "../src";
import {
EdcConnectorClient,
EdcConnectorClientContext,
EdcController,
} from "../src";
import { Addresses } from "../src";

describe("EdcConnectorClient", () => {
Expand Down Expand Up @@ -39,4 +43,41 @@ describe("EdcConnectorClient", () => {
expect(context.control).toBe(addresses.control);
});
});

describe("edcClient.Builder.use", () => {
interface ActiveResponse {
active: boolean;
}

class FooController extends EdcController {
async testFoo(): Promise<ActiveResponse> {
return {
active: true,
};
}
}

class BarController extends EdcController {
async testBar(): Promise<ActiveResponse> {
return {
active: false,
};
}
}

it("allows to extend the clients through middlewares", async () => {
const client = new EdcConnectorClient.Builder()
.use("foo", FooController)
.use("bar", BarController)
.build();

await expect(client.foo.testFoo()).resolves.toEqual({
active: true,
});

await expect(client.bar.testBar()).resolves.toEqual({
active: false,
});
});
});
});

0 comments on commit 99d65bb

Please sign in to comment.