Skip to content

Commit

Permalink
feat: allow narango.module to be registered as global
Browse files Browse the repository at this point in the history
  • Loading branch information
Nargonath committed Oct 25, 2023
1 parent 055143f commit 5bc0608
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 18 deletions.
1 change: 1 addition & 0 deletions lib/narango.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ import type { Config } from "arangojs/connection";

export const MODULE_OPTIONS = Symbol("NARANGO_CONFIG");
export interface NarangoModuleOptions {
global?: boolean;
database: Config;
}
1 change: 1 addition & 0 deletions lib/narango.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export class NarangoModule {
static register(options: NarangoModuleOptions): DynamicModule {
return {
module: NarangoModule,
global: options.global,
providers: [
{ provide: MODULE_OPTIONS, useValue: options },
NarangoService,
Expand Down
53 changes: 35 additions & 18 deletions lib/narango.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Test } from "@nestjs/testing";

import { NarangoModule } from "./narango.module";
import { NarangoService } from "./narango.service";
import { Injectable, Module } from "@nestjs/common";

const closeSpy = jest.fn();
jest.mock("arangojs", () => ({
Expand All @@ -21,16 +22,16 @@ jest.mock("arangojs", () => ({
}));

describe("Narango module", () => {
it("can be imported in another module with database options", async () => {
const options = {
url: "http://localhost:8529",
auth: {
username: "root",
password: "password",
},
databaseName: "dbName",
};
const options = {
url: "http://localhost:8529",
auth: {
username: "root",
password: "password",
},
databaseName: "dbName",
};

it("can be imported in another module with database options", async () => {
const module = await Test.createTestingModule({
imports: [
NarangoModule.register({
Expand All @@ -48,15 +49,6 @@ describe("Narango module", () => {
});

it("close arango connection when application shutdown", async () => {
const options = {
url: "http://localhost:8529",
auth: {
username: "root",
password: "password",
},
databaseName: "dbName",
};

const module = await Test.createTestingModule({
imports: [
NarangoModule.register({
Expand All @@ -69,6 +61,31 @@ describe("Narango module", () => {

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

it("can be registered globally", async () => {
@Injectable()
class TestService {
constructor(private narango: NarangoService) {}
}

@Module({ providers: [TestService] })
class ChildModule {}

// NestJS would throw if the NarangoService is not available in the child module
const rootModule = await Test.createTestingModule({
imports: [
NarangoModule.register({
global: true,
database: options,
}),
ChildModule,
],
}).compile();

const serviceFromRoot = rootModule.get<NarangoService>(NarangoService);
expect(serviceFromRoot).toBeDefined();
expect((serviceFromRoot.db as any).options).toEqual(options);
});
});

/* eslint-enable @typescript-eslint/no-explicit-any */

0 comments on commit 5bc0608

Please sign in to comment.