Skip to content

Commit 2373719

Browse files
authored
refactor: organize test files (#506)
1 parent 042bda1 commit 2373719

29 files changed

+924
-908
lines changed

packages/gantt/src/table/test/table.spec.ts

Lines changed: 0 additions & 56 deletions
This file was deleted.

packages/gantt/src/test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from '@ang
77
import { setDefaultOptions } from 'date-fns';
88
import { zhCN } from 'date-fns/locale';
99

10-
setDefaultOptions({
11-
locale: zhCN
12-
});
10+
console.log('Default timezone:' + Intl.DateTimeFormat().resolvedOptions().timeZone);
1311

1412
// First, initialize the Angular testing environment.
1513
getTestBed().initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting(), {

packages/gantt/src/class/test/group.spec.ts renamed to packages/gantt/src/test/class/gantt-group.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { GanttDate } from '../../utils/date';
2-
import { GanttGroup, GanttGroupInternal } from '../group';
1+
import { GanttGroup, GanttGroupInternal } from '../../class/group';
32

43
const group: GanttGroup = {
54
id: '00001',

packages/gantt/src/class/test/item.spec.ts renamed to packages/gantt/src/test/class/gantt-item.spec.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { GanttLinkType } from 'ngx-gantt';
22
import { GanttDate } from '../../utils/date';
3-
import { GanttItem, GanttItemInternal } from '../item';
3+
import { GanttItem, GanttItemInternal } from '../../class/item';
44

55
class FakeView {
66
getDateByXPoint() {
@@ -111,12 +111,4 @@ describe('GanttItemInternal', () => {
111111
ganttItemInternal.setExpand(true);
112112
expect(ganttItemInternal.expanded).toBe(true);
113113
});
114-
115-
it(`should add link`, () => {
116-
ganttItemInternal.addLink({
117-
link: '0102',
118-
type: GanttLinkType.fs
119-
});
120-
// expect(ganttItemInternal.links).toContain('0102');
121-
});
122114
});
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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

Comments
 (0)