Skip to content

Commit

Permalink
chore: Discovery module setup
Browse files Browse the repository at this point in the history
And fix unit tests on DI modules
  • Loading branch information
alexandremgo committed Feb 7, 2024
1 parent 3758800 commit 538432d
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 6 deletions.
4 changes: 3 additions & 1 deletion packages/core/src/di.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { makeLoggerMiddleware } from "inversify-logger-middleware";
import { configModuleFactory } from "@internal/config/di/configModule";
import { usbModuleFactory } from "@internal/usb/di/usbModule";
import { deviceModelModuleFactory } from "@internal/device-model/di/deviceModelModule";
import { discoveryModuleFactory } from "@internal/discovery/di/discoveryModule";

const logger = makeLoggerMiddleware();

Expand All @@ -18,7 +19,8 @@ export const makeContainer = ({
container.load(
configModuleFactory({ stub }),
deviceModelModuleFactory(),
usbModuleFactory({ stub })
usbModuleFactory({ stub }),
discoveryModuleFactory({ stub }),
// modules go here
);

Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/internal/discovery/di/discoveryDiTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const discoveryDiTypes = {
StartDiscoveryUseCase: Symbol.for("StartDiscoveryUseCase"),
};
31 changes: 31 additions & 0 deletions packages/core/src/internal/discovery/di/discoveryModule.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Container } from "inversify";
import { StartDiscoveryUseCase } from "@internal/discovery/use-case/UsbHidStartDiscoveryUseCase";
import { discoveryModuleFactory } from "./discoveryModule";
import { usbModuleFactory } from "@internal/usb/di/usbModule";
import { deviceModelModuleFactory } from "@internal/device-model/di/deviceModelModule";
import { discoveryDiTypes } from "./discoveryDiTypes";


describe("discoveryModuleFactory", () => {
let container: Container;
let mod: ReturnType<typeof discoveryModuleFactory>;
beforeEach(() => {
mod = discoveryModuleFactory();
container = new Container();
container.load(
mod,
// The following modules are injected into discovery module
usbModuleFactory(),
deviceModelModuleFactory(),
);
});

it("should return the device module", () => {
expect(mod).toBeDefined();
});

it("should return none mocked use cases", () => {
const startDiscoveryUseCase = container.get(discoveryDiTypes.StartDiscoveryUseCase);
expect(startDiscoveryUseCase).toBeInstanceOf(StartDiscoveryUseCase);
});
});
19 changes: 19 additions & 0 deletions packages/core/src/internal/discovery/di/discoveryModule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ContainerModule } from "inversify";
import { StartDiscoveryUseCase } from "@internal/discovery/use-case/UsbHidStartDiscoveryUseCase";
import { discoveryDiTypes } from "./discoveryDiTypes";

type FactoryProps = {
stub: boolean;
};

export const discoveryModuleFactory = ({
stub = false,
}: Partial<FactoryProps> = {}) =>
new ContainerModule((bind, _unbind, _isBound, _rebind) => {
bind(discoveryDiTypes.StartDiscoveryUseCase).to(StartDiscoveryUseCase);

if (stub) {
// We can rebind our interfaces to their mock implementations
// rebind(...).to(....);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Observable } from "rxjs";
import { inject, injectable } from "inversify";
import { usbDiTypes } from "@internal/usb/di/usbDiTypes";
import type { UsbHidTransport } from "@internal/usb/transport/UsbHidTransport";
import { DiscoveredDevice } from "@internal/usb/model/DiscoveredDevice";

/**
* Starts discovering devices connected via USB HID (and later BLE).
*
* For the WebHID implementation, this use-case needs to be called as a result of an user interaction (button "click" event for ex).
*/
@injectable()
export class StartDiscoveryUseCase {
constructor(
@inject(usbDiTypes.UsbHidTransport) private usbHidTransport: UsbHidTransport,
// Later: @inject(usbDiTypes.BleTransport) private bleTransport: BleTransport,
) {
}

execute(): Observable<DiscoveredDevice> {
return this.usbHidTransport.startDiscovering();
}
}
11 changes: 6 additions & 5 deletions packages/core/src/internal/usb/di/usbModule.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Container } from "inversify";
import { WebUsbHidTransport } from "@internal/usb/transport/WebUsbHidTransport";
import { DefaultUsbService } from "@internal/usb/service/DefaultUsbService";
import { deviceModelModuleFactory } from "@internal/device-model/di/deviceModelModule";
import { usbModuleFactory } from "./usbModule";
import { usbDiTypes } from "./usbDiTypes";

Expand All @@ -10,18 +10,19 @@ describe("usbModuleFactory", () => {
beforeEach(() => {
mod = usbModuleFactory();
container = new Container();
container.load(mod);
container.load(
mod,
deviceModelModuleFactory(),
);
});

it("should return the usb module", () => {
expect(mod).toBeDefined();
});

it("should return none mocked services and transport", () => {
it("should return none mocked transports", () => {
const usbHidTransport = container.get(usbDiTypes.UsbHidTransport);
const usbService = container.get(usbDiTypes.UsbService);
expect(usbHidTransport).toBeInstanceOf(WebUsbHidTransport);
expect(usbService).toBeInstanceOf(DefaultUsbService);
});
});

0 comments on commit 538432d

Please sign in to comment.