Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nagdahimanshu committed Oct 18, 2023
1 parent aa264fa commit fb50a82
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/utils/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const copyLegacyDB = async (_this: Command) => {
_this.log(`Legacy database for Lisk Core v4 has been created at ${LEGACY_DB_PATH}`);
};

const getFinalConfigPath = async (outputDir: string, network: string) =>
export const getFinalConfigPath = async (outputDir: string, network: string) =>
(await exists(`${outputDir}/config.json`))
? outputDir
: path.resolve(__dirname, '../..', 'config', network);
Expand Down
60 changes: 59 additions & 1 deletion test/unit/utils/node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@
*
* Removal or modification of this copyright notice is prohibited.
*/
import { validateStartCommandParams } from '../../../src/utils/node';
import { join, resolve } from 'path';
import { validateStartCommandParams, getFinalConfigPath } from '../../../src/utils/node';

const clientFilePath = resolve(`${__dirname}/../../../src/client`);

afterEach(() => {
jest.clearAllMocks();
jest.resetModules();
});

describe('Test validateStartCommandParams method', () => {
const allowedFlags = [
Expand Down Expand Up @@ -127,3 +135,53 @@ describe('Test validateStartCommandParams method', () => {
});
});
});

describe('Test getFinalConfigPath method', () => {
const network = 'mainnet';

it('should return default config filepath when output directory does not exists', async () => {
const outputDir = join(__dirname, '../../..', 'test/unit/fixtures/outputDir');
const configPath = await getFinalConfigPath(outputDir, network);

const expectedResponse = join(__dirname, '../../..', 'config/mainnet');
expect(configPath).toBe(expectedResponse);
});

it('should return correct config filepath when output directory exists', async () => {
const outputDir = join(__dirname, '../../..', 'config');
const configPath = await getFinalConfigPath(outputDir, network);

const expectedResponse = join(__dirname, '../../..', 'config/mainnet');
expect(configPath).toBe(expectedResponse);
});
});

describe('Test isLiskCoreV3Running method', () => {
it('should return true when node is running', async () => {
jest.mock(clientFilePath, () => ({
getAPIClient: jest.fn().mockResolvedValueOnce({
node: { getNodeInfo: jest.fn().mockReturnValue({}) },
}),
}));

/* eslint-disable-next-line global-require, @typescript-eslint/no-var-requires */
const { isLiskCoreV3Running } = require('../../../src/utils/node');
const response = await isLiskCoreV3Running();

expect(response).toBe(true);
});

it('should return false when node is not running', async () => {
jest.mock(clientFilePath, () => ({
getAPIClient: jest.fn().mockResolvedValueOnce({
node: { getNodeInfo: undefined },
}),
}));

/* eslint-disable-next-line global-require, @typescript-eslint/no-var-requires */
const { isLiskCoreV3Running } = require('../../../src/utils/node');
const response = await isLiskCoreV3Running();

expect(response).toBe(false);
});
});

0 comments on commit fb50a82

Please sign in to comment.