Skip to content

Commit

Permalink
fix: add register custom view test
Browse files Browse the repository at this point in the history
  • Loading branch information
HandsomeButterball committed Dec 12, 2022
1 parent 4609b68 commit 90eca8a
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 1 deletion.
122 changes: 122 additions & 0 deletions packages/gantt/src/views/test/custom-view.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import {
GanttView,
GanttViewOptions,
primaryDatePointTop,
secondaryDatePointTop,
GanttViewDate,
GanttDate,
eachDayOfInterval,
GanttDatePoint,
GanttViewType
} from 'ngx-gantt';

const viewOptions: GanttViewOptions = {
cellWidth: 50,
start: new GanttDate().startOfYear().startOfWeek({ weekStartsOn: 1 }),
end: new GanttDate().endOfYear().endOfWeek({ weekStartsOn: 1 }),
addAmount: 1,
addUnit: 'month'
};

export class GanttViewCustom extends GanttView {
override showWeekBackdrop = true;

override showTimeline = true;

override viewType = GanttViewType.day;

constructor(start: GanttViewDate, end: GanttViewDate, options?: GanttViewOptions) {
super(start, end, Object.assign({}, viewOptions, options));
}

startOf(date: GanttDate) {
return date.startOfWeek({ weekStartsOn: 1 });
}

endOf(date: GanttDate) {
return date.endOfWeek({ weekStartsOn: 1 });
}

getPrimaryWidth() {
if (!this.options.showWeekend) {
return this.getCellWidth() * 5;
} else {
return this.getCellWidth() * 7;
}
}

getDayOccupancyWidth(date: GanttDate): number {
if (!this.options.showWeekend && date.isWeekend()) {
return 0;
}
return this.cellWidth;
}

getPrimaryDatePoints(): GanttDatePoint[] {
const days = eachDayOfInterval({ start: this.start.value, end: this.end.value });
const points: GanttDatePoint[] = [];
const dayInWeekMap = {
'1': '周一',
'2': '周二',
'3': '周三',
'4': '周四',
'5': '周五',
'6': '周六',
'0': '周日'
};
for (let i = 0; i < days.length; i++) {
const start = new GanttDate(days[i]);
const isWeekend = start.isWeekend();
const point = new GanttDatePoint(
start,
`${dayInWeekMap[start.getDay()]}`,
i * this.getCellWidth() + this.getCellWidth() / 2,
primaryDatePointTop,
{
isWeekend,
isToday: start.isToday()
}
);
points.push(point);
}
if (!this.options.showWeekend) {
return points
.filter((point) => !point.additions.isWeekend)
.map((point, i) => {
return { ...point, x: i * this.getCellWidth() + this.getCellWidth() / 2 };
});
} else {
return points;
}
}

getSecondaryDatePoints(): GanttDatePoint[] {
const days = eachDayOfInterval({ start: this.start.value, end: this.end.value });
const points: GanttDatePoint[] = [];
for (let i = 0; i < days.length; i++) {
const start = new GanttDate(days[i]);
const isWeekend = start.isWeekend();
const point = new GanttDatePoint(
start,
`${start.format('MM/d')}`,
i * this.getCellWidth() + this.getCellWidth() / 2,
secondaryDatePointTop,
{
isWeekend,
isToday: start.isToday()
}
);
points.push(point);
}

if (!this.options.showWeekend) {
return points
.filter((point) => !point.additions.isWeekend)
.map((point, i) => {
return { ...point, x: i * this.getCellWidth() + this.getCellWidth() / 2 };
});
} else {
return points;
}
}
}
12 changes: 11 additions & 1 deletion packages/gantt/src/views/test/factory.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { GanttViewMonth } from '../month';
import { GanttViewDay } from '../day';
import { GanttViewQuarter } from '../quarter';
import { createViewFactory } from '../factory';
import { createViewFactory, registerView } from '../factory';
import { GanttViewType } from '../../class';
import { date } from './mock';
import { GanttViewYear } from '../year';
import { GanttViewWeek } from '../week';
import { GanttViewCustom } from './custom-view.mock';

describe('CreateViewFactory', () => {
it(`should be day view`, () => {
Expand Down Expand Up @@ -39,3 +40,12 @@ describe('CreateViewFactory', () => {
// }).toThrow(new Error('gantt view type invalid'));
// });
});

describe('RegisterView', () => {
it(`should register custom view`, () => {
const viewType = 'custom';
registerView(viewType, GanttViewCustom);
const customView = createViewFactory(viewType as GanttViewType, date.start, date.end);
expect(customView).toEqual(jasmine.any(GanttViewCustom));
});
});

0 comments on commit 90eca8a

Please sign in to comment.