-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy path22-signal-inputs.component.spec.ts
More file actions
91 lines (74 loc) · 2.66 KB
/
Copy path22-signal-inputs.component.spec.ts
File metadata and controls
91 lines (74 loc) · 2.66 KB
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
import { render, screen } 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, {
componentInputs: {
greeting: 'Hello',
name: 'world',
},
});
expect(screen.getByText(/hello world/i)).toBeInTheDocument();
});
test('can update signal inputs', async () => {
const { fixture } = await render(SignalInputComponent, {
componentInputs: {
greeting: 'Hello',
name: 'world',
},
});
expect(screen.getByText(/hello world/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 screen.findByText(/hello updated/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, {
componentInputs: {
greeting: 'Hello',
name: 'world',
},
componentOutputs: {
submit: { emit: submitFn } as any,
},
});
await userEvent.click(screen.getByRole('button'));
expect(submitFn).toHaveBeenCalledWith('world');
});
test('model update also updates the template', async () => {
const { fixture } = await render(SignalInputComponent, {
componentInputs: {
greeting: 'Hello',
name: 'initial',
},
});
expect(screen.getByText(/hello initial/i)).toBeInTheDocument();
await userEvent.clear(screen.getByRole('textbox'));
await userEvent.type(screen.getByRole('textbox'), 'updated');
expect(screen.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 screen.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 and rerenders', async () => {
const view = await render(SignalInputComponent, {
componentInputs: {
greeting: 'Hello',
name: 'world',
},
});
expect(screen.getByText(/hello world/i)).toBeInTheDocument();
await view.rerender({
componentInputs: {
greeting: 'bye',
name: 'test',
},
});
expect(screen.getByText(/bye test/i)).toBeInTheDocument();
});