-
Notifications
You must be signed in to change notification settings - Fork 93
Add support for Angular bindings API with twoWayBinding #547
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
Merged
timdeschryver
merged 7 commits into
main
from
copilot/fix-e93f4325-fc7e-4d7a-b6a8-9ff4df8eb8d9
Sep 29, 2025
+389
−5
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b815a18
Initial plan
Copilot 6a6d336
Add support for Angular v20 bindings API
Copilot 3dca2ea
Add documentation for Angular v20 bindings API
Copilot ec079c2
Address review feedback: Remove re-exports, enable mixed usage with w…
Copilot 39fe933
Add twoWayBinding support and writable signal test cases
Copilot 04e96a2
manual tweaks
timdeschryver c44861e
add tests for the example component
timdeschryver File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
147 changes: 147 additions & 0 deletions
147
apps/example-app/src/app/examples/24-bindings-api.component.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
import { signal, inputBinding, outputBinding, twoWayBinding } from '@angular/core'; | ||
import { render, screen } from '@testing-library/angular'; | ||
import { BindingsApiExampleComponent } from './24-bindings-api.component'; | ||
|
||
test('displays computed greeting message with input values', async () => { | ||
await render(BindingsApiExampleComponent, { | ||
bindings: [ | ||
inputBinding('greeting', () => 'Hello'), | ||
inputBinding('age', () => 25), | ||
twoWayBinding('name', signal('John')), | ||
], | ||
}); | ||
|
||
expect(screen.getByTestId('input-value')).toHaveTextContent('Hello John of 25 years old'); | ||
expect(screen.getByTestId('computed-value')).toHaveTextContent('Hello John of 25 years old'); | ||
expect(screen.getByTestId('current-age')).toHaveTextContent('Current age: 25'); | ||
}); | ||
|
||
test('emits submitValue output when submit button is clicked', async () => { | ||
const submitHandler = jest.fn(); | ||
const nameSignal = signal('Alice'); | ||
|
||
await render(BindingsApiExampleComponent, { | ||
bindings: [ | ||
inputBinding('greeting', () => 'Good morning'), | ||
inputBinding('age', () => 28), | ||
twoWayBinding('name', nameSignal), | ||
outputBinding('submitValue', submitHandler), | ||
], | ||
}); | ||
|
||
const submitButton = screen.getByTestId('submit-button'); | ||
submitButton.click(); | ||
expect(submitHandler).toHaveBeenCalledWith('Alice'); | ||
}); | ||
|
||
test('emits ageChanged output when increment button is clicked', async () => { | ||
const ageChangedHandler = jest.fn(); | ||
|
||
await render(BindingsApiExampleComponent, { | ||
bindings: [ | ||
inputBinding('greeting', () => 'Hi'), | ||
inputBinding('age', () => 20), | ||
twoWayBinding('name', signal('Charlie')), | ||
outputBinding('ageChanged', ageChangedHandler), | ||
], | ||
}); | ||
|
||
const incrementButton = screen.getByTestId('increment-button'); | ||
incrementButton.click(); | ||
|
||
expect(ageChangedHandler).toHaveBeenCalledWith(21); | ||
}); | ||
|
||
test('updates name through two-way binding when input changes', async () => { | ||
const nameSignal = signal('Initial Name'); | ||
|
||
await render(BindingsApiExampleComponent, { | ||
bindings: [ | ||
inputBinding('greeting', () => 'Hello'), | ||
inputBinding('age', () => 25), | ||
twoWayBinding('name', nameSignal), | ||
], | ||
}); | ||
|
||
const nameInput = screen.getByTestId('name-input') as HTMLInputElement; | ||
|
||
// Verify initial value | ||
expect(nameInput.value).toBe('Initial Name'); | ||
expect(screen.getByTestId('input-value')).toHaveTextContent('Hello Initial Name of 25 years old'); | ||
|
||
// Update the signal externally | ||
nameSignal.set('Updated Name'); | ||
|
||
// Verify the input and display update | ||
expect(await screen.findByDisplayValue('Updated Name')).toBeInTheDocument(); | ||
expect(screen.getByTestId('input-value')).toHaveTextContent('Hello Updated Name of 25 years old'); | ||
expect(screen.getByTestId('computed-value')).toHaveTextContent('Hello Updated Name of 25 years old'); | ||
}); | ||
|
||
test('updates computed value when inputs change', async () => { | ||
const greetingSignal = signal('Good day'); | ||
const nameSignal = signal('David'); | ||
const ageSignal = signal(35); | ||
|
||
const { fixture } = await render(BindingsApiExampleComponent, { | ||
bindings: [ | ||
inputBinding('greeting', greetingSignal), | ||
inputBinding('age', ageSignal), | ||
twoWayBinding('name', nameSignal), | ||
], | ||
}); | ||
|
||
// Initial state | ||
expect(screen.getByTestId('computed-value')).toHaveTextContent('Good day David of 35 years old'); | ||
|
||
// Update greeting | ||
greetingSignal.set('Good evening'); | ||
fixture.detectChanges(); | ||
expect(screen.getByTestId('computed-value')).toHaveTextContent('Good evening David of 35 years old'); | ||
|
||
// Update age | ||
ageSignal.set(36); | ||
fixture.detectChanges(); | ||
expect(screen.getByTestId('computed-value')).toHaveTextContent('Good evening David of 36 years old'); | ||
|
||
// Update name | ||
nameSignal.set('Daniel'); | ||
fixture.detectChanges(); | ||
expect(screen.getByTestId('computed-value')).toHaveTextContent('Good evening Daniel of 36 years old'); | ||
}); | ||
|
||
test('handles multiple output emissions correctly', async () => { | ||
const submitHandler = jest.fn(); | ||
const ageChangedHandler = jest.fn(); | ||
const nameSignal = signal('Emma'); | ||
|
||
await render(BindingsApiExampleComponent, { | ||
bindings: [ | ||
inputBinding('greeting', () => 'Hey'), | ||
inputBinding('age', () => 22), | ||
twoWayBinding('name', nameSignal), | ||
outputBinding('submitValue', submitHandler), | ||
outputBinding('ageChanged', ageChangedHandler), | ||
], | ||
}); | ||
|
||
// Click submit button multiple times | ||
const submitButton = screen.getByTestId('submit-button'); | ||
submitButton.click(); | ||
submitButton.click(); | ||
|
||
expect(submitHandler).toHaveBeenCalledTimes(2); | ||
expect(submitHandler).toHaveBeenNthCalledWith(1, 'Emma'); | ||
expect(submitHandler).toHaveBeenNthCalledWith(2, 'Emma'); | ||
|
||
// Click increment button multiple times | ||
const incrementButton = screen.getByTestId('increment-button'); | ||
incrementButton.click(); | ||
incrementButton.click(); | ||
incrementButton.click(); | ||
|
||
expect(ageChangedHandler).toHaveBeenCalledTimes(3); | ||
expect(ageChangedHandler).toHaveBeenNthCalledWith(1, 23); | ||
expect(ageChangedHandler).toHaveBeenNthCalledWith(2, 23); // Still 23 because age input doesn't change | ||
expect(ageChangedHandler).toHaveBeenNthCalledWith(3, 23); | ||
}); |
36 changes: 36 additions & 0 deletions
36
apps/example-app/src/app/examples/24-bindings-api.component.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Component, computed, input, model, numberAttribute, output } from '@angular/core'; | ||
import { FormsModule } from '@angular/forms'; | ||
|
||
@Component({ | ||
selector: 'atl-bindings-api-example', | ||
template: ` | ||
<div data-testid="input-value">{{ greetings() }} {{ name() }} of {{ age() }} years old</div> | ||
<div data-testid="computed-value">{{ greetingMessage() }}</div> | ||
<button data-testid="submit-button" (click)="submitName()">Submit</button> | ||
<button data-testid="increment-button" (click)="incrementAge()">Increment Age</button> | ||
<input type="text" data-testid="name-input" [(ngModel)]="name" /> | ||
<div data-testid="current-age">Current age: {{ age() }}</div> | ||
`, | ||
standalone: true, | ||
imports: [FormsModule], | ||
}) | ||
export class BindingsApiExampleComponent { | ||
greetings = input<string>('', { | ||
alias: 'greeting', | ||
}); | ||
age = input.required<number, string>({ transform: numberAttribute }); | ||
name = model.required<string>(); | ||
submitValue = output<string>(); | ||
ageChanged = output<number>(); | ||
|
||
greetingMessage = computed(() => `${this.greetings()} ${this.name()} of ${this.age()} years old`); | ||
|
||
submitName() { | ||
this.submitValue.emit(this.name()); | ||
} | ||
|
||
incrementAge() { | ||
const newAge = this.age() + 1; | ||
this.ageChanged.emit(newAge); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.