|
| 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 | +}); |
0 commit comments