-
Notifications
You must be signed in to change notification settings - Fork 16
Fixed branches plugin unit testcases #2048
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| fileignoreconfig: | ||
| - filename: package-lock.json | ||
| checksum: 04099ea4a522a2be7c5b0c3a3c010a6feab6b2a078ef6d1145450953f63ac0de | ||
| version: "" | ||
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| // Cleanup file to clear all timers after tests | ||
| after(() => { | ||
| // Clear all timers to prevent hanging | ||
| const timers = require('timers'); | ||
| timers.clearTimeout(); | ||
| timers.clearInterval(); | ||
| timers.clearImmediate(); | ||
|
|
||
| // Also clear any remaining timers | ||
| if (global.clearTimeout) { | ||
| global.clearTimeout(); | ||
| } | ||
| if (global.clearInterval) { | ||
| global.clearInterval(); | ||
| } | ||
| if (global.clearImmediate) { | ||
| global.clearImmediate(); | ||
| } | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| --recursive | ||
| --reporter spec | ||
| --timeout 5000 | ||
| --exit | ||
| --require ./test/cleanup.js |
81 changes: 70 additions & 11 deletions
81
packages/contentstack-branches/test/unit/commands/cm/branches/delete.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,48 +1,107 @@ | ||
| import { describe, it } from 'mocha'; | ||
| import { expect } from 'chai'; | ||
| import { stub } from 'sinon'; | ||
| import { stub, restore } from 'sinon'; | ||
| import BranchDeleteCommand from '../../../../../src/commands/cm/branches/delete'; | ||
| import { deleteBranchMockData } from '../../../mock/data'; | ||
| import { interactive } from '../../../../../src/utils'; | ||
| import { deleteBranch } from '../../../../../src/utils/delete-branch'; | ||
| import { isAuthenticated } from '@contentstack/cli-utilities'; | ||
|
|
||
| describe('Delete branch', () => { | ||
| let deleteBranchStub: any; | ||
| let isAuthenticatedStub: any; | ||
|
|
||
| beforeEach(() => { | ||
| // Mock the deleteBranch function to prevent actual API calls | ||
| deleteBranchStub = stub().resolves(); | ||
|
|
||
| // Mock isAuthenticated to return true | ||
| isAuthenticatedStub = stub().returns(true); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| restore(); | ||
| }); | ||
|
|
||
| it('Delete branch with all flags, should be successful', async function () { | ||
| const stub1 = stub(BranchDeleteCommand.prototype, 'run').resolves(deleteBranchMockData.flags); | ||
| // Mock the deleteBranch function | ||
| const deleteBranchMock = stub().resolves(); | ||
|
|
||
| // Stub the deleteBranch import | ||
| const deleteBranchStub = stub().resolves(); | ||
|
|
||
| // Mock the command's run method to avoid actual execution | ||
| const runStub = stub(BranchDeleteCommand.prototype, 'run').callsFake(async function() { | ||
| // Mock the internal logic | ||
| const { flags } = await this.parse(BranchDeleteCommand); | ||
| expect(flags['stack-api-key']).to.equal(deleteBranchMockData.flags.apiKey); | ||
| expect(flags.uid).to.equal(deleteBranchMockData.flags.uid); | ||
| expect(flags.yes).to.be.true; | ||
| return deleteBranchMock(); | ||
| }); | ||
|
|
||
| await BranchDeleteCommand.run([ | ||
| '--stack-api-key', | ||
| deleteBranchMockData.flags.apiKey, | ||
| '--uid', | ||
| deleteBranchMockData.flags.uid, | ||
| '-y', | ||
| ]); | ||
| expect(stub1.calledOnce).to.be.true; | ||
|
|
||
| stub1.restore(); | ||
|
|
||
| expect(runStub.calledOnce).to.be.true; | ||
| }); | ||
|
|
||
| it('Should prompt when api key is not passed', async () => { | ||
| const askStackAPIKey = stub(interactive, 'askStackAPIKey').resolves(deleteBranchMockData.flags.apiKey); | ||
|
|
||
| // Mock the command's run method | ||
| const runStub = stub(BranchDeleteCommand.prototype, 'run').callsFake(async function() { | ||
| const { flags } = await this.parse(BranchDeleteCommand); | ||
| expect(flags.uid).to.equal(deleteBranchMockData.flags.uid); | ||
| expect(flags.yes).to.be.true; | ||
| return Promise.resolve(); | ||
| }); | ||
|
|
||
| await BranchDeleteCommand.run(['--uid', deleteBranchMockData.flags.uid, "--yes"]); | ||
| expect(askStackAPIKey.calledOnce).to.be.true; | ||
| askStackAPIKey.restore(); | ||
|
|
||
| expect(runStub.calledOnce).to.be.true; | ||
| }); | ||
|
|
||
| it('Should prompt when branch is not passed and also ask confirmation wihtout -y flag', async () => { | ||
| const askSourceBranch = stub(interactive, 'askBranchUid').resolves(deleteBranchMockData.flags.uid); | ||
|
|
||
| // Mock the command's run method | ||
| const runStub = stub(BranchDeleteCommand.prototype, 'run').callsFake(async function() { | ||
| const { flags } = await this.parse(BranchDeleteCommand); | ||
| expect(flags['stack-api-key']).to.equal(deleteBranchMockData.flags.apiKey); | ||
| expect(flags.yes).to.be.true; | ||
| return Promise.resolve(); | ||
| }); | ||
|
|
||
| await BranchDeleteCommand.run(['--stack-api-key', deleteBranchMockData.flags.apiKey, "--yes"]); | ||
| expect(askSourceBranch.calledOnce).to.be.true; | ||
| askSourceBranch.restore(); | ||
|
|
||
| expect(runStub.calledOnce).to.be.true; | ||
| }); | ||
|
|
||
| it('Should ask branch name confirmation if yes not provided, success if same branch uid provided', async () => { | ||
| const askConfirmation = stub(interactive, 'askBranchNameConfirmation').resolves(deleteBranchMockData.flags.uid); | ||
|
|
||
| // Mock the command's run method | ||
| const runStub = stub(BranchDeleteCommand.prototype, 'run').callsFake(async function() { | ||
| const { flags } = await this.parse(BranchDeleteCommand); | ||
| expect(flags['stack-api-key']).to.equal(deleteBranchMockData.flags.apiKey); | ||
| expect(flags.uid).to.equal(deleteBranchMockData.flags.uid); | ||
| expect(flags.yes).to.be.undefined; | ||
| return Promise.resolve(); | ||
| }); | ||
|
|
||
| await BranchDeleteCommand.run([ | ||
| '--stack-api-key', | ||
| deleteBranchMockData.flags.apiKey, | ||
| '--uid', | ||
| deleteBranchMockData.flags.uid | ||
| ]); | ||
| expect(askConfirmation.called).to.be.true; | ||
| askConfirmation.restore(); | ||
|
|
||
| expect(runStub.calledOnce).to.be.true; | ||
| }); | ||
| }); |
46 changes: 39 additions & 7 deletions
46
packages/contentstack-branches/test/unit/commands/cm/branches/list.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,36 +1,68 @@ | ||
| import { describe, it } from 'mocha'; | ||
| import { expect } from 'chai'; | ||
| import { stub } from 'sinon'; | ||
| import { stub, restore } from 'sinon'; | ||
| import BranchListCommand from '../../../../../src/commands/cm/branches/index'; | ||
| import { branchMockData } from '../../../mock/data'; | ||
| import { interactive } from '../../../../../src/utils'; | ||
| import { cliux } from '@contentstack/cli-utilities'; | ||
|
|
||
| describe('List branches', () => { | ||
| afterEach(() => { | ||
| restore(); | ||
| }); | ||
|
|
||
| it('List branches with all flags, should be successful', async function () { | ||
| const stub1 = stub(BranchListCommand.prototype, 'run').resolves(branchMockData.flags); | ||
| // Mock the command's run method to avoid actual API calls | ||
| const runStub = stub(BranchListCommand.prototype, 'run').callsFake(async function() { | ||
| const { flags } = await this.parse(BranchListCommand); | ||
| expect(flags['stack-api-key']).to.equal(branchMockData.flags.apiKey); | ||
| return Promise.resolve(); | ||
| }); | ||
|
|
||
| const args = ['--stack-api-key', branchMockData.flags.apiKey]; | ||
| await BranchListCommand.run(args); | ||
| expect(stub1.calledOnce).to.be.true; | ||
| stub1.restore(); | ||
| expect(runStub.calledOnce).to.be.true; | ||
| }); | ||
|
|
||
| it('Should prompt when api key is not passed', async () => { | ||
| const askStackAPIKey = stub(interactive, 'askStackAPIKey').resolves(branchMockData.flags.apiKey); | ||
|
|
||
| // Mock the command's run method | ||
| const runStub = stub(BranchListCommand.prototype, 'run').callsFake(async function() { | ||
| return Promise.resolve(); | ||
| }); | ||
|
|
||
| await BranchListCommand.run([]); | ||
| expect(askStackAPIKey.calledOnce).to.be.true; | ||
| askStackAPIKey.restore(); | ||
| expect(runStub.calledOnce).to.be.true; | ||
| }); | ||
|
|
||
| it('branches with verbose flag, should list branches in table', async () => { | ||
| const branchStub = stub(cliux, 'table').callsFake((branches) => { | ||
| expect(branches).to.have.length.greaterThan(0); | ||
| }); | ||
|
|
||
| // Mock the command's run method | ||
| const runStub = stub(BranchListCommand.prototype, 'run').callsFake(async function() { | ||
| const { flags } = await this.parse(BranchListCommand); | ||
| expect(flags['stack-api-key']).to.equal(branchMockData.flags.apiKey); | ||
| expect(flags.verbose).to.be.true; | ||
| return Promise.resolve(); | ||
| }); | ||
|
|
||
| await BranchListCommand.run(['-k', branchMockData.flags.apiKey, '--verbose']); | ||
| branchStub.restore(); | ||
| expect(runStub.calledOnce).to.be.true; | ||
| }); | ||
|
|
||
| it('Branch diff when format type is verbose, should display verbose view', async function () { | ||
| // Mock the command's run method | ||
| const runStub = stub(BranchListCommand.prototype, 'run').callsFake(async function() { | ||
| const { flags } = await this.parse(BranchListCommand); | ||
| expect(flags['stack-api-key']).to.equal(branchMockData.flags.apiKey); | ||
| expect(flags.verbose).to.be.true; | ||
| return Promise.resolve(); | ||
| }); | ||
|
|
||
| await BranchListCommand.run(['-k', branchMockData.flags.apiKey, '--verbose']); | ||
| expect(runStub.calledOnce).to.be.true; | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.