Skip to content

Commit 9160da1

Browse files
authored
fix: add support for InputSignalWithTransform in inputs property (#484)
Closes #483
1 parent 8ca97c7 commit 9160da1

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

Diff for: apps/example-app/src/app/examples/22-signal-inputs.component.spec.ts

+16-9
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,45 @@ test('works with signal inputs', async () => {
77
inputs: {
88
...aliasedInput('greeting', 'Hello'),
99
name: 'world',
10+
age: '45',
1011
},
1112
});
1213

1314
const inputValue = within(screen.getByTestId('input-value'));
14-
expect(inputValue.getByText(/hello world/i)).toBeInTheDocument();
15+
expect(inputValue.getByText(/hello world of 45 years old/i)).toBeInTheDocument();
1516
});
1617

1718
test('works with computed', async () => {
1819
await render(SignalInputComponent, {
1920
inputs: {
2021
...aliasedInput('greeting', 'Hello'),
2122
name: 'world',
23+
age: '45',
2224
},
2325
});
2426

2527
const computedValue = within(screen.getByTestId('computed-value'));
26-
expect(computedValue.getByText(/hello world/i)).toBeInTheDocument();
28+
expect(computedValue.getByText(/hello world of 45 years old/i)).toBeInTheDocument();
2729
});
2830

2931
test('can update signal inputs', async () => {
3032
const { fixture } = await render(SignalInputComponent, {
3133
inputs: {
3234
...aliasedInput('greeting', 'Hello'),
3335
name: 'world',
36+
age: '45',
3437
},
3538
});
3639

3740
const inputValue = within(screen.getByTestId('input-value'));
3841
const computedValue = within(screen.getByTestId('computed-value'));
3942

40-
expect(inputValue.getByText(/hello world/i)).toBeInTheDocument();
43+
expect(inputValue.getByText(/hello world of 45 years old/i)).toBeInTheDocument();
4144

4245
fixture.componentInstance.name.set('updated');
4346
// set doesn't trigger change detection within the test, findBy is needed to update the template
44-
expect(await inputValue.findByText(/hello updated/i)).toBeInTheDocument();
45-
expect(await computedValue.findByText(/hello updated/i)).toBeInTheDocument();
47+
expect(await inputValue.findByText(/hello updated of 45 years old/i)).toBeInTheDocument();
48+
expect(await computedValue.findByText(/hello updated of 45 years old/i)).toBeInTheDocument();
4649

4750
// it's not recommended to access the model directly, but it's possible
4851
expect(fixture.componentInstance.name()).toBe('updated');
@@ -54,6 +57,7 @@ test('output emits a value', async () => {
5457
inputs: {
5558
...aliasedInput('greeting', 'Hello'),
5659
name: 'world',
60+
age: '45',
5761
},
5862
on: {
5963
submit: submitFn,
@@ -70,6 +74,7 @@ test('model update also updates the template', async () => {
7074
inputs: {
7175
...aliasedInput('greeting', 'Hello'),
7276
name: 'initial',
77+
age: '45',
7378
},
7479
});
7580

@@ -100,22 +105,24 @@ test('works with signal inputs, computed values, and rerenders', async () => {
100105
inputs: {
101106
...aliasedInput('greeting', 'Hello'),
102107
name: 'world',
108+
age: '45',
103109
},
104110
});
105111

106112
const inputValue = within(screen.getByTestId('input-value'));
107113
const computedValue = within(screen.getByTestId('computed-value'));
108114

109-
expect(inputValue.getByText(/hello world/i)).toBeInTheDocument();
110-
expect(computedValue.getByText(/hello world/i)).toBeInTheDocument();
115+
expect(inputValue.getByText(/hello world of 45 years old/i)).toBeInTheDocument();
116+
expect(computedValue.getByText(/hello world of 45 years old/i)).toBeInTheDocument();
111117

112118
await view.rerender({
113119
inputs: {
114120
...aliasedInput('greeting', 'bye'),
115121
name: 'test',
122+
age: '0',
116123
},
117124
});
118125

119-
expect(inputValue.getByText(/bye test/i)).toBeInTheDocument();
120-
expect(computedValue.getByText(/bye test/i)).toBeInTheDocument();
126+
expect(inputValue.getByText(/bye test of 0 years old/i)).toBeInTheDocument();
127+
expect(computedValue.getByText(/bye test of 0 years old/i)).toBeInTheDocument();
121128
});

Diff for: apps/example-app/src/app/examples/22-signal-inputs.component.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { Component, computed, input, model, output } from '@angular/core';
1+
import { Component, computed, input, model, numberAttribute, output } from '@angular/core';
22
import { FormsModule } from '@angular/forms';
33

44
@Component({
55
selector: 'app-signal-input',
66
template: `
7-
<div data-testid="input-value">{{ greetings() }} {{ name() }}</div>
7+
<div data-testid="input-value">{{ greetings() }} {{ name() }} of {{ age() }} years old</div>
88
<div data-testid="computed-value">{{ greetingMessage() }}</div>
99
<button (click)="submitName()">Submit</button>
1010
<input type="text" [(ngModel)]="name" />
@@ -16,10 +16,11 @@ export class SignalInputComponent {
1616
greetings = input<string>('', {
1717
alias: 'greeting',
1818
});
19+
age = input.required<number, string>({ transform: numberAttribute });
1920
name = model.required<string>();
2021
submit = output<string>();
2122

22-
greetingMessage = computed(() => `${this.greetings()} ${this.name()}`);
23+
greetingMessage = computed(() => `${this.greetings()} ${this.name()} of ${this.age()} years old`);
2324

2425
submitName() {
2526
this.submit.emit(this.name());

Diff for: projects/testing-library/src/lib/models.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Type, DebugElement, EventEmitter, Signal } from '@angular/core';
1+
import { Type, DebugElement, EventEmitter, Signal, InputSignalWithTransform } from '@angular/core';
22
import { ComponentFixture, DeferBlockBehavior, DeferBlockState, TestBed } from '@angular/core/testing';
33
import { Routes } from '@angular/router';
44
import { BoundFunction, Queries, queries, Config as dtlConfig, PrettyDOMOptions } from '@testing-library/dom';
@@ -94,7 +94,11 @@ export type AliasedInputs = Record<string, AliasedInput<unknown>>;
9494

9595
export type ComponentInput<T> =
9696
| {
97-
[P in keyof T]?: T[P] extends Signal<infer U> ? U : T[P];
97+
[P in keyof T]?: T[P] extends InputSignalWithTransform<any, infer U>
98+
? U
99+
: T[P] extends Signal<infer U>
100+
? U
101+
: T[P];
98102
}
99103
| AliasedInputs;
100104

0 commit comments

Comments
 (0)