|
| 1 | +import { ReactiveFormsModule, FormsModule, FormControl } from '@angular/forms'; |
| 2 | +import { render, RenderResult } from '../../src/public_api'; |
| 3 | +import { Component, ViewChild, Input } from '@angular/core'; |
| 4 | +import { fakeAsync, tick } from '@angular/core/testing'; |
| 5 | + |
| 6 | +describe('updates the value', () => { |
| 7 | + test('with a template-driven form', async () => { |
| 8 | + @Component({ |
| 9 | + selector: 'fixture', |
| 10 | + template: ` |
| 11 | + <input type="text" [(ngModel)]="value" data-testid="input" /> |
| 12 | + <p data-testid="text">{{ value }}</p> |
| 13 | + `, |
| 14 | + }) |
| 15 | + class FixtureComponent { |
| 16 | + value: string; |
| 17 | + } |
| 18 | + |
| 19 | + const component = await render(FixtureComponent, { |
| 20 | + imports: [FormsModule], |
| 21 | + }); |
| 22 | + |
| 23 | + assertType(component, () => component.fixture.componentInstance.value); |
| 24 | + }); |
| 25 | + |
| 26 | + test('with a reactive form', async () => { |
| 27 | + @Component({ |
| 28 | + selector: 'fixture', |
| 29 | + template: ` |
| 30 | + <input type="text" [formControl]="value" data-testid="input" /> |
| 31 | + <p data-testid="text">{{ value.value }}</p> |
| 32 | + `, |
| 33 | + }) |
| 34 | + class FixtureComponent { |
| 35 | + value = new FormControl(''); |
| 36 | + } |
| 37 | + |
| 38 | + const component = await render(FixtureComponent, { |
| 39 | + imports: [ReactiveFormsModule], |
| 40 | + }); |
| 41 | + |
| 42 | + assertType(component, () => component.fixture.componentInstance.value.value); |
| 43 | + }); |
| 44 | + |
| 45 | + test('with events', async () => { |
| 46 | + @Component({ |
| 47 | + selector: 'fixture', |
| 48 | + template: ` |
| 49 | + <input type="text" (input)="onInput($event)" data-testid="input" /> |
| 50 | + <p data-testid="text">{{ value }}</p> |
| 51 | + `, |
| 52 | + }) |
| 53 | + class FixtureComponent { |
| 54 | + value = ''; |
| 55 | + |
| 56 | + onInput(event: KeyboardEvent) { |
| 57 | + this.value = (<HTMLInputElement>event.target).value; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + const component = await render(FixtureComponent); |
| 62 | + |
| 63 | + assertType(component, () => component.fixture.componentInstance.value); |
| 64 | + }); |
| 65 | + |
| 66 | + test('by reference', async () => { |
| 67 | + @Component({ |
| 68 | + selector: 'fixture', |
| 69 | + template: ` |
| 70 | + <input type="text" data-testid="input" #input /> |
| 71 | + <p data-testid="text">{{ input.value }}</p> |
| 72 | + `, |
| 73 | + }) |
| 74 | + class FixtureComponent { |
| 75 | + @ViewChild('input', { static: false }) value; |
| 76 | + } |
| 77 | + |
| 78 | + const component = await render(FixtureComponent); |
| 79 | + |
| 80 | + assertType(component, () => component.fixture.componentInstance.value.nativeElement.value); |
| 81 | + }); |
| 82 | + |
| 83 | + function assertType(component: RenderResult, value: () => string) { |
| 84 | + const input = '@testing-library/angular'; |
| 85 | + const inputControl = component.getByTestId('input') as HTMLInputElement; |
| 86 | + component.type(inputControl, input); |
| 87 | + |
| 88 | + expect(value()).toBe(input); |
| 89 | + expect(component.getByTestId('text').textContent).toBe(input); |
| 90 | + expect(inputControl.value).toBe(input); |
| 91 | + expect(inputControl).toHaveProperty('value', input); |
| 92 | + } |
| 93 | +}); |
| 94 | + |
| 95 | +describe('options', () => { |
| 96 | + @Component({ |
| 97 | + selector: 'fixture', |
| 98 | + template: ` |
| 99 | + <input |
| 100 | + type="text" |
| 101 | + data-testid="input" |
| 102 | + (input)="onInput($event)" |
| 103 | + (change)="onChange($event)" |
| 104 | + (keydown)="onKeyDown($event)" |
| 105 | + (keypress)="onKeyPress($event)" |
| 106 | + (keyup)="onKeyUp($event)" |
| 107 | + /> |
| 108 | + `, |
| 109 | + }) |
| 110 | + class FixtureComponent { |
| 111 | + onInput($event) {} |
| 112 | + onChange($event) {} |
| 113 | + onKeyDown($event) {} |
| 114 | + onKeyPress($event) {} |
| 115 | + onKeyUp($event) {} |
| 116 | + } |
| 117 | + |
| 118 | + async function setup() { |
| 119 | + const componentProperties = { |
| 120 | + onInput: jest.fn(), |
| 121 | + onChange: jest.fn(), |
| 122 | + onKeyDown: jest.fn(), |
| 123 | + onKeyPress: jest.fn(), |
| 124 | + onKeyUp: jest.fn(), |
| 125 | + }; |
| 126 | + const component = await render(FixtureComponent, { componentProperties }); |
| 127 | + |
| 128 | + return { component, ...componentProperties }; |
| 129 | + } |
| 130 | + |
| 131 | + describe('allAtOnce', () => { |
| 132 | + test('false: updates the value one char at a time', async () => { |
| 133 | + const { component, onInput, onChange, onKeyDown, onKeyPress, onKeyUp } = await setup(); |
| 134 | + |
| 135 | + const inputControl = component.getByTestId('input') as HTMLInputElement; |
| 136 | + const inputValue = 'foobar'; |
| 137 | + component.type(inputControl, inputValue); |
| 138 | + |
| 139 | + expect(onInput).toBeCalledTimes(inputValue.length); |
| 140 | + expect(onKeyDown).toBeCalledTimes(inputValue.length); |
| 141 | + expect(onKeyPress).toBeCalledTimes(inputValue.length); |
| 142 | + expect(onKeyUp).toBeCalledTimes(inputValue.length); |
| 143 | + |
| 144 | + component.blur(inputControl); |
| 145 | + expect(onChange).toBeCalledTimes(1); |
| 146 | + }); |
| 147 | + |
| 148 | + test('true: updates the value in one time and does not trigger other events', async () => { |
| 149 | + const { component, onInput, onChange, onKeyDown, onKeyPress, onKeyUp } = await setup(); |
| 150 | + |
| 151 | + const inputControl = component.getByTestId('input') as HTMLInputElement; |
| 152 | + const inputValue = 'foobar'; |
| 153 | + component.type(inputControl, inputValue, { allAtOnce: true }); |
| 154 | + |
| 155 | + expect(onInput).toBeCalledTimes(1); |
| 156 | + expect(onKeyDown).toBeCalledTimes(0); |
| 157 | + expect(onKeyPress).toBeCalledTimes(0); |
| 158 | + expect(onKeyUp).toBeCalledTimes(0); |
| 159 | + |
| 160 | + component.blur(inputControl); |
| 161 | + expect(onChange).toBeCalledTimes(1); |
| 162 | + }); |
| 163 | + }); |
| 164 | + |
| 165 | + describe('delay', () => { |
| 166 | + test('delays the input', fakeAsync(async () => { |
| 167 | + const { component } = await setup(); |
| 168 | + |
| 169 | + const inputControl = component.getByTestId('input') as HTMLInputElement; |
| 170 | + const inputValue = 'foobar'; |
| 171 | + component.type(inputControl, inputValue, { delay: 25 }); |
| 172 | + |
| 173 | + [...inputValue].forEach((_, i) => { |
| 174 | + expect(inputControl.value).toBe(inputValue.substr(0, i)); |
| 175 | + tick(25); |
| 176 | + }); |
| 177 | + })); |
| 178 | + }); |
| 179 | +}); |
| 180 | + |
| 181 | +test('should not type when event.preventDefault() is called', async () => { |
| 182 | + @Component({ |
| 183 | + selector: 'fixture', |
| 184 | + template: ` |
| 185 | + <input |
| 186 | + type="text" |
| 187 | + data-testid="input" |
| 188 | + (input)="onInput($event)" |
| 189 | + (change)="onChange($event)" |
| 190 | + (keydown)="onKeyDown($event)" |
| 191 | + (keypress)="onKeyPress($event)" |
| 192 | + (keyup)="onKeyUp($event)" |
| 193 | + /> |
| 194 | + `, |
| 195 | + }) |
| 196 | + class FixtureComponent { |
| 197 | + onInput($event) {} |
| 198 | + onChange($event) {} |
| 199 | + onKeyDown($event) {} |
| 200 | + onKeyPress($event) {} |
| 201 | + onKeyUp($event) {} |
| 202 | + } |
| 203 | + |
| 204 | + const componentProperties = { |
| 205 | + onChange: jest.fn(), |
| 206 | + onKeyDown: jest.fn().mockImplementation(event => event.preventDefault()), |
| 207 | + }; |
| 208 | + |
| 209 | + const component = await render(FixtureComponent, { componentProperties }); |
| 210 | + |
| 211 | + const inputControl = component.getByTestId('input') as HTMLInputElement; |
| 212 | + const inputValue = 'foobar'; |
| 213 | + component.type(inputControl, inputValue); |
| 214 | + |
| 215 | + expect(componentProperties.onKeyDown).toHaveBeenCalledTimes(inputValue.length); |
| 216 | + |
| 217 | + component.blur(inputControl); |
| 218 | + expect(componentProperties.onChange).toBeCalledTimes(0); |
| 219 | + |
| 220 | + expect(inputControl.value).toBe(''); |
| 221 | +}); |
0 commit comments