Skip to content

Commit

Permalink
test: add cli test
Browse files Browse the repository at this point in the history
  • Loading branch information
sebbi08 committed Oct 17, 2024
1 parent 1e0796e commit a145fc0
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
54 changes: 54 additions & 0 deletions test/cli/identity/init.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { IDatabaseMap } from "@js-soft/docdb-access-abstractions";
import { BaseCommand } from "../../../src/cli/BaseCommand";
import { identityInitHandler } from "../../../src/cli/commands/identity/init";
import { ConnectorRuntimeConfig, createConnectorConfig } from "../../../src/ConnectorRuntimeConfig";
import { setupEnvironment } from "../setup";

describe("identity init", () => {
let accountInfo: IDatabaseMap;
let config: ConnectorRuntimeConfig;
let originalArgv: any;
beforeAll(() => {
setupEnvironment();
config = createConnectorConfig();
});

afterEach(() => {
jest.resetAllMocks();

// Set process arguments back to the original value
process.argv = originalArgv;
});

beforeEach(async () => {
const dbConnection = await BaseCommand.createDBConnection(config);
const db = await dbConnection.getDatabase(`${config.database.dbNamePrefix}${config.database.dbName}`);

accountInfo = await db.getMap("AccountInfo");
await accountInfo.get("");
const list = await accountInfo.list();
for (const item of list) {
await accountInfo.delete(item.name);
}
// need to close as the data is only written to disk when the connection is closed
await dbConnection.close();

// Remove all cached modules. The cache needs to be cleared before running
// each command, otherwise you will see the same results from the command
// run in your first test in subsequent tests.
jest.resetModules();

// Each test overwrites process arguments so store the original arguments
originalArgv = process.argv;
});

test("identity creation", async () => {
const consoleSpy = jest.spyOn(console, "log");
await identityInitHandler({ config: undefined });
expect(consoleSpy).toHaveBeenCalledWith("Identity created successfully!");
expect(consoleSpy).toHaveBeenCalledTimes(1);
await identityInitHandler({ config: undefined });
expect(consoleSpy).toHaveBeenCalledWith("Identity already created!");
expect(consoleSpy).toHaveBeenCalledTimes(2);
});
});
14 changes: 14 additions & 0 deletions test/cli/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export function setupEnvironment(): void {
process.env.database = JSON.stringify({
driver: "lokijs",
folder: "./",
dbName: "default",
dbNamePrefix: "local-"
});
process.env.NODE_CONFIG_ENV = "test";
process.env.API_KEY = "test";

process.env["transportLibrary:baseUrl"] = process.env["NMSHD_TEST_BASEURL"];
process.env["transportLibrary:platformClientId"] = process.env["NMSHD_TEST_CLIENTID"];
process.env["transportLibrary:platformClientSecret"] = process.env["NMSHD_TEST_CLIENTSECRET"];
}

0 comments on commit a145fc0

Please sign in to comment.