|
1 | | -import { describe, it } from 'mocha'; |
| 1 | +import { describe, it, beforeEach, afterEach } from 'mocha'; |
2 | 2 | import { expect } from 'chai'; |
3 | | -import { stub } from 'sinon'; |
| 3 | +import { stub, restore } from 'sinon'; |
4 | 4 | import RemoveBranchConfigCommand from '../../../src/commands/config/remove/base-branch'; |
5 | 5 | import { interactive } from '../../../src/utils'; |
6 | 6 | import { removeConfigMockData } from '../mock'; |
7 | 7 | import { cliux, configHandler } from '@contentstack/cli-utilities'; |
8 | 8 |
|
9 | 9 | describe('Delete config', () => { |
| 10 | + const testApiKey = removeConfigMockData.flags.apiKey; |
| 11 | + const testBaseBranch = 'test-branch'; |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + // Set up test config before each test |
| 15 | + configHandler.set(`baseBranch.${testApiKey}`, testBaseBranch); |
| 16 | + }); |
| 17 | + |
| 18 | + afterEach(() => { |
| 19 | + // Clean up test config after each test |
| 20 | + try { |
| 21 | + configHandler.delete(`baseBranch.${testApiKey}`); |
| 22 | + } catch (error) { |
| 23 | + // Ignore if config doesn't exist |
| 24 | + } |
| 25 | + restore(); |
| 26 | + }); |
| 27 | + |
10 | 28 | it('Delete config with all flags, should be successful', async function () { |
11 | | - const stub1 = stub(RemoveBranchConfigCommand.prototype, 'run'); |
12 | | - await RemoveBranchConfigCommand.run(['--stack-api-key', removeConfigMockData.flags.apiKey, '-y']); |
13 | | - expect(stub1.calledOnce).to.be.true; |
14 | | - stub1.restore(); |
| 29 | + const successStub = stub(cliux, 'success'); |
| 30 | + await RemoveBranchConfigCommand.run(['--stack-api-key', testApiKey, '-y']); |
| 31 | + |
| 32 | + // Verify that base branch and stack-api-key are displayed before deletion |
| 33 | + expect(successStub.called).to.be.true; |
| 34 | + const successCalls = successStub.getCalls(); |
| 35 | + const messages = successCalls.map(call => call.args[0]); |
| 36 | + |
| 37 | + // Should show base branch and stack-api-key before deletion |
| 38 | + expect(messages.some(msg => msg.includes(`base branch : ${testBaseBranch}`))).to.be.true; |
| 39 | + expect(messages.some(msg => msg.includes(`stack-api-key: ${testApiKey}`))).to.be.true; |
| 40 | + // Should show success message after deletion |
| 41 | + expect(messages.some(msg => msg.includes('removed successfully'))).to.be.true; |
15 | 42 | }); |
| 43 | + |
16 | 44 | it('Should prompt when api key is not passed', async () => { |
17 | | - const askStackAPIKey = stub(interactive, 'askStackAPIKey').resolves(removeConfigMockData.flags.apiKey); |
| 45 | + const askStackAPIKey = stub(interactive, 'askStackAPIKey').resolves(testApiKey); |
18 | 46 | await RemoveBranchConfigCommand.run(['-y']); |
19 | 47 | expect(askStackAPIKey.calledOnce).to.be.true; |
20 | | - askStackAPIKey.restore(); |
21 | 48 | }); |
| 49 | + |
22 | 50 | it('Should throw an error if config doesnt exist', async () => { |
23 | | - const config = configHandler; |
24 | | - const getConfig = config.get(`baseBranch.${removeConfigMockData.flags.apiKey}`); |
| 51 | + // Remove the config first |
| 52 | + configHandler.delete(`baseBranch.${testApiKey}`); |
| 53 | + |
| 54 | + const errorStub = stub(cliux, 'error'); |
| 55 | + await RemoveBranchConfigCommand.run(['--stack-api-key', testApiKey]); |
25 | 56 |
|
26 | | - const throwError = stub(cliux, 'error'); |
27 | | - await RemoveBranchConfigCommand.run(['--stack-api-key', removeConfigMockData.flags.apiKey]); |
28 | | - if (getConfig === undefined) expect(throwError.calledOnce).to.be.true; |
29 | | - throwError.restore(); |
| 57 | + expect(errorStub.calledOnce).to.be.true; |
| 58 | + expect(errorStub.getCalls()[0].args[0]).to.include(`No config set for stack-api-key : ${testApiKey}`); |
30 | 59 | }); |
| 60 | + |
31 | 61 | it('Should ask for confirmation to remove config if the config exists', async () => { |
32 | | - const config = configHandler; |
33 | | - const getConfig = config.get(`baseBranch.${removeConfigMockData.flags.apiKey}`); |
| 62 | + const askConfirmation = stub(interactive, 'askConfirmation').resolves(true); |
| 63 | + await RemoveBranchConfigCommand.run(['--stack-api-key', testApiKey]); |
| 64 | + |
| 65 | + expect(askConfirmation.calledOnce).to.be.true; |
| 66 | + }); |
34 | 67 |
|
35 | | - const askConfirmation = stub(interactive, 'askConfirmation'); |
36 | | - await RemoveBranchConfigCommand.run(['--stack-api-key', removeConfigMockData.flags.apiKey]); |
37 | | - if (getConfig) expect(askConfirmation.calledOnce).to.be.true; |
38 | | - askConfirmation.restore(); |
| 68 | + it('Should show base branch and stack-api-key before deletion', async () => { |
| 69 | + const successStub = stub(cliux, 'success'); |
| 70 | + const askConfirmation = stub(interactive, 'askConfirmation').resolves(true); |
| 71 | + |
| 72 | + await RemoveBranchConfigCommand.run(['--stack-api-key', testApiKey]); |
| 73 | + |
| 74 | + const successCalls = successStub.getCalls(); |
| 75 | + const messages = successCalls.map(call => call.args[0]); |
| 76 | + |
| 77 | + // Verify that base branch and stack-api-key are displayed |
| 78 | + expect(messages.some(msg => msg.includes(`base branch : ${testBaseBranch}`))).to.be.true; |
| 79 | + expect(messages.some(msg => msg.includes(`stack-api-key: ${testApiKey}`))).to.be.true; |
39 | 80 | }); |
| 81 | + |
40 | 82 | it('Should show success message on deletion of config', async () => { |
41 | | - const config = configHandler; |
42 | | - const getConfig = config.get(`baseBranch.${removeConfigMockData.flags.apiKey}`); |
43 | | - |
44 | | - const askConfirmation = stub(interactive, 'askConfirmation'); |
45 | | - const showSuccess = stub(cliux, 'success'); |
46 | | - await RemoveBranchConfigCommand.run(['--stack-api-key', removeConfigMockData.flags.apiKey]); |
47 | | - if (getConfig && askConfirmation.calledOnce) expect(showSuccess.calledOnce).to.be.true; |
48 | | - askConfirmation.restore(); |
| 83 | + const successStub = stub(cliux, 'success'); |
| 84 | + const askConfirmation = stub(interactive, 'askConfirmation').resolves(true); |
| 85 | + |
| 86 | + await RemoveBranchConfigCommand.run(['--stack-api-key', testApiKey]); |
| 87 | + |
| 88 | + expect(askConfirmation.calledOnce).to.be.true; |
| 89 | + |
| 90 | + const successCalls = successStub.getCalls(); |
| 91 | + const messages = successCalls.map(call => call.args[0]); |
| 92 | + |
| 93 | + // Should show success message after deletion |
| 94 | + expect(messages.some(msg => msg.includes('removed successfully'))).to.be.true; |
| 95 | + }); |
| 96 | + |
| 97 | + it('Should not delete config if confirmation is rejected', async () => { |
| 98 | + const askConfirmation = stub(interactive, 'askConfirmation').resolves(false); |
| 99 | + const deleteStub = stub(configHandler, 'delete'); |
| 100 | + |
| 101 | + await RemoveBranchConfigCommand.run(['--stack-api-key', testApiKey]); |
| 102 | + |
| 103 | + expect(askConfirmation.calledOnce).to.be.true; |
| 104 | + // delete should not be called if confirmation is rejected |
| 105 | + expect(deleteStub.called).to.be.false; |
| 106 | + |
| 107 | + deleteStub.restore(); |
49 | 108 | }); |
50 | 109 | }); |
0 commit comments