Skip to content

Commit

Permalink
fix(util): add util test #INFR-1693
Browse files Browse the repository at this point in the history
  • Loading branch information
HandsomeButterball authored and walkerkay committed Apr 7, 2021
1 parent 6ca26af commit 4a642ac
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 1 deletion.
69 changes: 68 additions & 1 deletion packages/gantt/src/utils/test/date.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,50 @@
import { GanttDate } from '../date';
import { GanttDate, GanttDateUtil } from '../date';

describe('tiny-date', () => {
const date = new GanttDate('2020-2-2 20:20:20');

it('support getTime', () => expect(date.getTime()).toBe(date.value.getTime()));

it('support getDate', () => expect(date.getDate()).toBe(date.value.getDate()));

it('support getMilliseconds', () => expect(date.getMilliseconds()).toBe(date.value.getMilliseconds()));

it('support getDaysInMonth', () => {
expect(date.getDaysInMonth()).toBe(29);
});

it('support getDaysInQuarter', () => {
expect(date.getDaysInQuarter()).toBe(91);
});

it('support setDate', () => {
expect(date.setDate(10).getUnixTime()).toBe(new GanttDate('2020-02-10 20:20:20').getUnixTime());
});

it('support startOf', () => {
expect(date.startOfDay().getUnixTime()).toBe(new GanttDate('2020-02-02 00:00:00').getUnixTime());
expect(date.startOfMonth().getUnixTime()).toBe(new GanttDate('2020-02-01 00:00:00').getUnixTime());
});

it('support endOf', () => {
expect(date.endOfDay().getUnixTime()).toBe(new GanttDate('2020-02-02 23:59:59').getUnixTime());
expect(date.endOfMonth().getUnixTime()).toBe(new GanttDate('2020-02-29 23:59:59').getUnixTime());
});

it('support is', () => {
expect(date.isToday()).toBe(false);
expect(date.isWeekend()).toBe(true);
});

it('support add', () => {
let newGanttDate: GanttDate;

newGanttDate = date.addYears(1);
expect(newGanttDate.getYear()).toBe(date.getYear() + 1);

newGanttDate = date.addQuarters(1);
expect(newGanttDate.getUnixTime()).toBe(new GanttDate('2020-05-02 20:20:20').getUnixTime());

newGanttDate = date.addMonths(1);
expect(newGanttDate.getMonth()).toBe(date.getMonth() + 1);

Expand All @@ -32,7 +64,42 @@ describe('tiny-date', () => {
expect(newGanttDate.getSeconds()).toBe(date.getSeconds() + 1);
});

it('support add by type', () => {
let newGanttDate: GanttDate;

newGanttDate = date.add(1, 'year');
expect(newGanttDate.getYear()).toBe(date.getYear() + 1);

newGanttDate = date.add(1, 'quarter');
expect(newGanttDate.getUnixTime()).toBe(new GanttDate('2020-05-02 20:20:20').getUnixTime());

newGanttDate = date.add(1, 'month');
expect(newGanttDate.getMonth()).toBe(date.getMonth() + 1);

newGanttDate = date.add(1, 'week');
expect(newGanttDate.getWeek()).toBe(date.getWeek() + 1);

newGanttDate = date.add(1, 'day');
expect(newGanttDate.getDay()).toBe(date.getDay() + 1);

newGanttDate = date.add(1, 'hour');
expect(newGanttDate.getHours()).toBe(date.getHours() + 1);

newGanttDate = date.add(1, 'minute');
expect(newGanttDate.getMinutes()).toBe(date.getMinutes() + 1);

newGanttDate = date.add(1, 'second');
expect(newGanttDate.getSeconds()).toBe(date.getSeconds() + 1);

newGanttDate = date.add(1);
expect(newGanttDate.getSeconds()).toBe(date.getSeconds() + 1);
});

it('support clone', () => {
expect(date.getTime()).toBe(date.clone().getTime());
});

it('support format', () => {
expect(date.format('yyyy年QQQ')).toBe('2020年Q1');
});
});
73 changes: 73 additions & 0 deletions packages/gantt/src/utils/test/helpers.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { GanttItemInternal } from '../../class';
import { isNumber, isString, isUndefined, hexToRgb, uniqBy, flatten, recursiveItems } from '../helpers';

describe('helpers', () => {
it('isNumber', () => {
const result = isNumber('111');
const result1 = isNumber(111);

expect(result).toBe(false);
expect(result1).toBe(true);
});

it('isString', () => {
const result = isString('111');
const result1 = isString(111);

expect(result).toBe(true);
expect(result1).toBe(false);
});

it('isUndefined', () => {
const result = isUndefined('111');
const result1 = isUndefined(undefined);

expect(result).toBe(false);
expect(result1).toBe(true);
});

it('hexToRgb', () => {
const result = hexToRgb('#cccccc');
const result1 = hexToRgb('rgba(255,255,255)');

expect(result).toBe('rgba(204,204,204,1)');
expect(result1).toBe('rgba(255,255,255)');
});

it('uniqBy', () => {
const result = uniqBy([{ id: '3333' }, { id: '2222' }, { id: '3333' }], 'id');

expect(result.length).toBe(2);
});

it('flatten', () => {
const result = flatten([[{ id: '3333' }], [{ id: '2222' }], { id: '3333' }]);
result.forEach((value) => {
expect(value.length).toBe(undefined);
});
});

it('recursiveItems', () => {
const result = recursiveItems([
{
id: '3333',
expanded: true,
children: [
{
id: '3333-1'
}
]
} as GanttItemInternal,
{
id: '2222',
expanded: false,
children: [
{
id: '2222-1'
}
]
} as GanttItemInternal
]);
expect(result.length).toBe(3);
});
});

0 comments on commit 4a642ac

Please sign in to comment.