|
| 1 | +import { Component, ViewChild } from '@angular/core'; |
| 2 | +import { async, ComponentFixture, TestBed } from '@angular/core/testing'; |
| 3 | +import { MarkerState } from '../markers/marker.state'; |
| 4 | +import { TreeNode } from '../tree-node'; |
| 5 | +import { IndicatorBoxComponent } from './indicator-box.component'; |
| 6 | +import { By } from '@angular/platform-browser'; |
| 7 | + |
| 8 | +describe('IndicatorBoxComponent', () => { |
| 9 | + let component: IndicatorBoxComponent; |
| 10 | + let hostComponent: TestHostComponent; |
| 11 | + let fixture: ComponentFixture<TestHostComponent>; |
| 12 | + |
| 13 | + const sampleMarkerStates: MarkerState[] = [{ |
| 14 | + condition: (marker) => marker.sampleField && marker.otherField === 'test', |
| 15 | + cssClasses: 'someClass', |
| 16 | + label: (marker) => `sampleField = ${marker.sampleField}`, |
| 17 | + }, { |
| 18 | + condition: (marker) => !marker.sampleField, |
| 19 | + cssClasses: 'otherClass', |
| 20 | + label: (marker) => `otherField = ${marker.otherField}`, |
| 21 | + }]; |
| 22 | + |
| 23 | + @Component({ |
| 24 | + selector: `app-host-component`, |
| 25 | + template: `<app-indicator-box [model]="{'node': node, 'possibleStates': states}"></app-indicator-box>` |
| 26 | + }) |
| 27 | + class TestHostComponent { |
| 28 | + @ViewChild(IndicatorBoxComponent) |
| 29 | + public indicatorBoxComponentUnderTest: IndicatorBoxComponent; |
| 30 | + |
| 31 | + node: TreeNode; |
| 32 | + states: MarkerState[]; |
| 33 | + } |
| 34 | + |
| 35 | + beforeEach(async(() => { |
| 36 | + TestBed.configureTestingModule({ |
| 37 | + declarations: [ |
| 38 | + IndicatorBoxComponent, TestHostComponent |
| 39 | + ] |
| 40 | + }) |
| 41 | + .compileComponents(); |
| 42 | + })); |
| 43 | + |
| 44 | + beforeEach(() => { |
| 45 | + fixture = TestBed.createComponent(TestHostComponent); |
| 46 | + hostComponent = fixture.componentInstance; |
| 47 | + component = hostComponent.indicatorBoxComponentUnderTest; |
| 48 | + }); |
| 49 | + |
| 50 | + it('Can be instantiated', () => { |
| 51 | + expect(component).toBeTruthy(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('sets the css classes of the active marker state and "fa-fw"', () => { |
| 55 | + // given |
| 56 | + const marker = {sampleField: true, otherField: 'test'}; |
| 57 | + hostComponent.node = {children: [], name: 'sampleNode', root: null, marker: marker }; |
| 58 | + hostComponent.states = sampleMarkerStates; |
| 59 | + fixture.detectChanges(); |
| 60 | + |
| 61 | + // when |
| 62 | + const actualCssClasses = hostComponent.indicatorBoxComponentUnderTest.cssClasses; |
| 63 | + |
| 64 | + // then |
| 65 | + expect(actualCssClasses).toEqual('someClass fa-fw'); |
| 66 | + }); |
| 67 | + |
| 68 | + it('uses the active marker state`s label and css classes', () => { |
| 69 | + // given |
| 70 | + const marker = {sampleField: true, otherField: 'test'}; |
| 71 | + hostComponent.node = {children: [], name: 'sampleNode', root: null, marker: marker }; |
| 72 | + hostComponent.states = sampleMarkerStates; |
| 73 | + |
| 74 | + // when |
| 75 | + fixture.detectChanges(); |
| 76 | + |
| 77 | + // then |
| 78 | + const indicatorBoxTag = fixture.debugElement.query(By.css('div')); |
| 79 | + expect(indicatorBoxTag.nativeElement.attributes['title'].value).toEqual('sampleField = true'); |
| 80 | + expect(indicatorBoxTag.nativeElement.className).toEqual('someClass fa-fw'); |
| 81 | + }); |
| 82 | + |
| 83 | + it('changes label and css classes in accordance with changing marker states', () => { |
| 84 | + // given |
| 85 | + const marker = {sampleField: true, otherField: 'test'}; |
| 86 | + hostComponent.node = {children: [], name: 'sampleNode', root: null, marker: marker }; |
| 87 | + hostComponent.states = sampleMarkerStates; |
| 88 | + fixture.detectChanges(); |
| 89 | + const indicatorBoxTag = fixture.debugElement.query(By.css('div')); |
| 90 | + expect(indicatorBoxTag.nativeElement.attributes['title'].value).toEqual('sampleField = true'); |
| 91 | + expect(indicatorBoxTag.nativeElement.className).toEqual('someClass fa-fw'); |
| 92 | + |
| 93 | + // when |
| 94 | + marker.sampleField = false; |
| 95 | + fixture.detectChanges(); |
| 96 | + |
| 97 | + // then |
| 98 | + expect(indicatorBoxTag.nativeElement.attributes['title'].value).toEqual('otherField = test'); |
| 99 | + expect(indicatorBoxTag.nativeElement.className).toEqual('otherClass fa-fw'); |
| 100 | + }); |
| 101 | + |
| 102 | + it('handles exceptions in condition expressions gracefully and allows other state to become active', () => { |
| 103 | + // given |
| 104 | + const marker = {sampleField: true, otherField: 'test'}; |
| 105 | + hostComponent.node = {children: [], name: 'sampleNode', root: null, marker: marker }; |
| 106 | + hostComponent.states = sampleMarkerStates.slice(0); |
| 107 | + hostComponent.states.unshift({ |
| 108 | + condition: (marker_) => marker_.nonExisting.property === true, |
| 109 | + cssClasses: 'brokenStateClass', |
| 110 | + label: (marker_) => { throw new Error('broken label provider'); }, |
| 111 | + }); |
| 112 | + |
| 113 | + // when |
| 114 | + fixture.detectChanges(); |
| 115 | + |
| 116 | + // then |
| 117 | + const indicatorBoxTag = fixture.debugElement.query(By.css('div')); |
| 118 | + expect(indicatorBoxTag.nativeElement.attributes['title'].value).toEqual('sampleField = true'); |
| 119 | + expect(indicatorBoxTag.nativeElement.className).toEqual('someClass fa-fw'); |
| 120 | + }); |
| 121 | + |
| 122 | + it('handles exceptions in condition expressions gracefully and resorts to defaults when no state is active', () => { |
| 123 | + // given |
| 124 | + // given |
| 125 | + const marker = {sampleField: true, otherField: 'no state can become active now'}; |
| 126 | + hostComponent.node = {children: [], name: 'sampleNode', root: null, marker: marker }; |
| 127 | + hostComponent.states = sampleMarkerStates.slice(0); |
| 128 | + hostComponent.states.unshift({ |
| 129 | + condition: (marker_) => marker_.nonExisting.property === true, |
| 130 | + cssClasses: 'brokenStateClass', |
| 131 | + label: (marker_) => { throw new Error('broken label provider'); }, |
| 132 | + }); |
| 133 | + |
| 134 | + // when |
| 135 | + fixture.detectChanges(); |
| 136 | + |
| 137 | + // then |
| 138 | + const indicatorBoxTag = fixture.debugElement.query(By.css('div')); |
| 139 | + expect(indicatorBoxTag.nativeElement.className).toEqual('fa-fw'); |
| 140 | + expect(indicatorBoxTag.nativeElement.attributes['title'].value).toEqual(''); |
| 141 | + }); |
| 142 | +}); |
0 commit comments