11const { describe, it } = require ( 'mocha' ) ;
2- const AssetsPublish = require ( '../../../../src/commands/cm/assets/publish' ) ;
3- const { cliux } = require ( '@contentstack/cli-utilities' ) ;
42const sinon = require ( 'sinon' ) ;
3+ const { expect } = require ( 'chai' ) ;
54const { config } = require ( 'dotenv' ) ;
65
7- const { stub } = sinon ;
6+ const AssetsPublish = require ( '../../../../src/commands/cm/assets/publish' ) ;
87
98config ( ) ;
109
1110const environments = process . env . ENVIRONMENTS . split ( ',' ) ;
1211const locales = process . env . LOCALES . split ( ',' ) ;
1312
1413describe ( 'AssetsPublish' , ( ) => {
15- it ( 'Should run the command when all the flags are passed' , async function ( ) {
14+ it ( 'Should run the command when all the flags are passed' , async ( ) => {
1615 const args = [ '--environments' , environments [ 0 ] , '--locales' , locales [ 0 ] , '--alias' , process . env . MANAGEMENT_ALIAS , '--yes' ] ;
17- const inquireStub = stub ( cliux , 'inquire' ) ;
16+ const assetPublishSpy = sinon . spy ( AssetsPublish . prototype , 'run' ) ;
17+ await AssetsPublish . run ( args ) ;
18+ expect ( assetPublishSpy . calledOnce ) . to . be . true ;
19+ assetPublishSpy . restore ( ) ;
20+ } ) ;
21+
22+ it ( 'Should fail when alias and stack api key flags are not passed' , async ( ) => {
23+ const args = [ '--environments' , environments [ 0 ] , '--locales' , locales [ 0 ] , '--yes' ] ;
24+ const assetPublishSpy = sinon . spy ( AssetsPublish . prototype , 'run' ) ;
25+ const expectedError = 'Please use `--alias` or `--stack-api-key` to proceed.' ;
26+ try {
27+ await AssetsPublish . run ( args ) ;
28+ } catch ( error ) {
29+ expect ( error ) . to . be . an . instanceOf ( Error ) ;
30+ expect ( error . message ) . to . equal ( expectedError ) ;
31+ expect ( assetPublishSpy . calledOnce ) . to . be . true ;
32+ }
33+ assetPublishSpy . restore ( ) ;
34+ } ) ;
35+
36+ it ( 'Should run successfully when user is logged in and stack api key is passed' , async ( ) => {
37+ const args = [ '--environments' , environments [ 0 ] , '--locales' , locales [ 0 ] , '--stack-api-key' , process . env . STACK_API_KEY , '--yes' ] ;
38+ const assetPublishSpy = sinon . spy ( AssetsPublish . prototype , 'run' ) ;
1839 await AssetsPublish . run ( args ) ;
19- sinon . assert . notCalled ( inquireStub ) ;
20- inquireStub . restore ( ) ;
40+ expect ( assetPublishSpy . calledOnce ) . to . be . true ;
41+ assetPublishSpy . restore ( ) ;
2142 } ) ;
2243} ) ;
0 commit comments