Skip to content

Commit

Permalink
Ensure lazy loaded child components are detected
Browse files Browse the repository at this point in the history
  • Loading branch information
grantcopley committed Jun 25, 2024
1 parent 6cff648 commit a8667f3
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
13 changes: 12 additions & 1 deletion models/Component.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,18 @@ component output="true" {

// Check if lazy loading is enabled
if ( arguments.lazy ) {
return local.instance._generateXIntersectLazyLoadSnapshot( params=arguments.params );
local.lazyRendering = local.instance._generateXIntersectLazyLoadSnapshot( params=arguments.params );
// Based on the rendering, determine our outer component tag
local.componentTag = _getComponentTag( local.lazyRendering );
// Track the rendered child
variables._children.append( [
"#arguments.key#": [
local.componentTag,
local.instance._getId()
]
] );

return local.lazyRendering;
} else {
// Render it out normally
local.rendering = local.instance._render();
Expand Down
20 changes: 19 additions & 1 deletion test-harness/tests/specs/CBWIRESpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ component extends="coldbox.system.testing.BaseTestCase" {
expect( result ).toInclude( "<p>Child component</p>" );
} );

it( "should return proper names with child components", function() {
fit( "should return proper names with child components", function() {
var result = CBWIREController.wire( "test.should_support_child_components" );
var parent = parseRendering( result, 1 );
var child = parseRendering( result, 2 );
Expand Down Expand Up @@ -932,6 +932,7 @@ component extends="coldbox.system.testing.BaseTestCase" {
setup();
testComponent = getInstance("wires.TestComponent"); // Assuming TestComponent is a component that supports lazy loading
testComponent._withEvent(getRequestContext());
CBWIREController = getInstance( "CBWIREController@cbwire" );
prepareMock(testComponent);
});

Expand All @@ -958,6 +959,23 @@ component extends="coldbox.system.testing.BaseTestCase" {
// Check that the actual component content is not included in the output
expect(lazyHtml).notToInclude("Actual Component Content");
});

it( "should detect lazy loaded children", function() {
var parentHTML = CBWIREController.wire( "test.should_detect_lazy_loaded_children" );
var childHTML = reMatchNoCase( "<!-- start child -->.*<!-- end child -->", parentHTML )[ 1 ];
// Remove child HTML from parent HTML
parentHTML = replaceNoCase( parentHTML, childHTML, "", "one" );
var parent = parseRendering( parentHTML );
var child = parseRendering( childHTML );
// Ensure parent and child ids are different
expect( parent.snapshot.memo.id ).notToBe( child.snapshot.memo.id );
// Ensure parent has a child and it's the lazy loaded child
expect( structCount( parent.snapshot.memo.children ) ).toBe( 1 );
var keys = structKeyArray( parent.snapshot.memo.children );
expect( parent.snapshot.memo.children[ keys[ 1 ] ] ).toBeArray();
expect( parent.snapshot.memo.children[ keys[ 1 ] ][ 1 ] ).toBe( "div" );
expect( parent.snapshot.memo.children[ keys[ 1 ] ][ 2 ] ).toBe( child.snapshot.memo.id );
} );
});

describe("CBWIREController", function() {
Expand Down
2 changes: 1 addition & 1 deletion test-harness/wires/lazyloading.cfm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<h1>Lazy Loading</h1>

<div style="margin-top:500px">
#wire( name="SlowComponent", params={ "sleepTime": 10000 }, lazy=true )#
#wire( name="SlowComponent", params={ "sleepTime": 1000 }, lazy=true )#
</div>
</div>
</cfoutput>
14 changes: 14 additions & 0 deletions test-harness/wires/test/should_detect_lazy_loaded_children.cfm
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<cfscript>
// @startWire
// @endWire
</cfscript>

<cfoutput>
<div>
<h1>Parent</h1>
<!-- start child -->
#wire( name="test.child_component", lazy=true )#
<!-- end child -->
</div>
</cfoutput>

0 comments on commit a8667f3

Please sign in to comment.