import 'mocha' ; import { expect } from 'chai' ; import { PipeFactory } from '@/application/Parser/Script/Compiler/Expressions/Pipes/PipeFactory' ; import { PipeStub } from '@tests/unit/stubs/PipeStub' ; describe ( 'PipeFactory' , ( ) => { describe ( 'ctor' , ( ) => { it ( 'throws when instances with same name is registered' , ( ) => { const duplicateName = 'duplicateName' ; const expectedError = `Pipe name must be unique: "${ duplicateName }"` ; const pipes = [ new PipeStub ( ) . withName ( duplicateName ) , new PipeStub ( ) . withName ( 'uniqueName' ) , new PipeStub ( ) . withName ( duplicateName ) , ] ; const act = ( ) => new PipeFactory ( pipes ) ; expect ( act ) . to . throw ( expectedError ) ; } ) ; it ( 'throws when a pipe is undefined' , ( ) => { const expectedError = 'undefined pipe in list' ; const pipes = [ new PipeStub ( ) , undefined ] ; const act = ( ) => new PipeFactory ( pipes ) ; expect ( act ) . to . throw ( expectedError ) ; } ) ; describe ( 'throws when name is invalid' , ( ) => { const act = ( invalidName ) => new PipeFactory ( [ new PipeStub ( ) . withName ( invalidName ) ] ) ; testPipeNameValidation ( act ) ; } ) ; } ) ; describe ( 'get' , ( ) => { describe ( 'throws when name is invalid' , ( ) => { const sut = new PipeFactory ( ) ; const act = ( invalidName ) => sut . get ( invalidName ) ; testPipeNameValidation ( act ) ; } ) ; it ( 'gets registered instance when it exists' , ( ) => { const expected = new PipeStub ( ) . withName ( 'expectedName' ) ; const pipes = [ expected , new PipeStub ( ) . withName ( 'instanceToConfuse' ) ] ; const sut = new PipeFactory ( pipes ) ; const actual = sut . get ( expected . name ) ; expect ( actual ) . to . equal ( expected ) ; } ) ; it ( 'throws when instance does not exist' , ( ) => { const missingName = 'missingName' ; const expectedError = `Unknown pipe: "${ missingName }"` ; const pipes = [ ] ; const sut = new PipeFactory ( pipes ) ; const act = ( ) => sut . get ( missingName ) ; expect ( act ) . to . throw ( expectedError ) ; } ) ; } ) ; } ) ; function testPipeNameValidation ( testRunner ) { const testCases = [ { exceptionBuilder : ( ) => 'empty pipe name' , values : [ null , undefined , '' ] , } , { exceptionBuilder : ( name ) => `Pipe name should be camelCase: "${ name }"` , values : [ 'PascalCase' , 'snake-case' , 'includesNumb3rs' , 'includes Whitespace' , 'noSpec\'ial' , ] , } , ] ; for ( const testCase of testCases ) { for ( const invalidName of testCase . values ) { it ( `invalid name (${ printValue ( invalidName ) }) throws` , ( ) => { const expectedError = testCase . exceptionBuilder ( invalidName ) ; const act = ( ) => testRunner ( invalidName ) ; expect ( act ) . to . throw ( expectedError ) ; } ) ; } } } function printValue ( value ) { switch ( value ) { case undefined : return 'undefined' ; case null : return 'null' ; case '' : return 'empty' ; default : return value ; } }