|
1 | 1 | import { IgxInputState } from './../directives/input/input.directive'; |
2 | 2 | import { Component, ViewChild, DebugElement, OnInit, ViewChildren, QueryList } 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'; |
@@ -612,6 +612,24 @@ describe('igxSelect', () => { |
612 | 612 | expect(inputGroupWithRequiredAsterisk).toBeDefined(); |
613 | 613 | })); |
614 | 614 |
|
| 615 | + it('Should have correctly bound focus and blur handlers', () => { |
| 616 | + const fix = TestBed.createComponent(IgxSelectTemplateFormComponent); |
| 617 | + fix.detectChanges(); |
| 618 | + select = fix.componentInstance.select; |
| 619 | + const input = fix.debugElement.query(By.css(`.${CSS_CLASS_INPUT}`)); |
| 620 | + |
| 621 | + spyOn(select, 'onFocus'); |
| 622 | + spyOn(select, 'onBlur'); |
| 623 | + |
| 624 | + input.triggerEventHandler('focus', {}); |
| 625 | + expect(select.onFocus).toHaveBeenCalled(); |
| 626 | + expect(select.onFocus).toHaveBeenCalledWith(); |
| 627 | + |
| 628 | + input.triggerEventHandler('blur', {}); |
| 629 | + expect(select.onBlur).toHaveBeenCalled(); |
| 630 | + expect(select.onFocus).toHaveBeenCalledWith(); |
| 631 | + }); |
| 632 | + |
615 | 633 | // Bug #6025 Select does not disable in reactive form |
616 | 634 | it('Should disable when form is disabled', fakeAsync(() => { |
617 | 635 | const fix = TestBed.createComponent(IgxSelectReactiveFormComponent); |
@@ -2493,6 +2511,58 @@ describe('igxSelect', () => { |
2493 | 2511 | expect(selectCDR.value).toBe('ID'); |
2494 | 2512 | }); |
2495 | 2513 | }); |
| 2514 | +}); |
| 2515 | + |
| 2516 | +describe('igxSelect ControlValueAccessor Unit', () => { |
| 2517 | + let select: IgxSelectComponent; |
| 2518 | + it('Should correctly implement interface methods', () => { |
| 2519 | + const mockSelection = jasmine.createSpyObj('IgxSelectionAPIService', ['get', 'set', 'clear', 'first_item']); |
| 2520 | + const mockCdr = jasmine.createSpyObj('ChangeDetectorRef', ['detectChanges']); |
| 2521 | + const mockNgControl = jasmine.createSpyObj('NgControl', ['registerOnChangeCb', 'registerOnTouchedCb']); |
| 2522 | + const mockInjector = jasmine.createSpyObj('Injector', { |
| 2523 | + 'get': mockNgControl |
| 2524 | + }); |
| 2525 | + |
| 2526 | + // init |
| 2527 | + select = new IgxSelectComponent(null, mockCdr, mockSelection, null, mockInjector); |
| 2528 | + select.ngOnInit(); |
| 2529 | + select.registerOnChange(mockNgControl.registerOnChangeCb); |
| 2530 | + select.registerOnTouched(mockNgControl.registerOnTouchedCb); |
| 2531 | + expect(mockInjector.get).toHaveBeenCalledWith(NgControl, null); |
| 2532 | + |
| 2533 | + // writeValue |
| 2534 | + expect(select.value).toBeUndefined(); |
| 2535 | + select.writeValue('test'); |
| 2536 | + expect(mockSelection.clear).toHaveBeenCalled(); |
| 2537 | + expect(select.value).toBe('test'); |
| 2538 | + |
| 2539 | + // setDisabledState |
| 2540 | + select.setDisabledState(true); |
| 2541 | + expect(select.disabled).toBe(true); |
| 2542 | + select.setDisabledState(false); |
| 2543 | + expect(select.disabled).toBe(false); |
| 2544 | + |
| 2545 | + // OnChange callback |
| 2546 | + const item = new IgxDropDownItemComponent(select, null, null, mockSelection); |
| 2547 | + item.value = 'itemValue'; |
| 2548 | + select.selectItem(item); |
| 2549 | + expect(mockSelection.set).toHaveBeenCalledWith(select.id, new Set([item])); |
| 2550 | + expect(mockNgControl.registerOnChangeCb).toHaveBeenCalledWith('itemValue'); |
| 2551 | + |
| 2552 | + // OnTouched callback |
| 2553 | + select.onFocus(); |
| 2554 | + expect(mockNgControl.registerOnTouchedCb).toHaveBeenCalledTimes(1); |
| 2555 | + |
| 2556 | + select.input = {} as any; |
| 2557 | + spyOnProperty(select, 'collapsed').and.returnValue(true); |
| 2558 | + select.onBlur(); |
| 2559 | + expect(mockNgControl.registerOnTouchedCb).toHaveBeenCalledTimes(2); |
| 2560 | + }); |
| 2561 | + |
| 2562 | + it('Should correctly handle ngControl validity', () => { |
| 2563 | + pending('Convert existing form test here'); |
| 2564 | + }); |
| 2565 | +}); |
2496 | 2566 |
|
2497 | 2567 | @Component({ |
2498 | 2568 | template: ` |
@@ -2820,28 +2890,27 @@ class IgxSelectHeaderFooterComponent implements OnInit { |
2820 | 2890 | } |
2821 | 2891 | } |
2822 | 2892 |
|
2823 | | - @Component({ |
2824 | | - template: ` |
2825 | | - <h4>*ngIf test select for 'expression changed...console Warning'</h4> |
2826 | | - <div *ngIf="render"> |
2827 | | - <igx-select #selectCDR value="ID"> |
2828 | | - <label igxLabel>Column</label> |
2829 | | - <igx-select-item *ngFor="let column of columns" [value]="column.field"> |
2830 | | - {{column.field}} |
2831 | | - </igx-select-item> |
2832 | | - </igx-select> |
2833 | | - </div> |
2834 | | - ` |
2835 | | - }) |
2836 | | - class IgxSelectCDRComponent { |
2837 | | - @ViewChild('selectCDR', { read: IgxSelectComponent, static: false }) |
2838 | | - public select: IgxSelectComponent; |
2839 | | - |
2840 | | - public render = true; |
2841 | | - public columns: Array<any> = [ |
2842 | | - { field: 'ID', type: 'string' }, |
2843 | | - { field: 'CompanyName', type: 'string' }, |
2844 | | - { field: 'ContactName', type: 'string' } |
2845 | | - ]; |
2846 | | - } |
2847 | | -}); |
| 2893 | +@Component({ |
| 2894 | + template: ` |
| 2895 | + <h4>*ngIf test select for 'expression changed...console Warning'</h4> |
| 2896 | + <div *ngIf="render"> |
| 2897 | + <igx-select #selectCDR value="ID"> |
| 2898 | + <label igxLabel>Column</label> |
| 2899 | + <igx-select-item *ngFor="let column of columns" [value]="column.field"> |
| 2900 | + {{column.field}} |
| 2901 | + </igx-select-item> |
| 2902 | + </igx-select> |
| 2903 | + </div> |
| 2904 | + ` |
| 2905 | +}) |
| 2906 | +class IgxSelectCDRComponent { |
| 2907 | + @ViewChild('selectCDR', { read: IgxSelectComponent, static: false }) |
| 2908 | + public select: IgxSelectComponent; |
| 2909 | + |
| 2910 | + public render = true; |
| 2911 | + public columns: Array<any> = [ |
| 2912 | + { field: 'ID', type: 'string' }, |
| 2913 | + { field: 'CompanyName', type: 'string' }, |
| 2914 | + { field: 'ContactName', type: 'string' } |
| 2915 | + ]; |
| 2916 | +} |
0 commit comments