Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically populate data properties with passed parameters when onMount() isn't defined #132

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions models/concerns/OnMountConcern.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,34 @@ component accessors="true" singleton {
);
} else {
/**
* Use setter population to populate our component.
* Use scope population to populate our component.
*/

// 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 ] )
)
|| isObject( arguments.parameters[ paramKey ] )
|| isCustomFunction( arguments.parameters[ paramKey ] )
){
throw(
type = "MissingOnMount",
message = "The wire does NOT have onMount() function and the paramaters passed contain a
data type other than a string, boolean, numeric, date, array, or struct and cannot
be automatically inserted into the data properties. To avoid this error create an
onMount() method to handle incoming paramaters"
);
}
}

// if parameters passed in are string, boolean, numeric, date, array, or struct insert into variables.data
getPopulator().populateFromStruct(
target: this,
trustedSetter: true,
target: comp.getParent(),
memento: arguments.parameters,
excludes: ""
scope: "variables.data"
);
}

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>
Loading