Skip to content

Commit da4097b

Browse files
committed
test: add cli test
1 parent 34e7560 commit da4097b

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

test/cli/identity/init.test.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { IDatabaseMap } from "@js-soft/docdb-access-abstractions";
2+
import { BaseCommand } from "../../../src/cli/BaseCommand";
3+
import { identityInitHandler } from "../../../src/cli/commands/identity/init";
4+
import { ConnectorRuntimeConfig, createConnectorConfig } from "../../../src/ConnectorRuntimeConfig";
5+
import { setupEnvironment } from "../setup";
6+
7+
describe("identity init", () => {
8+
let accountInfo: IDatabaseMap;
9+
let config: ConnectorRuntimeConfig;
10+
let originalArgv: any;
11+
beforeAll(() => {
12+
setupEnvironment();
13+
config = createConnectorConfig();
14+
});
15+
16+
afterEach(() => {
17+
jest.resetAllMocks();
18+
19+
// Set process arguments back to the original value
20+
process.argv = originalArgv;
21+
});
22+
23+
beforeEach(async () => {
24+
const dbConnection = await BaseCommand.createDBConnection(config);
25+
const db = await dbConnection.getDatabase(`${config.database.dbNamePrefix}${config.database.dbName}`);
26+
27+
accountInfo = await db.getMap("AccountInfo");
28+
await accountInfo.get("");
29+
const list = await accountInfo.list();
30+
for (const item of list) {
31+
await accountInfo.delete(item.name);
32+
}
33+
// need to close as the data is only written to disk when the connection is closed
34+
await dbConnection.close();
35+
36+
// Remove all cached modules. The cache needs to be cleared before running
37+
// each command, otherwise you will see the same results from the command
38+
// run in your first test in subsequent tests.
39+
jest.resetModules();
40+
41+
// Each test overwrites process arguments so store the original arguments
42+
originalArgv = process.argv;
43+
});
44+
45+
test("identity creation", async () => {
46+
const consoleSpy = jest.spyOn(console, "log");
47+
await identityInitHandler({ config: undefined });
48+
expect(consoleSpy).toHaveBeenCalledWith("Identity created successfully!");
49+
expect(consoleSpy).toHaveBeenCalledTimes(1);
50+
await identityInitHandler({ config: undefined });
51+
expect(consoleSpy).toHaveBeenCalledWith("Identity already created!");
52+
expect(consoleSpy).toHaveBeenCalledTimes(2);
53+
});
54+
});

test/cli/setup.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export function setupEnvironment(): void {
2+
process.env.database = JSON.stringify({
3+
driver: "lokijs",
4+
folder: "./",
5+
dbName: "default",
6+
dbNamePrefix: "local-"
7+
});
8+
process.env.NODE_CONFIG_ENV = "test";
9+
process.env.API_KEY = "test";
10+
11+
process.env["transportLibrary:baseUrl"] = process.env["NMSHD_TEST_BASEURL"];
12+
process.env["transportLibrary:platformClientId"] = process.env["NMSHD_TEST_CLIENTID"];
13+
process.env["transportLibrary:platformClientSecret"] = process.env["NMSHD_TEST_CLIENTSECRET"];
14+
}

0 commit comments

Comments
 (0)