Skip to content

Commit eebecf4

Browse files
authored
Merge pull request #1726 from wix/infra/Timeline_tests
Add unit tests for the Packer util
2 parents 64cd964 + 86b3174 commit eebecf4

File tree

5 files changed

+89
-8
lines changed

5 files changed

+89
-8
lines changed

src/timeline/EventBlock.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {View, Text, TextStyle, TouchableOpacity, ViewStyle} from 'react-native';
33
import XDate from 'xdate';
44

55
export interface Event {
6+
id?: string;
67
start: string;
78
end: string;
89
title: string;

src/timeline/Packer.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@
22
import XDate from 'xdate';
33
import {Event, PackedEvent} from './EventBlock';
44

5-
export const HALF_HOUR_BLOCK_HEIGHT = 100;
5+
export const HOUR_BLOCK_HEIGHT = 100;
6+
const EVENT_BLOCK_RIGHT_MARGIN = 10;
67

78
function buildEvent(column: any, left: number, width: number, dayStart: number) {
89
const startTime = new XDate(column.start);
910
const endTime = column.end ? new XDate(column.end) : new XDate(startTime).addHours(1);
1011

1112
const dayStartTime = new XDate(startTime).clearTime();
1213

13-
column.top = (dayStartTime.diffHours(startTime) - dayStart) * HALF_HOUR_BLOCK_HEIGHT;
14-
column.height = startTime.diffHours(endTime) * HALF_HOUR_BLOCK_HEIGHT;
14+
column.top = (dayStartTime.diffHours(startTime) - dayStart) * HOUR_BLOCK_HEIGHT;
15+
column.height = startTime.diffHours(endTime) * HOUR_BLOCK_HEIGHT;
1516
column.width = width;
1617
column.left = left;
1718
return column;
@@ -46,7 +47,7 @@ function pack(columns: any, width: number, calculatedEvents: Event[], dayStart:
4647
for (let j = 0; j < col.length; j++) {
4748
const colSpan = expand(col[j], i, columns);
4849
const L = (i / colLength) * width;
49-
const W = (width * colSpan) / colLength - 10;
50+
const W = (width * colSpan) / colLength - EVENT_BLOCK_RIGHT_MARGIN;
5051

5152
calculatedEvents.push(buildEvent(col[j], L, W, dayStart));
5253
}

src/timeline/Timeline.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import map from 'lodash/map';
55

66
import {Theme} from '../types';
77
import styleConstructor from './style';
8-
import populateEvents, {HALF_HOUR_BLOCK_HEIGHT} from './Packer';
8+
import populateEvents, {HOUR_BLOCK_HEIGHT} from './Packer';
99
import TimelineHours from './TimelineHours';
1010
import EventBlock, {Event, PackedEvent} from './EventBlock';
1111

@@ -39,7 +39,7 @@ const Timeline = (props: TimelineProps) => {
3939
} = props;
4040

4141
const scrollView = useRef<ScrollView>();
42-
const calendarHeight = useRef((end - start) * HALF_HOUR_BLOCK_HEIGHT);
42+
const calendarHeight = useRef((end - start) * HOUR_BLOCK_HEIGHT);
4343
const styles = useRef(styleConstructor(theme || props.styles, calendarHeight.current));
4444

4545
const packedEvents = useMemo(() => {

src/timeline/TimelineHours.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, {useMemo} from 'react';
22
import {View, Text, ViewStyle, TextStyle, Dimensions} from 'react-native';
33
import range from 'lodash/range';
4-
import {HALF_HOUR_BLOCK_HEIGHT} from './Packer';
4+
import {HOUR_BLOCK_HEIGHT} from './Packer';
55

66
const {width: dimensionWidth} = Dimensions.get('window');
77

@@ -15,7 +15,7 @@ interface TimelineHoursProps {
1515
const TimelineHours = (props: TimelineHoursProps) => {
1616
const {format24h, start = 0, end = 24, styles} = props;
1717
// const offset = this.calendarHeight / (end - start);
18-
const offset = HALF_HOUR_BLOCK_HEIGHT;
18+
const offset = HOUR_BLOCK_HEIGHT;
1919
const EVENT_DIFF = 20;
2020

2121
const hours = useMemo(() => {
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import uut from '../Packer';
2+
3+
describe('Timeline Packer utils', () => {
4+
const events = [
5+
{
6+
id: 'event_1',
7+
start: '2017-09-06 01:30:00',
8+
end: '2017-09-06 02:30:00',
9+
title: 'Event 1'
10+
},
11+
{
12+
id: 'event_2',
13+
start: '2017-09-06 02:15:00',
14+
end: '2017-09-06 02:45:00',
15+
title: 'Event 2'
16+
},
17+
{
18+
id: 'event_3',
19+
start: '2017-09-06 01:55:00',
20+
end: '2017-09-06 03:00:00',
21+
title: 'Event 3'
22+
}
23+
];
24+
it('should sort events by start and end times', () => {
25+
const packedEvents = uut(events, 300, 0);
26+
expect(packedEvents[0].id).toBe('event_1');
27+
expect(packedEvents[1].id).toBe('event_3');
28+
expect(packedEvents[2].id).toBe('event_2');
29+
});
30+
31+
describe('should set events block size', () => {
32+
it('should set event block height based on their duration', () => {
33+
const packedEvents = uut(events, 300, 0);
34+
expect(packedEvents[0].height).toBe(100); // event 1
35+
expect(packedEvents[1].height).toBeCloseTo(108.333); // event 3
36+
expect(packedEvents[2].height).toBe(50); // event 2
37+
});
38+
39+
it('should set event block width based on overlaps of 3 events', () => {
40+
const packedEvents = uut(events, 300, 0);
41+
expect(packedEvents[0].width).toBe(90); // event 1
42+
expect(packedEvents[1].width).toBe(90); // event 3
43+
expect(packedEvents[2].width).toBe(90); // event 2
44+
});
45+
46+
it('should set event block width based on overlaps of 2 events', () => {
47+
const packedEvents = uut([events[0], events[1]], 300, 0);
48+
expect(packedEvents[0].width).toBe(140); // event 1
49+
expect(packedEvents[1].width).toBe(140); // event 2
50+
});
51+
52+
it('should set event block width when there is not overlaps', () => {
53+
const packedEvents = uut([events[0]], 300, 0);
54+
expect(packedEvents[0].width).toBe(290); // event 1
55+
});
56+
});
57+
58+
describe('should set events block position', () => {
59+
it('should set top position base on the event start time', () => {
60+
const packedEvents = uut(events, 300, 0);
61+
expect(packedEvents[0].top).toBe(150); // event 1
62+
expect(packedEvents[1].top).toBeCloseTo(191.666); // event 3
63+
expect(packedEvents[2].top).toBe(225); // event 2
64+
});
65+
66+
it('should set left position base when the 3 events overlap', () => {
67+
const packedEvents = uut(events, 300, 0);
68+
expect(packedEvents[0].left).toBe(0); // event 1
69+
expect(packedEvents[1].left).toBe(100); // event 3
70+
expect(packedEvents[2].left).toBe(200); // event 2
71+
});
72+
73+
it('should set left position base when the 2 events overlap', () => {
74+
const packedEvents = uut([events[0], events[1]], 300, 0);
75+
expect(packedEvents[0].left).toBe(0); // event 1
76+
expect(packedEvents[1].left).toBe(150); // event 3
77+
});
78+
});
79+
});

0 commit comments

Comments
 (0)