From 3eb770f63758a5557ada828896fea4ddec9b2bd4 Mon Sep 17 00:00:00 2001 From: shafeeqd959 Date: Wed, 31 Jan 2024 13:26:06 +0530 Subject: [PATCH 1/3] added unit tests for early access header config --- .../src/interfaces/index.ts | 2 +- .../unit/commands/early-access-header.test.ts | 98 +++++++++++++++++++ .../test/unit/mock/index.ts | 7 ++ 3 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 packages/contentstack-config/test/unit/commands/early-access-header.test.ts diff --git a/packages/contentstack-config/src/interfaces/index.ts b/packages/contentstack-config/src/interfaces/index.ts index 4e8ad0c01a..3c164bac2e 100644 --- a/packages/contentstack-config/src/interfaces/index.ts +++ b/packages/contentstack-config/src/interfaces/index.ts @@ -14,5 +14,5 @@ export interface Region { name: string; cma: string; cda: string; - uiHost: string; + uiHost?: string; } diff --git a/packages/contentstack-config/test/unit/commands/early-access-header.test.ts b/packages/contentstack-config/test/unit/commands/early-access-header.test.ts new file mode 100644 index 0000000000..408b8892ea --- /dev/null +++ b/packages/contentstack-config/test/unit/commands/early-access-header.test.ts @@ -0,0 +1,98 @@ +import { expect, should } from 'chai'; +import { stub, spy } from 'sinon'; +import { configHandler } from '@contentstack/cli-utilities'; +import { cliux } from '@contentstack/cli-utilities'; +import Conf from 'conf'; +import { setEarlyAccessHeaderMockData } from '../mock/index'; +import { interactive } from '../../../src/utils/index'; + +import GetEarlyAccessHeaderCommand from '../../../src/commands/config/get/early-access-header'; +import SetEarlyAccessHeaderCommand from '../../../src/commands/config/set/early-access-header'; +import RemoveEarlyAccessHeaderCommand from '../../../src/commands/config/remove/early-access-header'; + + +const config = configHandler; +describe('Early access header command', function () { + let configSetStub; + let cliuxSuccessStub; + let configHandlerDeleteStub; + before(() => { + configSetStub = stub(configHandler, 'set').returns({} as Conf>); + cliuxSuccessStub = stub(cliux, 'success').callsFake(()=> {}); + configHandlerDeleteStub = stub(configHandler, 'delete').resolves(""); + }); + + after(() => { + // Restore the original method after each test + configSetStub.restore(); + cliuxSuccessStub.restore(); + configHandlerDeleteStub.restore(); + }); + + it('Set early access header: with all flags, should be successful', async function () { + const args = [ + '--header-alias', + setEarlyAccessHeaderMockData.flags.headerAlias, + '--header', + setEarlyAccessHeaderMockData.flags.header, + ]; + await SetEarlyAccessHeaderCommand.run(args); + expect(cliuxSuccessStub.calledOnce).to.be.true; + }); + + it('Set early access header: should prompt when header alias is not passed', async () => { + const askEarlyAccessHeaderAlias = stub(interactive, 'askEarlyAccessHeaderAlias').resolves(setEarlyAccessHeaderMockData.flags.headerAlias); + await SetEarlyAccessHeaderCommand.run(["--header", setEarlyAccessHeaderMockData.flags.header]); + expect(askEarlyAccessHeaderAlias.calledOnce).to.be.true; + askEarlyAccessHeaderAlias.restore(); + }); + + it('Set early access header: should prompt when header is not passed', async () => { + const askEarlyAccessHeaderAlias = stub(interactive, 'askEarlyAccessHeaderValue').resolves(setEarlyAccessHeaderMockData.flags.header); + await SetEarlyAccessHeaderCommand.run(["--header-alias", setEarlyAccessHeaderMockData.flags.headerAlias]); + expect(askEarlyAccessHeaderAlias.calledOnce).to.be.true; + askEarlyAccessHeaderAlias.restore(); + }); + + it('Get early access header: with all flags, should be successful', async function () { + const cliuxTableStub = stub(cliux, 'table'); + await GetEarlyAccessHeaderCommand.run([]); + expect(cliuxTableStub.calledOnce).to.be.true; + cliuxTableStub.restore(); + }); + + it('Remove early access header: with all flags, should be successful', async function () { + const configGetStub = stub(configHandler, 'get').resolves(setEarlyAccessHeaderMockData.flags.headerAlias); + const args = [ + '--header-alias', + setEarlyAccessHeaderMockData.flags.headerAlias, + '--yes' + ]; + await RemoveEarlyAccessHeaderCommand.run(args); + expect(configHandlerDeleteStub.calledOnce).to.be.true; + configGetStub.restore(); + }); + + it('Remove early access header: with only alias flag should prompt for confirmation', async function () { + const configGetStub = stub(configHandler, 'get').resolves(setEarlyAccessHeaderMockData.flags.headerAlias); + const confirmationStub = stub(interactive, 'askConfirmation').resolves(true); + const args = [ + '--header-alias', + setEarlyAccessHeaderMockData.flags.headerAlias, + ]; + await RemoveEarlyAccessHeaderCommand.run(args); + expect(confirmationStub.calledOnce).to.be.true; + configGetStub.restore(); + }); + + it('Remove early access header: without alias flag should prompt', async function () { + const configGetStub = stub(configHandler, 'get').resolves(setEarlyAccessHeaderMockData.flags.headerAlias); + const askHeaderAliasStub = stub(interactive, 'askEarlyAccessHeaderAlias').resolves(setEarlyAccessHeaderMockData.flags.headerAlias); + const args = [ + "--yes" + ]; + await RemoveEarlyAccessHeaderCommand.run(args); + expect(askHeaderAliasStub.calledOnce).to.be.true; + configGetStub.restore(); + }); +}); diff --git a/packages/contentstack-config/test/unit/mock/index.ts b/packages/contentstack-config/test/unit/mock/index.ts index b49cbfd01c..fe9eb7eba4 100644 --- a/packages/contentstack-config/test/unit/mock/index.ts +++ b/packages/contentstack-config/test/unit/mock/index.ts @@ -5,6 +5,13 @@ export const setConfigMockData = { }, }; +export const setEarlyAccessHeaderMockData = { + flags: { + headerAlias: 'taxonomy', + header: 'taxonomytestorg', + }, +}; + export const removeConfigMockData = { flags: { apiKey: 'abcd', From 6d6ade51d1a38071636ace5021e0eb69cfb36d13 Mon Sep 17 00:00:00 2001 From: shafeeqd959 Date: Thu, 1 Feb 2024 13:03:03 +0530 Subject: [PATCH 2/3] fixed early access test case --- .../test/unit/commands/base-branch.test.ts | 1 + .../test/unit/commands/early-access-header.test.ts | 5 +++-- .../test/unit/commands/remove-base-branch.test.ts | 5 +++-- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/packages/contentstack-config/test/unit/commands/base-branch.test.ts b/packages/contentstack-config/test/unit/commands/base-branch.test.ts index 638784ee2c..dd5cdc0a5c 100644 --- a/packages/contentstack-config/test/unit/commands/base-branch.test.ts +++ b/packages/contentstack-config/test/unit/commands/base-branch.test.ts @@ -51,5 +51,6 @@ describe('base-branch command', function () { const branchStub = stub(cliux, 'table').callsFake(() => {}); await BranchGetCommand.run([]); expect(branchStub.calledOnce).to.be.true; + branchStub.restore(); }); }); diff --git a/packages/contentstack-config/test/unit/commands/early-access-header.test.ts b/packages/contentstack-config/test/unit/commands/early-access-header.test.ts index 408b8892ea..c7d04c3b75 100644 --- a/packages/contentstack-config/test/unit/commands/early-access-header.test.ts +++ b/packages/contentstack-config/test/unit/commands/early-access-header.test.ts @@ -81,8 +81,9 @@ describe('Early access header command', function () { setEarlyAccessHeaderMockData.flags.headerAlias, ]; await RemoveEarlyAccessHeaderCommand.run(args); - expect(confirmationStub.calledOnce).to.be.true; - configGetStub.restore(); + expect(confirmationStub.calledOnce).to.be.true; + configGetStub.restore(); + confirmationStub.restore() }); it('Remove early access header: without alias flag should prompt', async function () { diff --git a/packages/contentstack-config/test/unit/commands/remove-base-branch.test.ts b/packages/contentstack-config/test/unit/commands/remove-base-branch.test.ts index 87fddd4d5c..13653d3dd9 100644 --- a/packages/contentstack-config/test/unit/commands/remove-base-branch.test.ts +++ b/packages/contentstack-config/test/unit/commands/remove-base-branch.test.ts @@ -41,9 +41,10 @@ describe('Delete config', () => { const config = configHandler; const getConfig = config.get(`baseBranch.${removeConfigMockData.flags.apiKey}`); - const askConfirmation = stub(interactive, 'askConfirmation').calledOnce; + const askConfirmation = stub(interactive, 'askConfirmation'); const showSuccess = stub(cliux, 'success'); await RemoveBranchConfigCommand.run(['--stack-api-key', removeConfigMockData.flags.apiKey]); - if (getConfig && askConfirmation) expect(showSuccess.calledOnce).to.be.true; + if (getConfig && askConfirmation.calledOnce) expect(showSuccess.calledOnce).to.be.true; + askConfirmation.restore(); }); }); From 23d67883cde843785750344aa0fb2e0499148bd0 Mon Sep 17 00:00:00 2001 From: shafeeqd959 Date: Thu, 1 Feb 2024 13:07:26 +0530 Subject: [PATCH 3/3] corrected the type --- packages/contentstack-config/src/interfaces/index.ts | 2 +- packages/contentstack-config/test/unit/commands/region.test.ts | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/contentstack-config/src/interfaces/index.ts b/packages/contentstack-config/src/interfaces/index.ts index 3c164bac2e..4e8ad0c01a 100644 --- a/packages/contentstack-config/src/interfaces/index.ts +++ b/packages/contentstack-config/src/interfaces/index.ts @@ -14,5 +14,5 @@ export interface Region { name: string; cma: string; cda: string; - uiHost?: string; + uiHost: string; } diff --git a/packages/contentstack-config/test/unit/commands/region.test.ts b/packages/contentstack-config/test/unit/commands/region.test.ts index 6713255ed3..410a0cd263 100644 --- a/packages/contentstack-config/test/unit/commands/region.test.ts +++ b/packages/contentstack-config/test/unit/commands/region.test.ts @@ -12,6 +12,7 @@ describe('Region command', function () { name: 'test', cma: 'https://api.contentstack.com', cda: 'https://cda.contentstack.com', + uiHost: '', }; let cliuxPrintStub; beforeEach(function () {