Skip to content

Fix children prop bug in the v0.9 Angular ComponentBinder and improve unit tests to use real models#1472

Merged
josemontespg merged 12 commits into
google:mainfrom
josemontespg:angular-binder-unit-tests
May 21, 2026
Merged

Fix children prop bug in the v0.9 Angular ComponentBinder and improve unit tests to use real models#1472
josemontespg merged 12 commits into
google:mainfrom
josemontespg:angular-binder-unit-tests

Conversation

@josemontespg
Copy link
Copy Markdown
Collaborator

@josemontespg josemontespg commented May 20, 2026

Description

This PR refactors the Angular ComponentBinder unit tests for the v0.9 specification to use real, live instances of ComponentContext, ComponentModel, and SurfaceModel from @a2ui/web_core, completely eliminating fragile, verbose, and easily out-of-date mocks.

It also addresses two critical bugs identified in the binder service implementation and adds robust validation coverage.

Motivation

The primary motivation behind this refactoring is to create a highly reliable, realistic safety net. This enables future refactoring of the ComponentBinder implementation with complete confidence, ensuring that its behavior remains completely unchanged and fully compliant with the v0.9 protocol specification.

Key Changes & Added Test Cases

  • Real Implementations Integration: Added a clean, reusable test helper to construct real contexts and dynamic state. Assertions are verified directly against the actual, reactive DataModel state rather than call spies.
  • Extensive Binding Coverage: Added detailed test cases covering all possible binding styles from the spec file:
    • Basic Literals: strings, numbers, booleans, objects, and nulls.
    • Two-way bindings: validating reactive propagation of updates back up to the DataModel.
    • Single Child references: handling child, trigger, and content with robust fallback logic for falsy values and pre-existing Child structures.
    • Children lists: covering static child ID arrays, dynamic lists, custom Child object arrays, and template-based ChildList expansions.
    • Checks validation: verifying dynamic validation updates and multiple error outputs against the actual state.

Important Bug Fixes Included

  1. Loop Variable Leak (Template Leak):
    • Bug: The template variable was declared outside the properties loop. When processing a component with a children list that defined a template, that template would get leaked and incorrectly attached to all subsequent properties.
    • Fix: Moved the template declaration inside the for (const key of Object.keys(props)) loop so it is properly reset to undefined for each property.
    • Coverage: Added a dedicated unit test should not leak template to subsequent properties to prevent regression.
  2. Children Property Null Pointer:
    • Bug: Optional children properties resolving to null or undefined would throw a TypeError during componentId resolution in ComponentBinder.bind.
    • Fix: Added safe optional chaining (value?.componentId and value?.path).
    • Coverage: Covered via should handle null/falsy or non-array values by returning an empty array.

Fixes #1477

…odels

Refactored the ComponentBinder unit tests to use real, live instances of ComponentContext, ComponentModel, and SurfaceModel instead of fragile and verbose mocks.

Additional extensive test cases have been added to cover all possible binding declarations specified in the A2UI v0.9 protocol:
- Basic literal property bindings (strings, numbers, booleans, objects, null)
- Reactive path-based property bindings (with verification against the actual DataModel)
- Single child/trigger/content bindings with fallback handling for falsy values and existing Child objects
- Extensive children list bindings covering static array of string IDs, resolved dynamic path array elements, pre-formatted Child objects, and template-based ChildList object expansions
- Robust checks validation, testing dynamic reactivity and evaluation of multiple validation errors directly against the DataModel state

This refactoring fixes a potential null-pointer TypeError bug when resolving children properties with null/falsy values. Improving these unit tests with real implementations provides a highly reliable safety net for future refactoring of the binder implementation while ensuring its behavior remains unchanged.
@github-project-automation github-project-automation Bot moved this to Todo in A2UI May 20, 2026
@josemontespg josemontespg marked this pull request as ready for review May 20, 2026 18:24
@josemontespg josemontespg requested a review from ditman May 20, 2026 18:24
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the ComponentBinder test suite by introducing a helper function for context creation and significantly expanding test coverage for property binding, child resolution, and validation logic. In the service itself, optional chaining was added for children property access. Feedback includes a critical bug report regarding a variable leak in the property binding loop, a potential memory leak due to the service's lifecycle management, and a suggestion to return a promise in a test helper to prevent runtime errors.

Comment thread renderers/angular/src/v0_9/core/component-binder.service.ts
Comment thread renderers/angular/src/v0_9/core/component-binder.service.spec.ts
Comment thread renderers/angular/src/v0_9/core/component-binder.service.spec.ts Outdated
@josemontespg josemontespg enabled auto-merge (squash) May 20, 2026 18:25
@josemontespg josemontespg force-pushed the angular-binder-unit-tests branch from e6ff9fb to 8bdf542 Compare May 20, 2026 18:40
@josemontespg josemontespg requested a review from andrewkolos May 20, 2026 21:42
Comment thread renderers/angular/src/v0_9/core/component-binder.service.ts
Copy link
Copy Markdown
Collaborator

@andrewkolos andrewkolos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change looks good to me, and I verified that the new tests fail before the change 👍 . However, I'm withholding approval per ditman's comment. Feel free to re-request review when addressed

Comment thread renderers/angular/src/v0_9/core/component-binder.service.ts
@josemontespg
Copy link
Copy Markdown
Collaborator Author

This PR fixes the null de-referencing TypeError when is null/undefined (tracked in #1477).

@josemontespg
Copy link
Copy Markdown
Collaborator Author

Thanks for the review! Ready for another pass

@josemontespg josemontespg requested a review from ditman May 20, 2026 23:07
@josemontespg josemontespg disabled auto-merge May 20, 2026 23:07
@josemontespg josemontespg enabled auto-merge (squash) May 20, 2026 23:07
@josemontespg josemontespg disabled auto-merge May 20, 2026 23:09
Copy link
Copy Markdown
Collaborator

@ditman ditman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for finding and fixing this! Let's go!

@ditman
Copy link
Copy Markdown
Collaborator

ditman commented May 20, 2026

Before you land this, please rename the PR to address what the bug is fixing, it's more than tests now!

@josemontespg josemontespg changed the title test(angular): refactor v0.9 ComponentBinder unit tests to use real models Fix children prop bug in the v0.9 Angular ComponentBinder and improve unit tests to use real models May 21, 2026
@josemontespg josemontespg merged commit ebb474b into google:main May 21, 2026
18 checks passed
@github-project-automation github-project-automation Bot moved this from Todo to Done in A2UI May 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[Angular] TypeError: Cannot read properties of null (reading 'componentId') in ComponentBinder when resolving children

3 participants