import 'mocha' ; import { expect } from 'chai' ; import { getEnumValues } from '@/application/Common/Enum' ; import { Script } from '@/domain/Script' ; import { RecommendationLevel } from '@/domain/RecommendationLevel' ; import { IScriptCode } from '@/domain/IScriptCode' ; import { ScriptCodeStub } from '@tests/unit/stubs/ScriptCodeStub' ; describe ( 'Script' , ( ) => { describe ( 'ctor' , ( ) => { describe ( 'scriptCode' , ( ) => { it ( 'sets as expected' , ( ) => { const expected = new ScriptCodeStub ( ) ; const sut = new ScriptBuilder ( ) . withCode ( expected ) . build ( ) ; const actual = sut . code ; expect ( actual ) . to . deep . equal ( expected ) ; } ) ; it ( 'throws if undefined' , ( ) => { const name = 'script-name' ; const expectedError = `undefined code (script: ${ name })` ; const code = undefined ; const construct = ( ) => new ScriptBuilder ( ) . withName ( name ) . withCode ( code ) . build ( ) ; expect ( construct ) . to . throw ( expectedError ) ; } ) ; } ) ; describe ( 'canRevert' , ( ) => { it ( 'returns false without revert code' , ( ) => { const sut = new ScriptBuilder ( ) . withCodes ( 'code' ) . build ( ) ; const actual = sut . canRevert ( ) ; expect ( actual ) . to . equal ( false ) ; } ) ; it ( 'returns true with revert code' , ( ) => { const sut = new ScriptBuilder ( ) . withCodes ( 'code' , 'non empty revert code' ) . build ( ) ; const actual = sut . canRevert ( ) ; expect ( actual ) . to . equal ( true ) ; } ) ; } ) ; describe ( 'level' , ( ) => { it ( 'cannot construct with invalid wrong value' , ( ) => { const invalidValue = 55 ; const expectedError = 'invalid level' ; const construct = ( ) => new ScriptBuilder ( ) . withRecommendationLevel ( invalidValue ) . build ( ) ; expect ( construct ) . to . throw ( expectedError ) ; } ) ; it ( 'sets undefined as expected' , ( ) => { const expected = undefined ; const sut = new ScriptBuilder ( ) . withRecommendationLevel ( expected ) . build ( ) ; expect ( sut . level ) . to . equal ( expected ) ; } ) ; it ( 'sets as expected' , ( ) => { for ( const expected of getEnumValues ( RecommendationLevel ) ) { const sut = new ScriptBuilder ( ) . withRecommendationLevel ( expected ) . build ( ) ; const actual = sut . level ; expect ( actual ) . to . equal ( expected ) ; } } ) ; } ) ; describe ( 'documentationUrls' , ( ) => { it ( 'sets as expected' , ( ) => { const expected = [ 'doc1' , 'doc2' ] ; const sut = new ScriptBuilder ( ) . withDocumentationUrls ( expected ) . build ( ) ; const actual = sut . documentationUrls ; expect ( actual ) . to . equal ( expected ) ; } ) ; } ) ; } ) ; } ) ; class ScriptBuilder { private name = 'test-script' ; private code : IScriptCode = new ScriptCodeStub ( ) ; private level = RecommendationLevel . Standard ; private documentationUrls : readonly string [ ] = undefined ; public withCodes ( code , revertCode = '' ) { this . code = new ScriptCodeStub ( ) . withExecute ( code ) . withRevert ( revertCode ) ; return this ; } public withCode ( code ) { this . code = code ; return this ; } public withName ( name ) { this . name = name ; return this ; } public withRecommendationLevel ( level ) { this . level = level ; return this ; } public withDocumentationUrls ( urls ) { this . documentationUrls = urls ; return this ; } public build ( ) { return new Script ( this . name , this . code , this . documentationUrls , this . level ) ; } }