|
1 | 1 | import { IgxInputState } from './../directives/input/input.directive'; |
2 | 2 | import { Component, ViewChild, DebugElement } from '@angular/core'; |
3 | 3 | import { async, TestBed, tick, fakeAsync } from '@angular/core/testing'; |
4 | | -import { FormsModule, FormGroup, FormBuilder, FormControl, Validators, ReactiveFormsModule, NgForm } from '@angular/forms'; |
| 4 | +import { FormsModule, FormGroup, FormBuilder, FormControl, Validators, ReactiveFormsModule, NgForm, NgControl } from '@angular/forms'; |
5 | 5 | import { By } from '@angular/platform-browser'; |
6 | | -import { IgxDropDownModule } from '../drop-down/index'; |
| 6 | +import { IgxDropDownModule, IgxDropDownItemComponent } from '../drop-down/index'; |
7 | 7 | import { IgxIconModule } from '../icon/index'; |
8 | 8 | import { IgxInputGroupModule } from '../input-group/index'; |
9 | 9 | import { NoopAnimationsModule } from '@angular/platform-browser/animations'; |
@@ -604,6 +604,24 @@ describe('igxSelect', () => { |
604 | 604 | expect(inputGroupWithRequiredAsterisk).toBeDefined(); |
605 | 605 | })); |
606 | 606 |
|
| 607 | + it('Should have correctly bound focus and blur handlers', () => { |
| 608 | + const fix = TestBed.createComponent(IgxSelectTemplateFormComponent); |
| 609 | + fix.detectChanges(); |
| 610 | + select = fix.componentInstance.select; |
| 611 | + const input = fix.debugElement.query(By.css(`.${CSS_CLASS_INPUT}`)); |
| 612 | + |
| 613 | + spyOn(select, 'onFocus'); |
| 614 | + spyOn(select, 'onBlur'); |
| 615 | + |
| 616 | + input.triggerEventHandler('focus', {}); |
| 617 | + expect(select.onFocus).toHaveBeenCalled(); |
| 618 | + expect(select.onFocus).toHaveBeenCalledWith(); |
| 619 | + |
| 620 | + input.triggerEventHandler('blur', {}); |
| 621 | + expect(select.onBlur).toHaveBeenCalled(); |
| 622 | + expect(select.onFocus).toHaveBeenCalledWith(); |
| 623 | + }); |
| 624 | + |
607 | 625 | // Bug #6025 Select does not disable in reactive form |
608 | 626 | it('Should disable when form is disabled', fakeAsync(() => { |
609 | 627 | const fix = TestBed.createComponent(IgxSelectReactiveFormComponent); |
@@ -2394,6 +2412,57 @@ describe('igxSelect', () => { |
2394 | 2412 | }); |
2395 | 2413 | }); |
2396 | 2414 |
|
| 2415 | +describe('igxSelect ControlValueAccessor Unit', () => { |
| 2416 | + let select: IgxSelectComponent; |
| 2417 | + it('Should correctly implement interface methods', () => { |
| 2418 | + const mockSelection = jasmine.createSpyObj('IgxSelectionAPIService', ['get', 'set', 'clear', 'first_item']); |
| 2419 | + const mockCdr = jasmine.createSpyObj('ChangeDetectorRef', ['detectChanges']); |
| 2420 | + const mockNgControl = jasmine.createSpyObj('NgControl', ['registerOnChangeCb', 'registerOnTouchedCb']); |
| 2421 | + const mockInjector = jasmine.createSpyObj('Injector', { |
| 2422 | + 'get': mockNgControl |
| 2423 | + }); |
| 2424 | + |
| 2425 | + // init |
| 2426 | + select = new IgxSelectComponent(null, mockCdr, mockSelection, null, mockInjector); |
| 2427 | + select.ngOnInit(); |
| 2428 | + select.registerOnChange(mockNgControl.registerOnChangeCb); |
| 2429 | + select.registerOnTouched(mockNgControl.registerOnTouchedCb); |
| 2430 | + expect(mockInjector.get).toHaveBeenCalledWith(NgControl, null); |
| 2431 | + |
| 2432 | + // writeValue |
| 2433 | + expect(select.value).toBeUndefined(); |
| 2434 | + select.writeValue('test'); |
| 2435 | + expect(mockSelection.clear).toHaveBeenCalled(); |
| 2436 | + expect(select.value).toBe('test'); |
| 2437 | + |
| 2438 | + // setDisabledState |
| 2439 | + select.setDisabledState(true); |
| 2440 | + expect(select.disabled).toBe(true); |
| 2441 | + select.setDisabledState(false); |
| 2442 | + expect(select.disabled).toBe(false); |
| 2443 | + |
| 2444 | + // OnChange callback |
| 2445 | + const item = new IgxDropDownItemComponent(select, null, null, mockSelection); |
| 2446 | + item.value = 'itemValue'; |
| 2447 | + select.selectItem(item); |
| 2448 | + expect(mockSelection.set).toHaveBeenCalledWith(select.id, new Set([item])); |
| 2449 | + expect(mockNgControl.registerOnChangeCb).toHaveBeenCalledWith('itemValue'); |
| 2450 | + |
| 2451 | + // OnTouched callback |
| 2452 | + select.onFocus(); |
| 2453 | + expect(mockNgControl.registerOnTouchedCb).toHaveBeenCalledTimes(1); |
| 2454 | + |
| 2455 | + select.input = {} as any; |
| 2456 | + spyOnProperty(select, 'collapsed').and.returnValue(true); |
| 2457 | + select.onBlur(); |
| 2458 | + expect(mockNgControl.registerOnTouchedCb).toHaveBeenCalledTimes(2); |
| 2459 | + }); |
| 2460 | + |
| 2461 | + it('Should correctly handle ngControl validity', () => { |
| 2462 | + pending('Convert existing form test here'); |
| 2463 | + }); |
| 2464 | +}); |
| 2465 | + |
2397 | 2466 | @Component({ |
2398 | 2467 | template: ` |
2399 | 2468 | <igx-select #select [width]="'300px'" [height]="'200px'" [placeholder]="'Choose a city'" [(ngModel)]="value"> |
|
0 commit comments