Skip to content

Commit

Permalink
add isObject and isCustomFunction checks & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mrigsby committed Oct 25, 2023
1 parent 3c0fd28 commit f2581c8
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 3 deletions.
10 changes: 7 additions & 3 deletions models/concerns/OnMountConcern.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@ component accessors="true" singleton {

// check datatypes parameters and throw error if not string, boolean, numeric, date, array, or struct
for( var paramKey IN arguments.parameters.keyArray() ){
if( !isSimpleValue( arguments.parameters[ paramKey ] )
&& !isArray( arguments.parameters[ paramKey ] )
&& !isStruct( arguments.parameters[ paramKey ] )
if( (
!isSimpleValue( arguments.parameters[ paramKey ] )
&& !isArray( arguments.parameters[ paramKey ] )
&& !isStruct( arguments.parameters[ paramKey ] )
)
|| isObject( arguments.parameters[ paramKey ] )
|| isCustomFunction( arguments.parameters[ paramKey ] )
){
throw(
type = "MissingOnMount",
Expand Down
82 changes: 82 additions & 0 deletions test-harness/tests/specs/CBWIRESpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,88 @@ component extends="coldbox.system.testing.BaseTestCase" {
expect( event.getRenderedContent() ).notToContain( "<!-- CBWIRE Scripts -->" );
} );
} );

describe( "Component without onMount", function(){

it( "can render with no parameters passed", function() {
var cbwireService = prepareMock( getInstance( "CBWIREService@cbwire" ) );
var result = cbwireService.wire( "tests.templates.withoutOnMountEvent" );
expect( result ).toContain( "COMPLETED-PROCESSING" );
} );

it( "can render with empty struct parameters passed", function() {
var cbwireService = prepareMock( getInstance( "CBWIREService@cbwire" ) );
var result = cbwireService.wire( "tests.templates.withoutOnMountEvent", {} );
expect( result ).toContain( "COMPLETED-PROCESSING" );
} );

it( "throws MissingOnMount when parameters contains value of datatype other than string, boolean, numeric, date, array, or struct (java object)", function(){
var cbwireService = prepareMock( getInstance( "CBWIREService@cbwire" ) );
var javaObj = createObject( "java", "java.lang.StringBuilder" ).init( "" );
expect( function() {
var result = cbwireService.wire(
"tests.templates.withoutOnMountEvent",
{
"testString" : "String Value",
"javaObj" : javaObj
}
);
}).toThrow( type="MissingOnMount" );
} );

it( "throws MissingOnMount when parameters contains value of datatype other than string, boolean, numeric, date, array, or struct (cfcomponent)", function(){
var cbwireService = prepareMock( getInstance( "CBWIREService@cbwire" ) );
var basicCFComponent = new tests.templates.basicCFComponent();
expect( function() {
var result = cbwireService.wire(
"tests.templates.withoutOnMountEvent",
{
"testString" : "String Value",
"basicCFComponent" : basicCFComponent
}
);
}).toThrow( type="MissingOnMount" );
} );

it( "throws MissingOnMount when parameters contains value of datatype other than string, boolean, numeric, date, array, or struct (user defined function)", function(){
var cbwireService = prepareMock( getInstance( "CBWIREService@cbwire" ) );
var myUDF = function(){
return "Hello Testbox!";
};
expect( function() {
var result = cbwireService.wire(
"tests.templates.withoutOnMountEvent",
{
"testString" : "String Value",
"myUDF" : myUDF
}
);
}).toThrow( type="MissingOnMount" );
} );

it( "can render with parameters of types string, boolean, numeric, date, array, or struct and merge with pre-existing data properties", function() {
var cbwireService = prepareMock( getInstance( "CBWIREService@cbwire" ) );
var result = cbwireService.wire(
"tests.templates.withoutOnMountEvent",
{
"testString" : "String Value",
"testArray" : [ "value1", "value2", "value3" ],
"testStruct" : { "keyOne" : 9, "keyTwo" : true, "keyThree" : "Test struct key three text" },
"testNumber" : 5678,
"testBoolean" : true,
"testDate" : now()
}
);
expect( result ).toContain( 'variables.data.testString = "String Value"' );
expect( result ).toContain( "variables.data.preDefinedNumber = 1234" );
expect( result ).toContain( "variables.data.testNumber = 5678" );
expect( result ).toContain( 'variables.testString = "String Value"' );
expect( result ).toContain( "variables.preDefinedNumber = 1234" );
expect( result ).toContain( "variables.testNumber = 5678" );
expect( result ).toContain( "COMPLETED-PROCESSING" );
} );

});
}

private function renderInitial( comp ) {
Expand Down
11 changes: 11 additions & 0 deletions test-harness/tests/templates/basicCFComponent.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
component displayname="basicCFComponent" output="false" {

public basicCFComponent function init() {
return this;
}

public string function helloTestBox(){
return "<h1>Hello Testbox!</h1>"
}

}
26 changes: 26 additions & 0 deletions test-harness/tests/templates/withoutOnMountEvent.cfm
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<cfscript>
data = {
"preDefinedString" : "Pre Defined String Value",
"preDefinedNumber" : 1234
};
</cfscript>

<cfoutput>
<div>
<h1>Test Wire Component Without OnMount() Event</h1>

<h5>Output from variables.data</h5>
<cfloop item="key" collection=#variables.data# >
variables.data.#key# = #serializeJSON( variables.data[key] )#<br/>
</cfloop>
<hr>

<h5>Output from variables</h5>
<cfloop item="key" collection=#variables.data# >
variables.#key# = #serializeJSON( variables[key] )#<br/>
</cfloop>
<hr>

COMPLETED-PROCESSING
</div>
</cfoutput>

0 comments on commit f2581c8

Please sign in to comment.