-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy path22-signal-inputs.component.spec.ts
128 lines (104 loc) · 4.23 KB
/
22-signal-inputs.component.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { aliasedInput, render, screen, within } from '@testing-library/angular';
import { SignalInputComponent } from './22-signal-inputs.component';
import userEvent from '@testing-library/user-event';
test('works with signal inputs', async () => {
await render(SignalInputComponent, {
inputs: {
...aliasedInput('greeting', 'Hello'),
name: 'world',
age: '45',
},
});
const inputValue = within(screen.getByTestId('input-value'));
expect(inputValue.getByText(/hello world of 45 years old/i)).toBeInTheDocument();
});
test('works with computed', async () => {
await render(SignalInputComponent, {
inputs: {
...aliasedInput('greeting', 'Hello'),
name: 'world',
age: '45',
},
});
const computedValue = within(screen.getByTestId('computed-value'));
expect(computedValue.getByText(/hello world of 45 years old/i)).toBeInTheDocument();
});
test('can update signal inputs', async () => {
const { fixture } = await render(SignalInputComponent, {
inputs: {
...aliasedInput('greeting', 'Hello'),
name: 'world',
age: '45',
},
});
const inputValue = within(screen.getByTestId('input-value'));
const computedValue = within(screen.getByTestId('computed-value'));
expect(inputValue.getByText(/hello world of 45 years old/i)).toBeInTheDocument();
fixture.componentInstance.name.set('updated');
// set doesn't trigger change detection within the test, findBy is needed to update the template
expect(await inputValue.findByText(/hello updated of 45 years old/i)).toBeInTheDocument();
expect(await computedValue.findByText(/hello updated of 45 years old/i)).toBeInTheDocument();
// it's not recommended to access the model directly, but it's possible
expect(fixture.componentInstance.name()).toBe('updated');
});
test('output emits a value', async () => {
const submitFn = jest.fn();
await render(SignalInputComponent, {
inputs: {
...aliasedInput('greeting', 'Hello'),
name: 'world',
age: '45',
},
on: {
submitValue: submitFn,
},
});
await userEvent.click(screen.getByRole('button'));
expect(submitFn).toHaveBeenCalledWith('world');
});
test('model update also updates the template', async () => {
const { fixture } = await render(SignalInputComponent, {
inputs: {
...aliasedInput('greeting', 'Hello'),
name: 'initial',
age: '45',
},
});
const inputValue = within(screen.getByTestId('input-value'));
const computedValue = within(screen.getByTestId('computed-value'));
expect(inputValue.getByText(/hello initial/i)).toBeInTheDocument();
expect(computedValue.getByText(/hello initial/i)).toBeInTheDocument();
await userEvent.clear(screen.getByRole('textbox'));
await userEvent.type(screen.getByRole('textbox'), 'updated');
expect(inputValue.getByText(/hello updated/i)).toBeInTheDocument();
expect(computedValue.getByText(/hello updated/i)).toBeInTheDocument();
expect(fixture.componentInstance.name()).toBe('updated');
fixture.componentInstance.name.set('new value');
// set doesn't trigger change detection within the test, findBy is needed to update the template
expect(await inputValue.findByText(/hello new value/i)).toBeInTheDocument();
expect(await computedValue.findByText(/hello new value/i)).toBeInTheDocument();
// it's not recommended to access the model directly, but it's possible
expect(fixture.componentInstance.name()).toBe('new value');
});
test('works with signal inputs, computed values, and rerenders', async () => {
const view = await render(SignalInputComponent, {
inputs: {
...aliasedInput('greeting', 'Hello'),
name: 'world',
age: '45',
},
});
const inputValue = within(screen.getByTestId('input-value'));
const computedValue = within(screen.getByTestId('computed-value'));
expect(inputValue.getByText(/hello world of 45 years old/i)).toBeInTheDocument();
expect(computedValue.getByText(/hello world of 45 years old/i)).toBeInTheDocument();
await view.rerender({
inputs: {
...aliasedInput('greeting', 'bye'),
name: 'test',
age: '0',
},
});
expect(inputValue.getByText(/bye test of 0 years old/i)).toBeInTheDocument();
expect(computedValue.getByText(/bye test of 0 years old/i)).toBeInTheDocument();
});