From e25a5b04ce0c964d65b90a1e81f37c5227895cae Mon Sep 17 00:00:00 2001 From: zhangwen <1062680993@qq.com> Date: Thu, 21 Jan 2021 10:27:45 +0800 Subject: [PATCH] fix: add gantt view test --- packages/gantt/src/gantt-upper.ts | 15 +++- packages/gantt/src/test/gantt-upper.spec.ts | 0 packages/gantt/src/views/day.ts | 2 +- packages/gantt/src/views/quarter.ts | 3 +- packages/gantt/src/views/test/view.spec.ts | 86 +++++++++++++++++++++ 5 files changed, 102 insertions(+), 4 deletions(-) create mode 100644 packages/gantt/src/test/gantt-upper.spec.ts create mode 100644 packages/gantt/src/views/test/view.spec.ts diff --git a/packages/gantt/src/gantt-upper.ts b/packages/gantt/src/gantt-upper.ts index f7109eb7..16d3ba09 100644 --- a/packages/gantt/src/gantt-upper.ts +++ b/packages/gantt/src/gantt-upper.ts @@ -1,4 +1,17 @@ -import { Input, TemplateRef, Output, EventEmitter, ContentChild, ElementRef, HostBinding, ChangeDetectorRef, NgZone, SimpleChanges, InjectionToken, Directive } from '@angular/core'; +import { + Input, + TemplateRef, + Output, + EventEmitter, + ContentChild, + ElementRef, + HostBinding, + ChangeDetectorRef, + NgZone, + SimpleChanges, + InjectionToken, + Directive +} from '@angular/core'; import { Subject } from 'rxjs'; import { takeUntil, take, skip } from 'rxjs/operators'; import { diff --git a/packages/gantt/src/test/gantt-upper.spec.ts b/packages/gantt/src/test/gantt-upper.spec.ts new file mode 100644 index 00000000..e69de29b diff --git a/packages/gantt/src/views/day.ts b/packages/gantt/src/views/day.ts index 3b634aaa..972a1bd3 100644 --- a/packages/gantt/src/views/day.ts +++ b/packages/gantt/src/views/day.ts @@ -31,7 +31,7 @@ export class GanttViewDay extends GanttView { return this.getCellWidth() * 7; } - getDayOccupancyWidth(date: GanttDate): number { + getDayOccupancyWidth(): number { return this.cellWidth; } diff --git a/packages/gantt/src/views/quarter.ts b/packages/gantt/src/views/quarter.ts index 4828cb8f..d44a5f45 100644 --- a/packages/gantt/src/views/quarter.ts +++ b/packages/gantt/src/views/quarter.ts @@ -37,7 +37,7 @@ export class GanttViewQuarter extends GanttView { getPrimaryDatePoints(): GanttDatePoint[] { const years = eachYearOfInterval({ start: this.start.value, end: this.end.value }); const points: GanttDatePoint[] = []; - for (let i = 0; i <= years.length; i++) { + for (let i = 0; i < years.length; i++) { const start = new GanttDate(years[i]); const point = new GanttDatePoint( start, @@ -47,7 +47,6 @@ export class GanttViewQuarter extends GanttView { ); points.push(point); } - return points; } diff --git a/packages/gantt/src/views/test/view.spec.ts b/packages/gantt/src/views/test/view.spec.ts new file mode 100644 index 00000000..7f745bb8 --- /dev/null +++ b/packages/gantt/src/views/test/view.spec.ts @@ -0,0 +1,86 @@ +import { GanttViewMonth } from '../month'; +import { GanttViewDay } from '../day'; +import { GanttDate } from '../../utils/date'; +import { GanttViewQuarter } from '../quarter'; + +const date = { + start: { + date: new GanttDate(1577808000), + isCustom: true + }, + end: { + date: new GanttDate(1609344000), + isCustom: true + } +}; + +const options = { + cellWidth: 20 +}; + +describe('GanttView', () => { + let ganttViewDay: GanttViewDay; + let ganttViewMonth: GanttViewMonth; + let ganttViewQuarter: GanttViewQuarter; + + beforeEach(() => { + ganttViewDay = new GanttViewDay(date.start, date.end, options); + ganttViewMonth = new GanttViewMonth(date.start, date.end, { cellWidth: 310 }); + ganttViewQuarter = new GanttViewQuarter(date.start, date.end, { cellWidth: 910 }); + }); + + it(`should has correct view start`, () => { + const startOfDay = ganttViewDay.startOf(date.start.date).getUnixTime(); + const startOfMonth = ganttViewMonth.startOf(date.start.date).getUnixTime(); + const startOfQuarter = ganttViewQuarter.startOf(date.start.date).getUnixTime(); + + expect(startOfDay).toEqual(1577635200); + expect(startOfMonth).toEqual(1577808000); + expect(startOfQuarter).toEqual(1577808000); + }); + + it(`should has correct view end`, () => { + const endOfDay = ganttViewDay.endOf(date.end.date).getUnixTime(); + const endOfMonth = ganttViewMonth.endOf(date.end.date).getUnixTime(); + const endOfQuarter = ganttViewQuarter.endOf(date.end.date).getUnixTime(); + expect(endOfDay).toEqual(1609689599); + expect(endOfMonth).toEqual(1609430399); + expect(endOfQuarter).toEqual(1609430399); + }); + + it(`should has correct cell width`, () => { + const dayCellWidth = ganttViewDay.getDayOccupancyWidth(); + const monthCellWidth = ganttViewMonth.getDayOccupancyWidth(date.start.date); + const quarterCellWidth = ganttViewQuarter.getDayOccupancyWidth(date.start.date); + expect(dayCellWidth).toEqual(20); + expect(monthCellWidth).toEqual(10); + expect(quarterCellWidth).toEqual(10); + }); + + it(`should has correct primary width`, () => { + const dayPrimaryWidth = ganttViewDay.getPrimaryWidth(); + const monthPrimaryWidth = ganttViewMonth.getPrimaryWidth(); + const quarterPrimaryWidth = ganttViewQuarter.getPrimaryWidth(); + expect(dayPrimaryWidth).toEqual(140); + expect(monthPrimaryWidth).toEqual(930); + expect(quarterPrimaryWidth).toEqual(3640); + }); + + it(`should has correct primary date points`, () => { + const dayPoints = ganttViewDay.getPrimaryDatePoints(); + const monthPoints = ganttViewMonth.getPrimaryDatePoints(); + const quarterPoints = ganttViewQuarter.getPrimaryDatePoints(); + expect(dayPoints.length).toBe(54); + expect(monthPoints.length).toBe(4); + expect(quarterPoints.length).toBe(1); + }); + + it(`should has correct secondary date points`, () => { + const dayPoints = ganttViewDay.getSecondaryDatePoints(); + const monthPoints = ganttViewMonth.getSecondaryDatePoints(); + const quarterPoints = ganttViewQuarter.getSecondaryDatePoints(); + expect(dayPoints.length).toBe(371); + expect(monthPoints.length).toBe(12); + expect(quarterPoints.length).toBe(4); + }); +});