|
| 1 | +import { signal, inputBinding, outputBinding, twoWayBinding } from '@angular/core'; |
| 2 | +import { render, screen } from '@testing-library/angular'; |
| 3 | +import { BindingsApiExampleComponent } from './24-bindings-api.component'; |
| 4 | + |
| 5 | +test('displays computed greeting message with input values', async () => { |
| 6 | + await render(BindingsApiExampleComponent, { |
| 7 | + bindings: [ |
| 8 | + inputBinding('greeting', () => 'Hello'), |
| 9 | + inputBinding('age', () => 25), |
| 10 | + twoWayBinding('name', signal('John')), |
| 11 | + ], |
| 12 | + }); |
| 13 | + |
| 14 | + expect(screen.getByTestId('input-value')).toHaveTextContent('Hello John of 25 years old'); |
| 15 | + expect(screen.getByTestId('computed-value')).toHaveTextContent('Hello John of 25 years old'); |
| 16 | + expect(screen.getByTestId('current-age')).toHaveTextContent('Current age: 25'); |
| 17 | +}); |
| 18 | + |
| 19 | +test('emits submitValue output when submit button is clicked', async () => { |
| 20 | + const submitHandler = jest.fn(); |
| 21 | + const nameSignal = signal('Alice'); |
| 22 | + |
| 23 | + await render(BindingsApiExampleComponent, { |
| 24 | + bindings: [ |
| 25 | + inputBinding('greeting', () => 'Good morning'), |
| 26 | + inputBinding('age', () => 28), |
| 27 | + twoWayBinding('name', nameSignal), |
| 28 | + outputBinding('submitValue', submitHandler), |
| 29 | + ], |
| 30 | + }); |
| 31 | + |
| 32 | + const submitButton = screen.getByTestId('submit-button'); |
| 33 | + submitButton.click(); |
| 34 | + expect(submitHandler).toHaveBeenCalledWith('Alice'); |
| 35 | +}); |
| 36 | + |
| 37 | +test('emits ageChanged output when increment button is clicked', async () => { |
| 38 | + const ageChangedHandler = jest.fn(); |
| 39 | + |
| 40 | + await render(BindingsApiExampleComponent, { |
| 41 | + bindings: [ |
| 42 | + inputBinding('greeting', () => 'Hi'), |
| 43 | + inputBinding('age', () => 20), |
| 44 | + twoWayBinding('name', signal('Charlie')), |
| 45 | + outputBinding('ageChanged', ageChangedHandler), |
| 46 | + ], |
| 47 | + }); |
| 48 | + |
| 49 | + const incrementButton = screen.getByTestId('increment-button'); |
| 50 | + incrementButton.click(); |
| 51 | + |
| 52 | + expect(ageChangedHandler).toHaveBeenCalledWith(21); |
| 53 | +}); |
| 54 | + |
| 55 | +test('updates name through two-way binding when input changes', async () => { |
| 56 | + const nameSignal = signal('Initial Name'); |
| 57 | + |
| 58 | + await render(BindingsApiExampleComponent, { |
| 59 | + bindings: [ |
| 60 | + inputBinding('greeting', () => 'Hello'), |
| 61 | + inputBinding('age', () => 25), |
| 62 | + twoWayBinding('name', nameSignal), |
| 63 | + ], |
| 64 | + }); |
| 65 | + |
| 66 | + const nameInput = screen.getByTestId('name-input') as HTMLInputElement; |
| 67 | + |
| 68 | + // Verify initial value |
| 69 | + expect(nameInput.value).toBe('Initial Name'); |
| 70 | + expect(screen.getByTestId('input-value')).toHaveTextContent('Hello Initial Name of 25 years old'); |
| 71 | + |
| 72 | + // Update the signal externally |
| 73 | + nameSignal.set('Updated Name'); |
| 74 | + |
| 75 | + // Verify the input and display update |
| 76 | + expect(await screen.findByDisplayValue('Updated Name')).toBeInTheDocument(); |
| 77 | + expect(screen.getByTestId('input-value')).toHaveTextContent('Hello Updated Name of 25 years old'); |
| 78 | + expect(screen.getByTestId('computed-value')).toHaveTextContent('Hello Updated Name of 25 years old'); |
| 79 | +}); |
| 80 | + |
| 81 | +test('updates computed value when inputs change', async () => { |
| 82 | + const greetingSignal = signal('Good day'); |
| 83 | + const nameSignal = signal('David'); |
| 84 | + const ageSignal = signal(35); |
| 85 | + |
| 86 | + const { fixture } = await render(BindingsApiExampleComponent, { |
| 87 | + bindings: [ |
| 88 | + inputBinding('greeting', greetingSignal), |
| 89 | + inputBinding('age', ageSignal), |
| 90 | + twoWayBinding('name', nameSignal), |
| 91 | + ], |
| 92 | + }); |
| 93 | + |
| 94 | + // Initial state |
| 95 | + expect(screen.getByTestId('computed-value')).toHaveTextContent('Good day David of 35 years old'); |
| 96 | + |
| 97 | + // Update greeting |
| 98 | + greetingSignal.set('Good evening'); |
| 99 | + fixture.detectChanges(); |
| 100 | + expect(screen.getByTestId('computed-value')).toHaveTextContent('Good evening David of 35 years old'); |
| 101 | + |
| 102 | + // Update age |
| 103 | + ageSignal.set(36); |
| 104 | + fixture.detectChanges(); |
| 105 | + expect(screen.getByTestId('computed-value')).toHaveTextContent('Good evening David of 36 years old'); |
| 106 | + |
| 107 | + // Update name |
| 108 | + nameSignal.set('Daniel'); |
| 109 | + fixture.detectChanges(); |
| 110 | + expect(screen.getByTestId('computed-value')).toHaveTextContent('Good evening Daniel of 36 years old'); |
| 111 | +}); |
| 112 | + |
| 113 | +test('handles multiple output emissions correctly', async () => { |
| 114 | + const submitHandler = jest.fn(); |
| 115 | + const ageChangedHandler = jest.fn(); |
| 116 | + const nameSignal = signal('Emma'); |
| 117 | + |
| 118 | + await render(BindingsApiExampleComponent, { |
| 119 | + bindings: [ |
| 120 | + inputBinding('greeting', () => 'Hey'), |
| 121 | + inputBinding('age', () => 22), |
| 122 | + twoWayBinding('name', nameSignal), |
| 123 | + outputBinding('submitValue', submitHandler), |
| 124 | + outputBinding('ageChanged', ageChangedHandler), |
| 125 | + ], |
| 126 | + }); |
| 127 | + |
| 128 | + // Click submit button multiple times |
| 129 | + const submitButton = screen.getByTestId('submit-button'); |
| 130 | + submitButton.click(); |
| 131 | + submitButton.click(); |
| 132 | + |
| 133 | + expect(submitHandler).toHaveBeenCalledTimes(2); |
| 134 | + expect(submitHandler).toHaveBeenNthCalledWith(1, 'Emma'); |
| 135 | + expect(submitHandler).toHaveBeenNthCalledWith(2, 'Emma'); |
| 136 | + |
| 137 | + // Click increment button multiple times |
| 138 | + const incrementButton = screen.getByTestId('increment-button'); |
| 139 | + incrementButton.click(); |
| 140 | + incrementButton.click(); |
| 141 | + incrementButton.click(); |
| 142 | + |
| 143 | + expect(ageChangedHandler).toHaveBeenCalledTimes(3); |
| 144 | + expect(ageChangedHandler).toHaveBeenNthCalledWith(1, 23); |
| 145 | + expect(ageChangedHandler).toHaveBeenNthCalledWith(2, 23); // Still 23 because age input doesn't change |
| 146 | + expect(ageChangedHandler).toHaveBeenNthCalledWith(3, 23); |
| 147 | +}); |
0 commit comments