Fix broken OutletTests, found and fixed bug with ancestor searching.

This commit is contained in:
Jesse Brault 2025-01-27 14:37:24 -06:00
parent 2b3cd3120c
commit f6071909b6
2 changed files with 25 additions and 4 deletions

View File

@ -70,7 +70,10 @@ public class DefaultComponentContext implements ComponentContext {
public @Nullable ViewComponent findNearestAncestor(Predicate<? super ViewComponent> matching) {
final List<ViewComponent> componentStack = this.getRenderContext().getComponentStack();
if (componentStack.size() > 1) {
for (final var ancestor : componentStack.subList(1, componentStack.size() - 1)) {
// 1/27/25: earlier this was originally componentStack.size() - 1 as the second argument to sublist().
// On examination today, it didn't make sense, because it would be chopping off the farthest ancestor from
// search. So I removed it, in accordance with the implementation in hasAncestor().
for (final var ancestor : componentStack.subList(1, componentStack.size())) {
if (matching.test(ancestor)) {
return ancestor;
}
@ -83,7 +86,7 @@ public class DefaultComponentContext implements ComponentContext {
public boolean hasAncestor(Predicate<? super ViewComponent> matching) {
final List<ViewComponent> componentStack = this.getRenderContext().getComponentStack();
if (componentStack.size() > 1) {
for (final var ancestor : componentStack.subList(1, componentStack.size() - 1)) {
for (final var ancestor : componentStack.subList(1, componentStack.size())) {
if (matching.test(ancestor)) {
return true;
}

View File

@ -1,17 +1,35 @@
package groowt.view.component.web.lib
import groowt.view.component.web.WebViewComponentContext
import groowt.view.component.web.WebViewComponentScope
import org.junit.jupiter.api.Test
class OutletTests extends AbstractWebViewComponentTests {
static final class DummyOutletContainer extends Echo implements OutletContainer {
DummyOutletContainer() {
super([:])
}
}
@Override
void configureContext(WebViewComponentContext context) {
context.configureRootScope(WebViewComponentScope) {
addWithNoArgConstructor(DummyOutletContainer)
}
}
@Test
void smokeScreen() {
doTest('<Outlet />', '')
doTest('<OutletTests.DummyOutletContainer><Outlet /></OutletTests.DummyOutletContainer>', '')
}
@Test
void withChildren() {
doTest('<Echo items={[0, 1, 2]}><Outlet children={items} /></Echo>', '012')
doTest('<OutletTests.DummyOutletContainer><Echo items={[0, 1, 2]}><Outlet children={items} /></Echo></OutletTests.DummyOutletContainer>', '012')
}
}