|
| 1 | +import { DebugElement } from '@angular/core'; |
| 2 | +import { ComponentFixture } from '@angular/core/testing'; |
| 3 | +import { By } from '@angular/platform-browser'; |
| 4 | +import { |
| 5 | + GanttBaselineItem, |
| 6 | + GanttBaselineItemInternal, |
| 7 | + GanttCalendarHeaderComponent, |
| 8 | + GanttGroup, |
| 9 | + GanttItem, |
| 10 | + GanttItemInternal, |
| 11 | + NgxGanttBarComponent, |
| 12 | + NgxGanttBaselineComponent, |
| 13 | + NgxGanttComponent |
| 14 | +} from 'ngx-gantt'; |
| 15 | + |
| 16 | +interface TestGanttComponentBase { |
| 17 | + ganttComponent: NgxGanttComponent; |
| 18 | +} |
| 19 | + |
| 20 | +export function assertGanttView<T extends TestGanttComponentBase>( |
| 21 | + fixture: ComponentFixture<T>, |
| 22 | + expected: { |
| 23 | + firstPrimaryDataPointText: string; |
| 24 | + lastPrimaryDataPointText: string; |
| 25 | + firstSecondaryDataPointText: string; |
| 26 | + lastSecondaryDataPointText: string; |
| 27 | + } |
| 28 | +) { |
| 29 | + const calendarElement = fixture.debugElement.query(By.directive(GanttCalendarHeaderComponent)); |
| 30 | + const primaryElements = calendarElement.queryAll(By.css('.primary-text')); |
| 31 | + const secondaryElements = calendarElement.queryAll(By.css('.secondary-text')); |
| 32 | + expect(primaryElements.length).toEqual(fixture.componentInstance.ganttComponent.view.primaryDatePoints.length); |
| 33 | + expect(secondaryElements.length).toEqual(fixture.componentInstance.ganttComponent.view.secondaryDatePoints.length); |
| 34 | + expect(primaryElements[0].nativeElement.textContent).toContain(expected.firstPrimaryDataPointText); |
| 35 | + expect(primaryElements[primaryElements.length - 1].nativeElement.textContent).toContain(expected.lastPrimaryDataPointText); |
| 36 | + expect(secondaryElements[0].nativeElement.textContent).toContain(expected.firstSecondaryDataPointText); |
| 37 | + expect(secondaryElements[secondaryElements.length - 1].nativeElement.textContent).toContain(expected.lastSecondaryDataPointText); |
| 38 | +} |
| 39 | + |
| 40 | +export function assertItem(item: DebugElement, ganttItem: GanttItemInternal | GanttBaselineItemInternal) { |
| 41 | + const elem = item.nativeElement as HTMLElement; |
| 42 | + const top = elem.style.getPropertyValue('top'); |
| 43 | + const bottom = elem.style.getPropertyValue('bottom'); |
| 44 | + const left = elem.style.getPropertyValue('left'); |
| 45 | + const width = elem.style.getPropertyValue('width'); |
| 46 | + if (ganttItem instanceof GanttItemInternal) { |
| 47 | + expect(top).toEqual(ganttItem.refs.y + 'px'); |
| 48 | + } else { |
| 49 | + expect(bottom).toEqual(2 + 'px'); |
| 50 | + } |
| 51 | + |
| 52 | + expect(left).toEqual(ganttItem.refs.x + 'px'); |
| 53 | + expect(width).toEqual(ganttItem.refs.width + 'px'); |
| 54 | +} |
| 55 | + |
| 56 | +export function assertItems<T extends TestGanttComponentBase>(fixture: ComponentFixture<T>, expectedItems: GanttItem[]) { |
| 57 | + const { ganttComponent } = fixture.componentInstance; |
| 58 | + const items = fixture.debugElement.queryAll(By.directive(NgxGanttBarComponent)); |
| 59 | + expect(items.length).toEqual(expectedItems.length); |
| 60 | + items.forEach((item: DebugElement, index: number) => { |
| 61 | + expect(ganttComponent.items[index].id).toEqual(expectedItems[index].id); |
| 62 | + assertItem(item, ganttComponent.items[index]); |
| 63 | + }); |
| 64 | +} |
| 65 | + |
| 66 | +export function assertBaselineItems<T extends TestGanttComponentBase>(fixture: ComponentFixture<T>, expectedItems: GanttBaselineItem[]) { |
| 67 | + const { ganttComponent } = fixture.componentInstance; |
| 68 | + const items = fixture.debugElement.queryAll(By.directive(NgxGanttBaselineComponent)); |
| 69 | + expect(items.length).toEqual(expectedItems.length); |
| 70 | + items.forEach((item: DebugElement, index: number) => { |
| 71 | + expect(ganttComponent.baselineItems[index].id).toEqual(expectedItems[index].id); |
| 72 | + assertItem(item, ganttComponent.baselineItems[index]); |
| 73 | + }); |
| 74 | +} |
| 75 | + |
| 76 | +export function assertGroups<T extends TestGanttComponentBase>(fixture: ComponentFixture<T>, expectedGroups: GanttGroup[]) { |
| 77 | + const { ganttComponent } = fixture.componentInstance; |
| 78 | + const groups = fixture.debugElement.queryAll(By.css('.gantt-group')); |
| 79 | + groups.forEach((group: DebugElement, groupIndex: number) => { |
| 80 | + expect(ganttComponent.groups[groupIndex].id).toEqual(expectedGroups[groupIndex].id); |
| 81 | + const items = group.queryAll(By.directive(NgxGanttBarComponent)); |
| 82 | + items.forEach((item: DebugElement, itemIndex: number) => { |
| 83 | + assertItem(item, ganttComponent.groups[groupIndex].items[itemIndex]); |
| 84 | + }); |
| 85 | + }); |
| 86 | +} |
| 87 | + |
| 88 | +export function assertConfigStyle(ganttComponent: NgxGanttComponent, ganttDebugElement: DebugElement) { |
| 89 | + const styleOptionsBindElement = { |
| 90 | + headerHeight: ['.gantt-calendar-header', '.gantt-table-header'], |
| 91 | + lineHeight: ['.gantt-item', '.gantt-table-item', '.gantt-group', '.gantt-table-group'], |
| 92 | + barHeight: ['.gantt-bar'] |
| 93 | + }; |
| 94 | + for (const key in styleOptionsBindElement) { |
| 95 | + if (Object.prototype.hasOwnProperty.call(styleOptionsBindElement, key)) { |
| 96 | + const bindElementsClass = styleOptionsBindElement[key]; |
| 97 | + bindElementsClass.forEach((elementClass) => { |
| 98 | + const element = ganttDebugElement.query(By.css(elementClass)); |
| 99 | + if (element) { |
| 100 | + const height = element.nativeElement.style.getPropertyValue('height'); |
| 101 | + expect(height).toEqual(ganttComponent.configService.config.styleOptions[key] + 'px'); |
| 102 | + } |
| 103 | + }); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments