|
| 1 | +import { Component, ViewChild } from '@angular/core'; |
| 2 | +import { CommandComponent } from './command.component'; |
| 3 | +import { ComponentFixture, TestBed } from '@angular/core/testing'; |
| 4 | +import { async } from 'q'; |
| 5 | +import { MockDirective } from 'ng-mocks'; |
| 6 | +import { LoadImageDirective } from '../../load-image/load-image.directive'; |
| 7 | +import { By } from '@angular/platform-browser'; |
| 8 | +import { KeyCodes } from '../../../shared/models/constants'; |
| 9 | + |
| 10 | +@Component({ |
| 11 | + selector: `app-command-host-component`, |
| 12 | + template: `<command [displayText]="displayText" [iconUrl]="iconUrl" [disabled]="disabled" (click)="onClick($event)"></command>` |
| 13 | +}) |
| 14 | +class TestCommandHostComponent { |
| 15 | + @ViewChild(CommandComponent) |
| 16 | + public commandComponent: CommandComponent; |
| 17 | + |
| 18 | + public iconUrl = 'testicon'; |
| 19 | + public displayText = 'testtext'; |
| 20 | + public disabled = false; |
| 21 | + |
| 22 | + public clicked = false; |
| 23 | + public onClick(event) { |
| 24 | + this.clicked = true; |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +describe('CommandControl', () => { |
| 29 | + let commandComponent: CommandComponent; |
| 30 | + let hostComponent: TestCommandHostComponent; |
| 31 | + let testFixture: ComponentFixture<TestCommandHostComponent>; |
| 32 | + |
| 33 | + beforeEach(async(() => { |
| 34 | + TestBed.configureTestingModule({ |
| 35 | + declarations: [CommandComponent, TestCommandHostComponent, MockDirective(LoadImageDirective)] |
| 36 | + }) |
| 37 | + .compileComponents(); |
| 38 | + |
| 39 | + })); |
| 40 | + |
| 41 | + beforeEach(() => { |
| 42 | + testFixture = TestBed.createComponent(TestCommandHostComponent); |
| 43 | + hostComponent = testFixture.componentInstance; |
| 44 | + commandComponent = hostComponent.commandComponent; |
| 45 | + testFixture.detectChanges(); |
| 46 | + }); |
| 47 | + |
| 48 | + describe('init', () => { |
| 49 | + it('should init control', () => { |
| 50 | + expect(commandComponent).toBeTruthy(); |
| 51 | + }); |
| 52 | + it('should have correct text and icon', () => { |
| 53 | + expect(commandComponent.displayText).toBe('testtext'); |
| 54 | + expect(commandComponent.iconUrl).toBe('testicon'); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should be enabled by default', () => { |
| 58 | + expect(commandComponent.disabled).toBeFalsy(); |
| 59 | + }); |
| 60 | + |
| 61 | + }); |
| 62 | + |
| 63 | + describe('Enabled Behavior', () => { |
| 64 | + it('should not have disabled-command class', () => { |
| 65 | + const elem = testFixture.debugElement.query(By.css('.disabled-command')); |
| 66 | + expect(elem).toBeFalsy(); |
| 67 | + }); |
| 68 | + |
| 69 | + it('click should trigger click event', () => { |
| 70 | + const elem = testFixture.debugElement.query(By.css('a')); |
| 71 | + elem.nativeElement.click(); |
| 72 | + expect(hostComponent.clicked).toBeTruthy(); |
| 73 | + }); |
| 74 | + |
| 75 | + it('enter keypress should trigger click event', () => { |
| 76 | + const elem = testFixture.debugElement.query(By.css('a')); |
| 77 | + elem.triggerEventHandler('keydown', { |
| 78 | + keyCode: KeyCodes.enter |
| 79 | + }); |
| 80 | + expect(hostComponent.clicked).toBeTruthy(); |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + describe('Disabled Behavior', () => { |
| 85 | + it('should have disabled-command class', () => { |
| 86 | + hostComponent.disabled = true; |
| 87 | + testFixture.detectChanges(); |
| 88 | + const elem = testFixture.debugElement.query(By.css('.disabled-command')); |
| 89 | + expect(elem).toBeTruthy(); |
| 90 | + }); |
| 91 | + it('click should not trigger click event', () => { |
| 92 | + hostComponent.disabled = true; |
| 93 | + testFixture.detectChanges(); |
| 94 | + const elem = testFixture.debugElement.query(By.css('a')); |
| 95 | + elem.nativeElement.click(); |
| 96 | + expect(hostComponent.clicked).toBeFalsy(); |
| 97 | + }); |
| 98 | + |
| 99 | + it('enter keypress should trigger click event', () => { |
| 100 | + hostComponent.disabled = true; |
| 101 | + testFixture.detectChanges(); |
| 102 | + const elem = testFixture.debugElement.query(By.css('a')); |
| 103 | + elem.triggerEventHandler('keydown', { |
| 104 | + keyCode: KeyCodes.enter |
| 105 | + }); |
| 106 | + expect(hostComponent.clicked).toBeFalsy(); |
| 107 | + }); |
| 108 | + }); |
| 109 | +}); |
0 commit comments