|
8 | 8 |
|
9 | 9 | import {Component, Directive, EventEmitter, Input, Output, Type} from '@angular/core';
|
10 | 10 | import {ComponentFixture, TestBed, async, fakeAsync, tick} from '@angular/core/testing';
|
11 |
| -import {AbstractControl, ControlValueAccessor, FormControl, FormGroup, FormsModule, NG_VALIDATORS, NG_VALUE_ACCESSOR, NgControl, NgForm, ReactiveFormsModule, Validators} from '@angular/forms'; |
| 11 | +import {AbstractControl, ControlValueAccessor, FormControl, FormGroup, FormsModule, NG_VALIDATORS, NG_VALUE_ACCESSOR, NgControl, NgForm, NgModel, ReactiveFormsModule, Validators} from '@angular/forms'; |
12 | 12 | import {By} from '@angular/platform-browser/src/dom/debug/by';
|
13 | 13 | import {dispatchEvent} from '@angular/platform-browser/testing/src/browser_util';
|
14 | 14 |
|
@@ -210,6 +210,31 @@ export function main() {
|
210 | 210 | expect(sfOption.nativeElement.selected).toBe(true);
|
211 | 211 | });
|
212 | 212 |
|
| 213 | + it('should support re-assigning the options array with compareWith', () => { |
| 214 | + const fixture = initTest(FormControlSelectWithCompareFn); |
| 215 | + fixture.detectChanges(); |
| 216 | + |
| 217 | + // Option IDs start out as 0 and 1, so setting the select value to "1: Object" |
| 218 | + // will select the second option (NY). |
| 219 | + const select = fixture.debugElement.query(By.css('select')); |
| 220 | + select.nativeElement.value = '1: Object'; |
| 221 | + dispatchEvent(select.nativeElement, 'change'); |
| 222 | + fixture.detectChanges(); |
| 223 | + |
| 224 | + expect(fixture.componentInstance.form.value).toEqual({city: {id: 2, name: 'NY'}}); |
| 225 | + |
| 226 | + fixture.componentInstance.cities = [{id: 1, name: 'SF'}, {id: 2, name: 'NY'}]; |
| 227 | + fixture.detectChanges(); |
| 228 | + |
| 229 | + // Now that the options array has been re-assigned, new option instances will |
| 230 | + // be created by ngFor. These instances will have different option IDs, subsequent |
| 231 | + // to the first: 2 and 3. For the second option to stay selected, the select |
| 232 | + // value will need to have the ID of the current second option: 3. |
| 233 | + const nyOption = fixture.debugElement.queryAll(By.css('option'))[1]; |
| 234 | + expect(select.nativeElement.value).toEqual('3: Object'); |
| 235 | + expect(nyOption.nativeElement.selected).toBe(true); |
| 236 | + }); |
| 237 | + |
213 | 238 | });
|
214 | 239 |
|
215 | 240 | describe('in template-driven forms', () => {
|
@@ -335,6 +360,37 @@ export function main() {
|
335 | 360 | expect(sfOption.nativeElement.selected).toBe(true);
|
336 | 361 | }));
|
337 | 362 |
|
| 363 | + it('should support re-assigning the options array with compareWith', fakeAsync(() => { |
| 364 | + const fixture = initTest(NgModelSelectWithCustomCompareFnForm); |
| 365 | + fixture.componentInstance.selectedCity = {id: 1, name: 'SF'}; |
| 366 | + fixture.componentInstance.cities = [{id: 1, name: 'SF'}, {id: 2, name: 'NY'}]; |
| 367 | + fixture.detectChanges(); |
| 368 | + tick(); |
| 369 | + |
| 370 | + // Option IDs start out as 0 and 1, so setting the select value to "1: Object" |
| 371 | + // will select the second option (NY). |
| 372 | + const select = fixture.debugElement.query(By.css('select')); |
| 373 | + select.nativeElement.value = '1: Object'; |
| 374 | + dispatchEvent(select.nativeElement, 'change'); |
| 375 | + fixture.detectChanges(); |
| 376 | + |
| 377 | + const model = fixture.debugElement.children[0].injector.get(NgModel); |
| 378 | + expect(model.value).toEqual({id: 2, name: 'NY'}); |
| 379 | + |
| 380 | + fixture.componentInstance.cities = [{id: 1, name: 'SF'}, {id: 2, name: 'NY'}]; |
| 381 | + fixture.detectChanges(); |
| 382 | + tick(); |
| 383 | + |
| 384 | + // Now that the options array has been re-assigned, new option instances will |
| 385 | + // be created by ngFor. These instances will have different option IDs, subsequent |
| 386 | + // to the first: 2 and 3. For the second option to stay selected, the select |
| 387 | + // value will need to have the ID of the current second option: 3. |
| 388 | + const nyOption = fixture.debugElement.queryAll(By.css('option'))[1]; |
| 389 | + expect(select.nativeElement.value).toEqual('3: Object'); |
| 390 | + expect(nyOption.nativeElement.selected).toBe(true); |
| 391 | + })); |
| 392 | + |
| 393 | + |
338 | 394 | });
|
339 | 395 |
|
340 | 396 | });
|
|
0 commit comments