Skip to content

Commit 2710fa8

Browse files
docs: add bindings API to docs (#1509)
1 parent 72c2243 commit 2710fa8

File tree

2 files changed

+69
-9
lines changed

2 files changed

+69
-9
lines changed

docs/angular-testing-library/api.mdx

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,63 @@ await render(AppComponent, {
120120
})
121121
```
122122

123+
### `bindings`
124+
125+
An array of bindings to apply to the component using Angular's native bindings API. This provides a more direct way to bind inputs and outputs compared to the `inputs` and `on` options. The bindings API uses Angular's `inputBinding`, `outputBinding`, and `twoWayBinding` functions from `@angular/core`.
126+
127+
**default** : `[]`
128+
129+
```typescript
130+
import { inputBinding, outputBinding, twoWayBinding, signal } from '@angular/core'
131+
132+
await render(AppComponent, {
133+
bindings: [
134+
// Bind a static input value
135+
inputBinding('greeting', () => 'Hello'),
136+
137+
// Bind a signal as an input
138+
inputBinding('age', () => 25),
139+
140+
// Two-way binding with a signal
141+
twoWayBinding('name', signal('John')),
142+
143+
// Output binding with a callback
144+
outputBinding('submitValue', (event) => console.log(event)),
145+
],
146+
})
147+
```
148+
149+
**Using signals for reactive updates**:
150+
151+
```typescript
152+
const greetingSignal = signal('Good day')
153+
const nameSignal = signal('David')
154+
const ageSignal = signal(35)
155+
156+
const { fixture } = await render(AppComponent, {
157+
bindings: [
158+
inputBinding('greeting', greetingSignal),
159+
inputBinding('age', ageSignal),
160+
twoWayBinding('name', nameSignal),
161+
],
162+
})
163+
164+
// Update signals externally
165+
greetingSignal.set('Good evening')
166+
```
167+
168+
**Handling outputs**:
169+
170+
```typescript
171+
const submitHandler = jest.fn()
172+
173+
await render(AppComponent, {
174+
bindings: [
175+
outputBinding('submit', submitHandler),
176+
],
177+
})
178+
```
179+
123180
### `declarations`
124181

125182
A collection of components, directives and pipes needed to render the component.

docs/angular-testing-library/examples.mdx

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,28 @@ export class CounterComponent {
3737
```
3838

3939
```typescript title="counter.component.spec.ts"
40-
import { render, screen, fireEvent, aliasedInput } from '@testing-library/angular';
40+
import { signal, inputBinding, twoWayBinding } from '@angular/core';
41+
import { render, screen, fireEvent } from '@testing-library/angular';
4142
import { CounterComponent } from './counter.component';
4243

4344
describe('Counter', () => {
44-
it('should render counter', async () => {
45+
it('renders counter', async () => {
4546
await render(CounterComponent, {
46-
inputs: {
47-
counter: 5,
48-
// aliases need to be specified using aliasedInput
49-
...aliasedInput('greeting', 'Hello Alias!'),
50-
},
47+
bindings: [
48+
twoWayBinding('counter', signal(5)),
49+
// aliases are handled naturally with inputBinding
50+
inputBinding('greeting', () => 'Hello Alias!'),
51+
],
5152
});
5253

5354
expect(screen.getByText('Current Count: 5')).toBeVisible();
5455
expect(screen.getByText('Hello Alias!')).toBeVisible();
5556
});
5657

57-
it('should increment the counter on click', async () => {
58-
await render(CounterComponent, { inputs: { counter: 5 } });
58+
it('increments the counter on click', async () => {
59+
await render(CounterComponent, {
60+
bindings: [twoWayBinding('counter', signal(5))],
61+
});
5962

6063
const incrementButton = screen.getByRole('button', { name: '+' });
6164
fireEvent.click(incrementButton);

0 commit comments

Comments
 (0)