|
1 | 1 | import { render, screen } from '@testing-library/angular';
|
2 | 2 | import { SignalInputComponent } from './22-signal-inputs.component';
|
| 3 | +import userEvent from '@testing-library/user-event'; |
3 | 4 |
|
4 | 5 | test('works with signal inputs', async () => {
|
5 | 6 | await render(SignalInputComponent, {
|
6 | 7 | componentInputs: {
|
| 8 | + greeting: 'Hello', |
7 | 9 | name: 'world',
|
| 10 | + }, |
| 11 | + }); |
| 12 | + |
| 13 | + expect(screen.getByText(/hello world/i)).toBeInTheDocument(); |
| 14 | +}); |
| 15 | + |
| 16 | +test('can update signal inputs', async () => { |
| 17 | + const { fixture } = await render(SignalInputComponent, { |
| 18 | + componentInputs: { |
8 | 19 | greeting: 'Hello',
|
| 20 | + name: 'world', |
9 | 21 | },
|
10 | 22 | });
|
11 | 23 |
|
12 | 24 | expect(screen.getByText(/hello world/i)).toBeInTheDocument();
|
| 25 | + |
| 26 | + fixture.componentInstance.name.set('updated'); |
| 27 | + // 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(); |
| 29 | + // it's not recommended to access the model directly, but it's possible |
| 30 | + expect(fixture.componentInstance.name()).toBe('updated'); |
| 31 | +}); |
| 32 | + |
| 33 | +test('output emits a value', async () => { |
| 34 | + const submitFn = jest.fn(); |
| 35 | + await render(SignalInputComponent, { |
| 36 | + componentInputs: { |
| 37 | + greeting: 'Hello', |
| 38 | + name: 'world', |
| 39 | + }, |
| 40 | + componentOutputs: { |
| 41 | + submit: { emit: submitFn } as any, |
| 42 | + }, |
| 43 | + }); |
| 44 | + |
| 45 | + await userEvent.click(screen.getByRole('button')); |
| 46 | + |
| 47 | + expect(submitFn).toHaveBeenCalledWith('world'); |
| 48 | +}); |
| 49 | + |
| 50 | +test('model update also updates the template', async () => { |
| 51 | + const { fixture } = await render(SignalInputComponent, { |
| 52 | + componentInputs: { |
| 53 | + greeting: 'Hello', |
| 54 | + name: 'initial', |
| 55 | + }, |
| 56 | + }); |
| 57 | + |
| 58 | + expect(screen.getByText(/hello initial/i)).toBeInTheDocument(); |
| 59 | + |
| 60 | + await userEvent.clear(screen.getByRole('textbox')); |
| 61 | + await userEvent.type(screen.getByRole('textbox'), 'updated'); |
| 62 | + |
| 63 | + expect(screen.getByText(/hello updated/i)).toBeInTheDocument(); |
| 64 | + expect(fixture.componentInstance.name()).toBe('updated'); |
| 65 | + |
| 66 | + fixture.componentInstance.name.set('new value'); |
| 67 | + // 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(); |
| 69 | + // it's not recommended to access the model directly, but it's possible |
| 70 | + expect(fixture.componentInstance.name()).toBe('new value'); |
13 | 71 | });
|
14 | 72 |
|
15 | 73 | test('works with signal inputs and rerenders', async () => {
|
|
0 commit comments