Skip to content

Commit 86c08e0

Browse files
docs: add computed example (#455)
1 parent 47f680f commit 86c08e0

File tree

2 files changed

+45
-12
lines changed

2 files changed

+45
-12
lines changed

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

+40-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { render, screen } from '@testing-library/angular';
1+
import { render, screen, within } from '@testing-library/angular';
22
import { SignalInputComponent } from './22-signal-inputs.component';
33
import userEvent from '@testing-library/user-event';
44

@@ -10,7 +10,20 @@ test('works with signal inputs', async () => {
1010
},
1111
});
1212

13-
expect(screen.getByText(/hello world/i)).toBeInTheDocument();
13+
const inputValue = within(screen.getByTestId('input-value'));
14+
expect(inputValue.getByText(/hello world/i)).toBeInTheDocument();
15+
});
16+
17+
test('works with computed', async () => {
18+
await render(SignalInputComponent, {
19+
componentInputs: {
20+
greeting: 'Hello',
21+
name: 'world',
22+
},
23+
});
24+
25+
const computedValue = within(screen.getByTestId('computed-value'));
26+
expect(computedValue.getByText(/hello world/i)).toBeInTheDocument();
1427
});
1528

1629
test('can update signal inputs', async () => {
@@ -21,11 +34,16 @@ test('can update signal inputs', async () => {
2134
},
2235
});
2336

24-
expect(screen.getByText(/hello world/i)).toBeInTheDocument();
37+
const inputValue = within(screen.getByTestId('input-value'));
38+
const computedValue = within(screen.getByTestId('computed-value'));
39+
40+
expect(inputValue.getByText(/hello world/i)).toBeInTheDocument();
2541

2642
fixture.componentInstance.name.set('updated');
2743
// set doesn't trigger change detection within the test, findBy is needed to update the template
28-
expect(await screen.findByText(/hello updated/i)).toBeInTheDocument();
44+
expect(await inputValue.findByText(/hello updated/i)).toBeInTheDocument();
45+
expect(await computedValue.findByText(/hello updated/i)).toBeInTheDocument();
46+
2947
// it's not recommended to access the model directly, but it's possible
3048
expect(fixture.componentInstance.name()).toBe('updated');
3149
});
@@ -55,30 +73,41 @@ test('model update also updates the template', async () => {
5573
},
5674
});
5775

58-
expect(screen.getByText(/hello initial/i)).toBeInTheDocument();
76+
const inputValue = within(screen.getByTestId('input-value'));
77+
const computedValue = within(screen.getByTestId('computed-value'));
78+
79+
expect(inputValue.getByText(/hello initial/i)).toBeInTheDocument();
80+
expect(computedValue.getByText(/hello initial/i)).toBeInTheDocument();
5981

6082
await userEvent.clear(screen.getByRole('textbox'));
6183
await userEvent.type(screen.getByRole('textbox'), 'updated');
6284

63-
expect(screen.getByText(/hello updated/i)).toBeInTheDocument();
85+
expect(inputValue.getByText(/hello updated/i)).toBeInTheDocument();
86+
expect(computedValue.getByText(/hello updated/i)).toBeInTheDocument();
6487
expect(fixture.componentInstance.name()).toBe('updated');
6588

6689
fixture.componentInstance.name.set('new value');
6790
// set doesn't trigger change detection within the test, findBy is needed to update the template
68-
expect(await screen.findByText(/hello new value/i)).toBeInTheDocument();
91+
expect(await inputValue.findByText(/hello new value/i)).toBeInTheDocument();
92+
expect(await computedValue.findByText(/hello new value/i)).toBeInTheDocument();
93+
6994
// it's not recommended to access the model directly, but it's possible
7095
expect(fixture.componentInstance.name()).toBe('new value');
7196
});
7297

73-
test('works with signal inputs and rerenders', async () => {
98+
test('works with signal inputs, computed values, and rerenders', async () => {
7499
const view = await render(SignalInputComponent, {
75100
componentInputs: {
76101
greeting: 'Hello',
77102
name: 'world',
78103
},
79104
});
80105

81-
expect(screen.getByText(/hello world/i)).toBeInTheDocument();
106+
const inputValue = within(screen.getByTestId('input-value'));
107+
const computedValue = within(screen.getByTestId('computed-value'));
108+
109+
expect(inputValue.getByText(/hello world/i)).toBeInTheDocument();
110+
expect(computedValue.getByText(/hello world/i)).toBeInTheDocument();
82111

83112
await view.rerender({
84113
componentInputs: {
@@ -87,5 +116,6 @@ test('works with signal inputs and rerenders', async () => {
87116
},
88117
});
89118

90-
expect(screen.getByText(/bye test/i)).toBeInTheDocument();
119+
expect(inputValue.getByText(/bye test/i)).toBeInTheDocument();
120+
expect(computedValue.getByText(/bye test/i)).toBeInTheDocument();
91121
});

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

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

44
@Component({
55
selector: 'app-signal-input',
66
template: `
7-
<div>{{ greetings() }} {{ name() }}</div>
7+
<div data-testid="input-value">{{ greetings() }} {{ name() }}</div>
8+
<div data-testid="computed-value">{{ greetingMessage() }}</div>
89
<button (click)="submitName()">Submit</button>
910
<input type="text" [(ngModel)]="name" />
1011
`,
@@ -18,6 +19,8 @@ export class SignalInputComponent {
1819
name = model.required<string>();
1920
submit = output<string>();
2021

22+
greetingMessage = computed(() => `${this.greetings()} ${this.name()}`);
23+
2124
submitName() {
2225
this.submit.emit(this.name());
2326
}

0 commit comments

Comments
 (0)