|
1 | 1 | import { IgxInputState } from './../directives/input/input.directive'; |
2 | 2 | import { Component, ViewChild, DebugElement, OnInit } 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 | inputGroupWithRequiredAsterisk = fix.debugElement.query(By.css('.' + CSS_CLASS_INPUT_GROUP_REQUIRED)); |
605 | 605 | expect(inputGroupWithRequiredAsterisk).toBeDefined(); |
606 | 606 | })); |
| 607 | + |
| 608 | + it('Should have correctly bound focus and blur handlers', () => { |
| 609 | + const fix = TestBed.createComponent(IgxSelectTemplateFormComponent); |
| 610 | + fix.detectChanges(); |
| 611 | + select = fix.componentInstance.select; |
| 612 | + const input = fix.debugElement.query(By.css(`.${CSS_CLASS_INPUT}`)); |
| 613 | + |
| 614 | + spyOn(select, 'onFocus'); |
| 615 | + spyOn(select, 'onBlur'); |
| 616 | + |
| 617 | + input.triggerEventHandler('focus', {}); |
| 618 | + expect(select.onFocus).toHaveBeenCalled(); |
| 619 | + expect(select.onFocus).toHaveBeenCalledWith(); |
| 620 | + |
| 621 | + input.triggerEventHandler('blur', {}); |
| 622 | + expect(select.onBlur).toHaveBeenCalled(); |
| 623 | + expect(select.onFocus).toHaveBeenCalledWith(); |
| 624 | + }); |
607 | 625 | }); |
608 | 626 | describe('Selection tests: ', () => { |
609 | 627 | beforeEach(async(() => { |
@@ -2248,6 +2266,57 @@ describe('igxSelect', () => { |
2248 | 2266 | }); |
2249 | 2267 | }); |
2250 | 2268 |
|
| 2269 | +describe('igxSelect ControlValueAccessor Unit', () => { |
| 2270 | + let select: IgxSelectComponent; |
| 2271 | + it('Should correctly implement interface methods', () => { |
| 2272 | + const mockSelection = jasmine.createSpyObj('IgxSelectionAPIService', ['get', 'set', 'clear', 'first_item']); |
| 2273 | + const mockCdr = jasmine.createSpyObj('ChangeDetectorRef', ['detectChanges']); |
| 2274 | + const mockNgControl = jasmine.createSpyObj('NgControl', ['registerOnChangeCb', 'registerOnTouchedCb']); |
| 2275 | + const mockInjector = jasmine.createSpyObj('Injector', { |
| 2276 | + 'get': mockNgControl |
| 2277 | + }); |
| 2278 | + |
| 2279 | + // init |
| 2280 | + select = new IgxSelectComponent(null, mockCdr, mockSelection, null, mockInjector); |
| 2281 | + select.ngOnInit(); |
| 2282 | + select.registerOnChange(mockNgControl.registerOnChangeCb); |
| 2283 | + select.registerOnTouched(mockNgControl.registerOnTouchedCb); |
| 2284 | + expect(mockInjector.get).toHaveBeenCalledWith(NgControl, null); |
| 2285 | + |
| 2286 | + // writeValue |
| 2287 | + expect(select.value).toBeUndefined(); |
| 2288 | + select.writeValue('test'); |
| 2289 | + expect(mockSelection.clear).toHaveBeenCalled(); |
| 2290 | + expect(select.value).toBe('test'); |
| 2291 | + |
| 2292 | + // setDisabledState |
| 2293 | + select.setDisabledState(true); |
| 2294 | + expect(select.disabled).toBe(true); |
| 2295 | + select.setDisabledState(false); |
| 2296 | + expect(select.disabled).toBe(false); |
| 2297 | + |
| 2298 | + // OnChange callback |
| 2299 | + const item = new IgxDropDownItemComponent(select, null, null, mockSelection); |
| 2300 | + item.value = 'itemValue'; |
| 2301 | + select.selectItem(item); |
| 2302 | + expect(mockSelection.set).toHaveBeenCalledWith(select.id, new Set([item])); |
| 2303 | + expect(mockNgControl.registerOnChangeCb).toHaveBeenCalledWith('itemValue'); |
| 2304 | + |
| 2305 | + // OnTouched callback |
| 2306 | + select.onFocus(); |
| 2307 | + expect(mockNgControl.registerOnTouchedCb).toHaveBeenCalledTimes(1); |
| 2308 | + |
| 2309 | + select.input = {} as any; |
| 2310 | + spyOnProperty(select, 'collapsed').and.returnValue(true); |
| 2311 | + select.onBlur(); |
| 2312 | + expect(mockNgControl.registerOnTouchedCb).toHaveBeenCalledTimes(2); |
| 2313 | + }); |
| 2314 | + |
| 2315 | + it('Should correctly handle ngControl validity', () => { |
| 2316 | + pending('Convert existing form test here'); |
| 2317 | + }); |
| 2318 | +}); |
| 2319 | + |
2251 | 2320 | @Component({ |
2252 | 2321 | template: ` |
2253 | 2322 | <igx-select #select [width]="'300px'" [height]="'200px'" [placeholder]="'Choose a city'" [(ngModel)]="value"> |
|
0 commit comments