From d8adffe40a944d3ff76a2486dde93181a6da93f5 Mon Sep 17 00:00:00 2001 From: "Michael V. Rigsby" Date: Fri, 21 Jun 2024 23:07:25 -0400 Subject: [PATCH] add additional check in _hydrate() loop over data properties and tests --- models/Component.cfc | 2 +- test-harness/tests/specs/CBWIRESpec.cfc | 39 +++++++++++++++++++++++ test-harness/wires/dataWithStructWire.cfc | 20 ++++++++++++ test-harness/wires/dataWithStructWire.cfm | 14 ++++++++ 4 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 test-harness/wires/dataWithStructWire.cfc create mode 100644 test-harness/wires/dataWithStructWire.cfm diff --git a/models/Component.cfc b/models/Component.cfc index 475dd23..04c701f 100644 --- a/models/Component.cfc +++ b/models/Component.cfc @@ -585,7 +585,7 @@ component output="true" { Provide file uploads to view */ variables.data.each( function( key, value ) { - if ( isArray( arguments.value ) && arguments.value.len() && arguments.value[ 1 ] contains "fileupload:" ) { + if ( isArray( arguments.value ) && arguments.value.len() && isSimpleValue( arguments.value[ 1 ] ) && arguments.value[ 1 ] contains "fileupload:" ) { // This property is holding an array of file uploads. value.each( function( uuid, index ) { local.fileUpload = getInstance( dsl="FileUpload@cbwire" ).load( diff --git a/test-harness/tests/specs/CBWIRESpec.cfc b/test-harness/tests/specs/CBWIRESpec.cfc index 4a70a5a..e258768 100644 --- a/test-harness/tests/specs/CBWIRESpec.cfc +++ b/test-harness/tests/specs/CBWIRESpec.cfc @@ -253,6 +253,21 @@ component extends="coldbox.system.testing.BaseTestCase" { } } ); + it( "should handle a data property that is an array of structs", function(){ + dataWithStructComponent = getInstance("wires.dataWithStructWire")._withEvent( getRequestContext( ) ); + prepareMock( dataWithStructComponent ); + // add aditional states + dataWithStructComponent.onMount(); + dataWithStructComponent.addState( "IA", "Iowa" ); + dataWithStructComponent.addState( "CA", "California" ); + // verify data struct read and write without errors + var structViewContent = dataWithStructComponent.view("wires.dataWithStructWire"); + expect(structViewContent).toInclude("Number Of States In data.states: 6"); + expect(structViewContent).toInclude("IA : Iowa"); + expect(structViewContent).toInclude("CA : California"); + + } ); + }); describe("Incoming Requests", function() { @@ -651,6 +666,30 @@ component extends="coldbox.system.testing.BaseTestCase" { expect( snapshot.data.modules[ 2 ] ).toBe( "CBORM" ); } ); + it( "should handle incoming reqeust when a data property contains an array of structs", function() { + var payload = incomingRequest( + memo = { + "name": "dataWithStructWire", + "id": "Z1Ruz1tGMPXSfw7osBW2", + "children": [] + }, + data = { + "title": "CBWIRE Rocks!", + "states": [ + { "name" : "Maryland", "abr" : "MD" }, + { "name" : "Virginia", "abr" : "VA" }, + { "name" : "Florida", "abr" : "FL" }, + { "name" : "Wyoming", "abr" : "WY" } + ] + }, + calls = [], + updates = { + "title": "CBWIRE Slaps!" + } + ); + var response = cbwireController.handleRequest( payload, event ); + expect( response.components[1].effects.html ).toInclude( "CBWIRE Slaps!" ); + } ); } ); describe("File Uploads", function() { diff --git a/test-harness/wires/dataWithStructWire.cfc b/test-harness/wires/dataWithStructWire.cfc new file mode 100644 index 0000000..f9d99d8 --- /dev/null +++ b/test-harness/wires/dataWithStructWire.cfc @@ -0,0 +1,20 @@ +component extends="cbwire.models.Component" { + + data = [ + "title": "CBWIRE Rocks!", + "states": [] + ]; + + function addState( abr, name ){ + data.states.append( { "name" : name, "abr" : abr } ); + } + + function onMount( params, event, rc, prc ) { + // Initialize some states + data.states.append( { "name" : "Maryland", "abr" : "MD" } ); + data.states.append( { "name" : "Virginia", "abr" : "VA" } ); + data.states.append( { "name" : "Florida", "abr" : "FL" } ); + data.states.append( { "name" : "Wyoming", "abr" : "WY" } ); + } + +} \ No newline at end of file diff --git a/test-harness/wires/dataWithStructWire.cfm b/test-harness/wires/dataWithStructWire.cfm new file mode 100644 index 0000000..128adb5 --- /dev/null +++ b/test-harness/wires/dataWithStructWire.cfm @@ -0,0 +1,14 @@ + +
+
#args.title#
+

States

+

Number Of States In data.states: #states.len()#

+ +
    + +
  • #currentState.abr# : #currentState.name#
  • +
    +
+
+
+
\ No newline at end of file