|
| 1 | +import { Component, DebugElement, ViewChild } from '@angular/core'; |
| 2 | +import { ComponentFixture, TestBed } from '@angular/core/testing'; |
| 3 | +import { MatButtonModule } from '@angular/material/button'; |
| 4 | +import { MatIconModule } from '@angular/material/icon'; |
| 5 | +import { MatTooltipModule } from '@angular/material/tooltip'; |
| 6 | +import { By } from '@angular/platform-browser'; |
| 7 | +import { FilteredResouces } from '@dasch-swiss/dsp-ui'; |
| 8 | +import { IntermediateComponent } from './intermediate.component'; |
| 9 | + |
| 10 | +/** |
| 11 | + * test host component to simulate parent component |
| 12 | + */ |
| 13 | +@Component({ |
| 14 | + template: '<app-intermediate #intermediateView [resources]="resources"></app-intermediate>' |
| 15 | +}) |
| 16 | +class OneSelectedResourcesComponent { |
| 17 | + |
| 18 | + @ViewChild('intermediateView') intermediateComponent: IntermediateComponent; |
| 19 | + |
| 20 | + resources: FilteredResouces = { |
| 21 | + 'count': 1, |
| 22 | + 'resListIndex': [1], |
| 23 | + 'resIds': [ |
| 24 | + 'http://rdfh.ch/0803/83616f8d8501' |
| 25 | + ], |
| 26 | + 'selectionType': 'multiple' |
| 27 | + }; |
| 28 | + |
| 29 | + constructor() { } |
| 30 | + |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * test host component to simulate parent component |
| 35 | + */ |
| 36 | +@Component({ |
| 37 | + template: '<app-intermediate #intermediateView [resources]="resources"></app-intermediate>' |
| 38 | +}) |
| 39 | +class ThreeSelectedResourcesComponent { |
| 40 | + |
| 41 | + @ViewChild('intermediateView') intermediateComponent: IntermediateComponent; |
| 42 | + |
| 43 | + resources: FilteredResouces = { |
| 44 | + 'count': 3, |
| 45 | + 'resListIndex': [3, 2, 1], |
| 46 | + 'resIds': [ |
| 47 | + 'http://rdfh.ch/0803/83616f8d8501', |
| 48 | + 'http://rdfh.ch/0803/71e0b9958a01', |
| 49 | + 'http://rdfh.ch/0803/683d5cd26f01' |
| 50 | + ], |
| 51 | + 'selectionType': 'multiple' |
| 52 | + }; |
| 53 | + |
| 54 | + constructor() { } |
| 55 | + |
| 56 | +} |
| 57 | + |
| 58 | +describe('IntermediateComponent', () => { |
| 59 | + |
| 60 | + let host1Component: OneSelectedResourcesComponent; |
| 61 | + let host1Fixture: ComponentFixture<OneSelectedResourcesComponent>; |
| 62 | + |
| 63 | + let host3Component: ThreeSelectedResourcesComponent; |
| 64 | + let host3Fixture: ComponentFixture<ThreeSelectedResourcesComponent>; |
| 65 | + |
| 66 | + beforeEach(async () => { |
| 67 | + await TestBed.configureTestingModule({ |
| 68 | + declarations: [ |
| 69 | + IntermediateComponent, |
| 70 | + OneSelectedResourcesComponent, |
| 71 | + ThreeSelectedResourcesComponent |
| 72 | + ], |
| 73 | + imports: [ |
| 74 | + MatButtonModule, |
| 75 | + MatIconModule, |
| 76 | + MatTooltipModule |
| 77 | + ] |
| 78 | + }) |
| 79 | + .compileComponents(); |
| 80 | + }); |
| 81 | + |
| 82 | + beforeEach(() => { |
| 83 | + host1Fixture = TestBed.createComponent(OneSelectedResourcesComponent); |
| 84 | + host1Component = host1Fixture.componentInstance; |
| 85 | + host1Fixture.detectChanges(); |
| 86 | + |
| 87 | + expect(host1Component).toBeTruthy(); |
| 88 | + |
| 89 | + host3Fixture = TestBed.createComponent(ThreeSelectedResourcesComponent); |
| 90 | + host3Component = host3Fixture.componentInstance; |
| 91 | + host3Fixture.detectChanges(); |
| 92 | + |
| 93 | + expect(host3Component).toBeTruthy(); |
| 94 | + |
| 95 | + }); |
| 96 | + |
| 97 | + describe('One selected resource', () => { |
| 98 | + |
| 99 | + it('expect count to be "1" and text to be "Resource Selected', () => { |
| 100 | + expect(host1Component.intermediateComponent).toBeTruthy(); |
| 101 | + expect(host1Component.intermediateComponent.resources).toBeDefined(); |
| 102 | + |
| 103 | + const hostCompDe = host1Fixture.debugElement; |
| 104 | + |
| 105 | + const count: DebugElement = hostCompDe.query(By.css('.count')); |
| 106 | + expect(count.nativeElement.innerText).toEqual('1'); |
| 107 | + const text: DebugElement = hostCompDe.query(By.css('.text')); |
| 108 | + expect(text.nativeElement.innerText).toEqual('Resource Selected'); |
| 109 | + |
| 110 | + }); |
| 111 | + |
| 112 | + it('expect compare button to be disabled', () => { |
| 113 | + expect(host1Component.intermediateComponent).toBeTruthy(); |
| 114 | + expect(host1Component.intermediateComponent.resources).toBeDefined(); |
| 115 | + |
| 116 | + const hostCompDe = host1Fixture.debugElement; |
| 117 | + |
| 118 | + const button: DebugElement = hostCompDe.query(By.css('.compare')); |
| 119 | + expect(button.nativeElement.disabled).toBeTruthy(); |
| 120 | + }); |
| 121 | + |
| 122 | + it('expect no card stack', () => { |
| 123 | + expect(host1Component.intermediateComponent).toBeTruthy(); |
| 124 | + expect(host1Component.intermediateComponent.resources).toBeDefined(); |
| 125 | + |
| 126 | + const hostCompDe = host1Fixture.debugElement; |
| 127 | + |
| 128 | + const twoCards: DebugElement = hostCompDe.query(By.css('.two')); |
| 129 | + expect(twoCards).toBeFalsy(); |
| 130 | + const threeCards: DebugElement = hostCompDe.query(By.css('.three')); |
| 131 | + expect(threeCards).toBeFalsy(); |
| 132 | + const moreCards: DebugElement = hostCompDe.query(By.css('.more')); |
| 133 | + expect(moreCards).toBeFalsy(); |
| 134 | + }); |
| 135 | + |
| 136 | + }); |
| 137 | + |
| 138 | + describe('Three selected resources', () => { |
| 139 | + |
| 140 | + it('expect count to be "3" and text to be "Resources Selected', () => { |
| 141 | + expect(host3Component.intermediateComponent).toBeTruthy(); |
| 142 | + expect(host3Component.intermediateComponent.resources).toBeDefined(); |
| 143 | + |
| 144 | + const hostCompDe = host3Fixture.debugElement; |
| 145 | + |
| 146 | + const count: DebugElement = hostCompDe.query(By.css('.count')); |
| 147 | + expect(count.nativeElement.innerText).toEqual('3'); |
| 148 | + const text: DebugElement = hostCompDe.query(By.css('.text')); |
| 149 | + expect(text.nativeElement.innerText).toEqual('Resources Selected'); |
| 150 | + |
| 151 | + }); |
| 152 | + |
| 153 | + it('expect compare button to be enabled', () => { |
| 154 | + expect(host3Component.intermediateComponent).toBeTruthy(); |
| 155 | + expect(host3Component.intermediateComponent.resources).toBeDefined(); |
| 156 | + |
| 157 | + const hostCompDe = host3Fixture.debugElement; |
| 158 | + |
| 159 | + const button: DebugElement = hostCompDe.query(By.css('.compare')); |
| 160 | + expect(button.nativeElement.disabled).toBeFalsy(); |
| 161 | + }); |
| 162 | + |
| 163 | + it('expect card stack of more than 2', () => { |
| 164 | + expect(host3Component.intermediateComponent).toBeTruthy(); |
| 165 | + expect(host3Component.intermediateComponent.resources).toBeDefined(); |
| 166 | + |
| 167 | + const hostCompDe = host3Fixture.debugElement; |
| 168 | + |
| 169 | + const twoCards: DebugElement = hostCompDe.query(By.css('.two')); |
| 170 | + expect(twoCards.nativeElement).toBeDefined(); |
| 171 | + const threeCards: DebugElement = hostCompDe.query(By.css('.three')); |
| 172 | + expect(threeCards.nativeElement).toBeDefined(); |
| 173 | + const moreCards: DebugElement = hostCompDe.query(By.css('.more')); |
| 174 | + expect(moreCards).toBeFalsy(); |
| 175 | + }); |
| 176 | + }); |
| 177 | + |
| 178 | +}); |
0 commit comments