|
| 1 | +import { Component, ViewChild } from '@angular/core'; |
| 2 | +import { ComponentFixture, fakeAsync, TestBed, waitForAsync } from '@angular/core/testing'; |
| 3 | +import { By } from '@angular/platform-browser'; |
| 4 | +import { NoopAnimationsModule } from '@angular/platform-browser/animations'; |
| 5 | +import { IgxCalendarComponent } from '../../calendar/public_api'; |
| 6 | +import { IgxButtonDirective, IgxButtonModule } from '../../directives/button/button.directive'; |
| 7 | +import { configureTestSuite } from '../../test-utils/configure-suite'; |
| 8 | +import { IgxPickerActionsDirective, IgxPickersCommonModule } from '../picker-icons.common'; |
| 9 | +import { IgxCalendarContainerComponent, IgxCalendarContainerModule } from './calendar-container.component'; |
| 10 | + |
| 11 | + |
| 12 | +describe('Calendar Container', () => { |
| 13 | + let fixture: ComponentFixture<IgxDatePickerTestComponent>; |
| 14 | + let container: IgxCalendarContainerComponent; |
| 15 | + configureTestSuite(); |
| 16 | + beforeAll(waitForAsync(() => { |
| 17 | + TestBed.configureTestingModule({ |
| 18 | + declarations: [ |
| 19 | + IgxDatePickerTestComponent |
| 20 | + ], |
| 21 | + imports: [IgxCalendarContainerModule, IgxPickersCommonModule, IgxButtonModule, NoopAnimationsModule] |
| 22 | + }) |
| 23 | + .compileComponents(); |
| 24 | + })); |
| 25 | + |
| 26 | + beforeEach(fakeAsync(() => { |
| 27 | + fixture = TestBed.createComponent(IgxDatePickerTestComponent); |
| 28 | + fixture.detectChanges(); |
| 29 | + container = fixture.componentInstance.container; |
| 30 | + })); |
| 31 | + |
| 32 | + it('should render calendar', () => { |
| 33 | + fixture = TestBed.createComponent(IgxDatePickerTestComponent); |
| 34 | + fixture.detectChanges(); |
| 35 | + const calendar = fixture.debugElement.query(By.directive(IgxCalendarComponent)); |
| 36 | + expect(calendar).toBeDefined(); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should render default actions', () => { |
| 40 | + spyOn(container.calendarClose, 'emit'); |
| 41 | + spyOn(container.todaySelection, 'emit'); |
| 42 | + container.closeButtonLabel = 'cancel'; |
| 43 | + fixture.detectChanges(); |
| 44 | + let buttons = fixture.debugElement.queryAll(By.directive(IgxButtonDirective)); |
| 45 | + expect(buttons).toHaveSize(1); |
| 46 | + expect(buttons[0].nativeElement.innerText).toEqual('cancel'); |
| 47 | + buttons[0].triggerEventHandler('click', {}); |
| 48 | + expect(container.calendarClose.emit).toHaveBeenCalledTimes(1); |
| 49 | + |
| 50 | + container.todayButtonLabel = 'ok'; |
| 51 | + fixture.detectChanges(); |
| 52 | + buttons = fixture.debugElement.queryAll(By.directive(IgxButtonDirective)); |
| 53 | + expect(buttons).toHaveSize(2); |
| 54 | + expect(buttons[1].nativeElement.innerText).toEqual('ok'); |
| 55 | + buttons[1].triggerEventHandler('click', {}); |
| 56 | + expect(container.todaySelection.emit).toHaveBeenCalledTimes(1); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should render default toggle and clear icons', () => { |
| 60 | + spyOn(fixture.componentInstance, 'doWork'); |
| 61 | + container.pickerActions = fixture.componentInstance.actions; |
| 62 | + fixture.detectChanges(); |
| 63 | + |
| 64 | + const calendar = fixture.debugElement.query(By.directive(IgxCalendarComponent)).componentInstance; |
| 65 | + const buttons = fixture.debugElement.queryAll(By.directive(IgxButtonDirective)); |
| 66 | + expect(buttons).toHaveSize(1); |
| 67 | + expect(buttons[0].nativeElement.innerText).toEqual('action'); |
| 68 | + buttons[0].triggerEventHandler('click', {}); |
| 69 | + expect(fixture.componentInstance.doWork).toHaveBeenCalledWith(calendar); |
| 70 | + }); |
| 71 | +}); |
| 72 | + |
| 73 | +@Component({ |
| 74 | + template: ` |
| 75 | + <igx-calendar-container> |
| 76 | + </igx-calendar-container> |
| 77 | + <ng-template igxPickerActions let-calendar> |
| 78 | + <button igxButton (click)="doWork(calendar)">action</button> |
| 79 | + </ng-template> |
| 80 | + ` |
| 81 | +}) |
| 82 | +export class IgxDatePickerTestComponent { |
| 83 | + @ViewChild(IgxCalendarContainerComponent) public container: IgxCalendarContainerComponent; |
| 84 | + @ViewChild(IgxPickerActionsDirective) public actions: IgxPickerActionsDirective; |
| 85 | + public doWork = (_calendar: any) => {}; |
| 86 | +} |
0 commit comments