Skip to content

Commit

Permalink
fix(view): fix view test #INFR-1690 (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
HandsomeButterball authored Apr 7, 2021
1 parent 4890e02 commit 6ca26af
Show file tree
Hide file tree
Showing 7 changed files with 296 additions and 77 deletions.
45 changes: 45 additions & 0 deletions packages/gantt/src/views/test/day.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { GanttViewDay } from '../day';
import { GanttDate } from '../../utils/date';
import { date, today } from './mock';

describe('GanttViewDay', () => {
let ganttViewDay: GanttViewDay;

beforeEach(() => {
ganttViewDay = new GanttViewDay(date.start, date.end, {
cellWidth: 20,
start: today.startOfYear().startOfWeek({ weekStartsOn: 1 }),
end: today.endOfYear().endOfWeek({ weekStartsOn: 1 })
});
});

it(`should has correct view start`, () => {
const startOfDay = ganttViewDay.startOf(date.start.date).getUnixTime();
expect(startOfDay).toEqual(new GanttDate('2019-12-30 00:00:00').getUnixTime());
});

it(`should has correct view end`, () => {
const endOfDay = ganttViewDay.endOf(date.end.date).getUnixTime();
expect(endOfDay).toEqual(new GanttDate('2021-01-03 23:59:59').getUnixTime());
});

it(`should has correct cell width`, () => {
const dayCellWidth = ganttViewDay.getDayOccupancyWidth();
expect(dayCellWidth).toEqual(20);
});

it(`should has correct primary width`, () => {
const dayPrimaryWidth = ganttViewDay.getPrimaryWidth();
expect(dayPrimaryWidth).toEqual(140);
});

it(`should has correct primary date points`, () => {
const dayPoints = ganttViewDay.getPrimaryDatePoints();
expect(dayPoints.length).toBe(54);
});

it(`should has correct secondary date points`, () => {
const dayPoints = ganttViewDay.getSecondaryDatePoints();
expect(dayPoints.length).toBe(371);
});
});
29 changes: 29 additions & 0 deletions packages/gantt/src/views/test/factory.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { GanttViewMonth } from '../month';
import { GanttViewDay } from '../day';
import { GanttViewQuarter } from '../quarter';
import { createViewFactory } from '../factory';
import { GanttViewType } from '../../class';
import { date } from './mock';

describe('CreateViewFactory', () => {
it(`should be day view`, () => {
const dayView = createViewFactory(GanttViewType.day, date.start, date.end);
expect(dayView).toEqual(jasmine.any(GanttViewDay));
});

it(`should be month view`, () => {
const monthView = createViewFactory(GanttViewType.month, date.start, date.end);
expect(monthView).toEqual(jasmine.any(GanttViewMonth));
});

it(`should be quarter view`, () => {
const quarterView = createViewFactory(GanttViewType.quarter, date.start, date.end);
expect(quarterView).toEqual(jasmine.any(GanttViewQuarter));
});

it(`should throw error`, () => {
expect(() => {
createViewFactory(GanttViewType.year, date.start, date.end);
}).toThrow(new Error('gantt view type invalid'));
});
});
14 changes: 14 additions & 0 deletions packages/gantt/src/views/test/mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { GanttDate } from '../../utils/date';

export const today = new GanttDate('2020-02-01 00:00:00');

export const date = {
start: {
date: new GanttDate('2020-01-01 00:00:00'),
isCustom: true
},
end: {
date: new GanttDate('2020-12-31 00:00:00'),
isCustom: true
}
};
45 changes: 45 additions & 0 deletions packages/gantt/src/views/test/month.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { GanttViewMonth } from '../month';
import { GanttDate } from '../../utils/date';
import { date, today } from './mock';

describe('GanttViewMonth', () => {
let ganttViewMonth: GanttViewMonth;

beforeEach(() => {
ganttViewMonth = new GanttViewMonth(date.start, date.end, {
cellWidth: 310,
start: today.startOfQuarter().addQuarters(-1),
end: today.endOfQuarter().addQuarters(2)
});
});

it(`should has correct view start`, () => {
const startOfMonth = ganttViewMonth.startOf(date.start.date).getUnixTime();
expect(startOfMonth).toEqual(new GanttDate('2020-01-01 00:00:00').getUnixTime());
});

it(`should has correct view end`, () => {
const endOfMonth = ganttViewMonth.endOf(date.end.date).getUnixTime();
expect(endOfMonth).toEqual(new GanttDate('2020-12-31 23:59:59').getUnixTime());
});

it(`should has correct cell width`, () => {
const monthCellWidth = ganttViewMonth.getDayOccupancyWidth(date.start.date);
expect(monthCellWidth).toEqual(10);
});

it(`should has correct primary width`, () => {
const monthPrimaryWidth = ganttViewMonth.getPrimaryWidth();
expect(monthPrimaryWidth).toEqual(930);
});

it(`should has correct primary date points`, () => {
const monthPoints = ganttViewMonth.getPrimaryDatePoints();
expect(monthPoints.length).toBe(4);
});

it(`should has correct secondary date points`, () => {
const monthPoints = ganttViewMonth.getSecondaryDatePoints();
expect(monthPoints.length).toBe(12);
});
});
45 changes: 45 additions & 0 deletions packages/gantt/src/views/test/quarter.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { GanttDate } from '../../utils/date';
import { GanttViewQuarter } from '../quarter';
import { date, today } from './mock';

describe('GanttViewQuarter', () => {
let ganttViewQuarter: GanttViewQuarter;

beforeEach(() => {
ganttViewQuarter = new GanttViewQuarter(date.start, date.end, {
cellWidth: 910,
start: today.addYears(-1).startOfYear(),
end: today.addYears(1).endOfYear()
});
});

it(`should has correct view start`, () => {
const startOfQuarter = ganttViewQuarter.startOf(date.start.date).getUnixTime();
expect(startOfQuarter).toEqual(new GanttDate('2020-01-01 00:00:00').getUnixTime());
});

it(`should has correct view end`, () => {
const endOfQuarter = ganttViewQuarter.endOf(date.end.date).getUnixTime();
expect(endOfQuarter).toEqual(new GanttDate('2020-12-31 23:59:59').getUnixTime());
});

it(`should has correct cell width`, () => {
const quarterCellWidth = ganttViewQuarter.getDayOccupancyWidth(date.start.date);
expect(quarterCellWidth).toEqual(10);
});

it(`should has correct primary width`, () => {
const quarterPrimaryWidth = ganttViewQuarter.getPrimaryWidth();
expect(quarterPrimaryWidth).toEqual(3640);
});

it(`should has correct primary date points`, () => {
const quarterPoints = ganttViewQuarter.getPrimaryDatePoints();
expect(quarterPoints.length).toBe(1);
});

it(`should has correct secondary date points`, () => {
const quarterPoints = ganttViewQuarter.getSecondaryDatePoints();
expect(quarterPoints.length).toBe(4);
});
});
189 changes: 115 additions & 74 deletions packages/gantt/src/views/test/view.spec.ts
Original file line number Diff line number Diff line change
@@ -1,99 +1,140 @@
import { GanttViewMonth } from '../month';
import { GanttViewDay } from '../day';
import { GanttDatePoint } from '../../class';
import { GanttDate } from '../../utils/date';
import { GanttViewQuarter } from '../quarter';

const today = new GanttDate('2020-02-01 00:00:00');

const date = {
start: {
date: new GanttDate('2020-01-01 00:00:00'),
isCustom: true
},
end: {
date: new GanttDate('2020-12-31 00:00:00'),
isCustom: true

import { GanttView, GanttViewDate, GanttViewOptions } from '../view';
import { date, today } from './mock';

class GanttViewMock extends GanttView {
constructor(start: GanttViewDate, end: GanttViewDate, options?: GanttViewOptions) {
super(start, end, options);
}

startOf(): GanttDate {
return new GanttDate('2020-01-01 00:00:00');
}
};

const options = {
cellWidth: 20
};
endOf(): GanttDate {
return new GanttDate('2020-12-31 23:59:59');
}

getPrimaryWidth(): number {
return 3640;
}

getDayOccupancyWidth(): number {
return 10;
}

getPrimaryDatePoints(): GanttDatePoint[] {
return [
{
text: '2020年',
x: 1820,
y: 18,
start: new GanttDate('2020-01-01 00:00:00')
}
];
}

getSecondaryDatePoints(): GanttDatePoint[] {
return [
{
text: 'Q1',
x: 455,
y: 36,
start: new GanttDate('2020-01-01 00:00:00')
},
{
text: 'Q2',
x: 1365,
y: 36,
start: new GanttDate('2020-04-01 00:00:00')
},
{ text: 'Q3', x: 2275, y: 36, start: new GanttDate('2020-07-01 00:00:00') },
{ text: 'Q4', x: 3185, y: 36, start: new GanttDate('2020-10-01 00:00:00') }
];
}
}

describe('GanttView', () => {
let ganttViewDay: GanttViewDay;
let ganttViewMonth: GanttViewMonth;
let ganttViewQuarter: GanttViewQuarter;
let ganttView: GanttView;

beforeEach(() => {
ganttViewDay = new GanttViewDay(date.start, date.end, {
ganttView = new GanttViewMock(date.start, date.end, {
cellWidth: 20,
start: today.startOfYear().startOfWeek({ weekStartsOn: 1 }),
end: today.endOfYear().endOfWeek({ weekStartsOn: 1 })
});
ganttViewMonth = new GanttViewMonth(date.start, date.end, {
cellWidth: 310,
start: today.startOfQuarter().addQuarters(-1),
end: today.endOfQuarter().addQuarters(2)
});
ganttViewQuarter = new GanttViewQuarter(date.start, date.end, {
cellWidth: 910,
start: today.addYears(-1).startOfYear(),
end: today.addYears(1).endOfYear()
});
});

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(new GanttDate('2019-12-30 00:00:00').getUnixTime());
expect(startOfMonth).toEqual(new GanttDate('2020-01-01 00:00:00').getUnixTime());
expect(startOfQuarter).toEqual(new GanttDate('2020-01-01 00:00:00').getUnixTime());
it(`should has correct view options`, () => {
const options = ganttView.options;
const cellWidth = options.cellWidth;
const start = options.start.getUnixTime();
const end = options.end.getUnixTime();
const max = options.max.getUnixTime();
const min = options.min.getUnixTime();

expect(cellWidth).toEqual(20);
expect(start).toEqual(new GanttDate('2019-12-30 00:00:00').getUnixTime());
expect(end).toEqual(new GanttDate('2021-01-03 23:59:59').getUnixTime());
expect(max).toEqual(new GanttDate('2022-12-31 23:59:59').getUnixTime());
expect(min).toEqual(new GanttDate('2020-01-01 00:00:00').getUnixTime());
});

it(`should has correct start and end`, () => {
const start = ganttView.start.getUnixTime();
const end = ganttView.end.getUnixTime();
expect(start).toEqual(new GanttDate('2020-01-01 00:00:00').getUnixTime());
expect(end).toEqual(new GanttDate('2020-12-31 23:59:59').getUnixTime());
});

it(`should add start date`, () => {
const newDate = ganttView.addStartDate();
expect(newDate.start.getUnixTime()).toEqual(new GanttDate('2020-01-01 00:00:00').getUnixTime());
expect(newDate.end.getUnixTime()).toEqual(new GanttDate('2020-01-01 00:00:00').getUnixTime());
});

it(`should add end date`, () => {
const newDate = ganttView.addEndDate();
expect(newDate.start.getUnixTime()).toEqual(new GanttDate('2020-12-31 23:59:59').getUnixTime());
expect(newDate.end.getUnixTime()).toEqual(new GanttDate('2020-12-31 23:59:59').getUnixTime());
});

it(`should update date`, () => {
ganttView.updateDate(new GanttDate('2020-10-01 00:00:00'), new GanttDate('2023-02-01 00:00:00'));
expect(ganttView.start.getUnixTime()).toEqual(new GanttDate('2020-01-01 00:00:00').getUnixTime());
expect(ganttView.end.getUnixTime()).toEqual(new GanttDate('2020-12-31 23:59:59').getUnixTime());
});

it(`should get width`, () => {
const width = ganttView.getWidth();
expect(width).toEqual(80);
});

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(new GanttDate('2021-01-03 23:59:59').getUnixTime());
expect(endOfMonth).toEqual(new GanttDate('2020-12-31 23:59:59').getUnixTime());
expect(endOfQuarter).toEqual(new GanttDate('2020-12-31 23:59:59').getUnixTime());
it(`should get cell width`, () => {
const cellWidth = ganttView.getCellWidth();
expect(cellWidth).toEqual(20);
});

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 get today x point`, () => {
const xPoint = ganttView.getTodayXPoint();
expect(xPoint).toEqual(null);
});

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 get x point by date`, () => {
const xPoint = ganttView.getXPointByDate(new GanttDate('2020-02-01 00:00:00'));
expect(xPoint).toEqual(310);
});

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 get date by x point`, () => {
ganttView.getSecondaryDatePoints();
const pointDate = ganttView.getDateByXPoint(60);
expect(pointDate.getUnixTime()).toEqual(new GanttDate('2020-10-01 00:00:00').getUnixTime());
});

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);
it(`should get date range width`, () => {
const width = ganttView.getDateRangeWidth(new GanttDate('2020-03-01 00:00:00'), new GanttDate('2020-05-01 00:00:00'));
expect(width).toEqual(610);
});
});
Loading

0 comments on commit 6ca26af

Please sign in to comment.