forked from namespace-ee/react-calendar-timeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
238 lines (211 loc) · 6.58 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import React from 'react'
import { mount } from 'enzyme'
import Timeline from 'lib/Timeline'
import moment from 'moment'
const groups = [
{ id: 2, title: 'group 2' },
{ id: 1, title: 'group 1' },
{ id: 3, title: 'group 3' }
]
const items = [
{
id: 1,
group: 1,
title: 'item 1',
start_time: moment('1995-12-25'),
end_time: moment('1995-12-25').add(1, 'hour')
},
{
id: 2,
group: 2,
title: 'item 2',
start_time: moment('1995-12-25').add(-0.5, 'hour'),
end_time: moment('1995-12-25').add(0.5, 'hour')
},
{
id: 3,
group: 3,
title: 'item 3',
start_time: moment('1995-12-25').add(2, 'hour'),
end_time: moment('1995-12-25').add(3, 'hour')
}
]
xdescribe('Timeline', () => {
it('shows grouping no matter of the group order', () => {
const wrapper = mount(
<Timeline
groups={groups}
items={items}
defaultTimeStart={moment('1995-12-25').add(-12, 'hour')}
defaultTimeEnd={moment('1995-12-25').add(12, 'hour')}
/>
)
// get the items parent
const itemsRendered = wrapper.find('.rct-items')
// array will hold the title and top-position for each item
var itemsOrder = []
// read for every item the title and the top-value and push it to itemsOrder[]
itemsRendered.props().children.forEach(itemRendered =>
itemsOrder.push({
title: itemRendered.props.item.title,
top: itemRendered.props.dimensions.top
})
)
// order the array by top-attribute
itemsOrder = itemsOrder.sort((a, b) => a.top - b.top)
expect(itemsOrder[0].title).toBe('item 2')
expect(itemsOrder[1].title).toBe('item 1')
expect(itemsOrder[2].title).toBe('item 3')
})
it('assigns top dimension to all items', () => {
const wrapper = mount(
<Timeline
groups={groups}
items={items}
defaultTimeStart={moment('1995-12-25').add(-12, 'hour')}
defaultTimeEnd={moment('1995-12-25').add(12, 'hour')}
/>
)
// get the items parent
const itemsRendered = wrapper.find('.rct-items')
itemsRendered.props().children.forEach(item => {
expect(item.props.dimensions.top).not.toBeNull()
})
})
it('renders component with empty groups', () => {
let allCorrect = true
try {
mount(
<Timeline
groups={[]}
items={items}
defaultTimeStart={moment('1995-12-25').add(-12, 'hour')}
defaultTimeEnd={moment('1995-12-25').add(12, 'hour')}
/>
)
} catch (err) {
allCorrect = false
}
expect(allCorrect).toBe(true)
})
it('renders items without corresponding group', () => {
let itemsNoValidGroup = [
{
start_time: moment('1995-12-25').add(-2, 'hour'),
end_time: moment('1995-12-25').add(2, 'hour'),
group: -1, // this ID is not found in groups!
id: 1,
title: 'Title'
}
]
let allCorrect = true
try {
mount(
<Timeline
groups={groups}
items={itemsNoValidGroup}
defaultTimeStart={moment('1995-12-25').add(-12, 'hour')}
defaultTimeEnd={moment('1995-12-25').add(12, 'hour')}
/>
)
} catch (err) {
allCorrect = false
}
expect(allCorrect).toBe(true)
})
it('passes correct props to plugins', () => {
let Plugin = () => <div className="test-plugin" />
const wrapper = mount(
<Timeline
groups={[]}
items={items}
stackItems
defaultTimeStart={moment('1995-12-25').add(-12, 'hour')}
defaultTimeEnd={moment('1995-12-25').add(12, 'hour')}
>
<Plugin />
</Timeline>
)
const pluginRendered = wrapper.find('Plugin')
const pluginProps = pluginRendered.props()
expect(typeof pluginProps.canvasTimeStart).toBe('number')
expect(typeof pluginProps.canvasTimeEnd).toBe('number')
expect(typeof pluginProps.canvasWidth).toBe('number')
expect(typeof pluginProps.visibleTimeStart).toBe('number')
expect(typeof pluginProps.visibleTimeEnd).toBe('number')
expect(typeof pluginProps.height).toBe('number')
expect(typeof pluginProps.minUnit).toBe('string')
expect(typeof pluginProps.dimensionItems).toBe('object')
expect(typeof pluginProps.items).toBe('object')
expect(typeof pluginProps.groups).toBe('object')
expect(typeof pluginProps.keys).toBe('object')
expect(typeof pluginProps.timeSteps).toBe('object')
expect(pluginProps.selected).toBeInstanceOf(Array)
expect(pluginProps.groupHeights).toBeInstanceOf(Array)
expect(pluginProps.groupTops).toBeInstanceOf(Array)
})
it('should render items', () => {
const props = {
items: items,
groups: groups,
defaultTimeStart: moment('1995-12-25').add(-12, 'hour'),
defaultTimeEnd : moment('1995-12-25').add(12, 'hour'),
}
const wrapper = mount(<Timeline {...props} />)
expect(wrapper.find('div.rct-item').length).toEqual(3)
})
it('should render custom elements using itemRenderer with title', () => {
const props = {
items: items,
groups: groups,
defaultTimeStart: moment('1995-12-25').add(-12, 'hour'),
defaultTimeEnd : moment('1995-12-25').add(12, 'hour'),
itemRenderer: ({
item,
timelineContext,
itemContext,
getItemProps,
getResizeProps
}) => {
const {
left: leftResizeProps,
right: rightResizeProps
} = getResizeProps()
return <h1 {...getItemProps(item.itemProps)}>{itemContext.title}</h1>
}
}
const wrapper = mount(<Timeline {...props} />)
const wrapperItems = wrapper.find('h1.rct-item')
expect(wrapperItems.length).toEqual(3)
wrapperItems.forEach((item, index) => {
expect(item.text()).toEqual(items[index].title)
})
})
it('should render custom elements using itemRenderer with title', () => {
const props = {
items: items,
groups: groups,
defaultTimeStart: moment('1995-12-25').add(-12, 'hour'),
defaultTimeEnd : moment('1995-12-25').add(12, 'hour'),
itemRenderer: ({
item,
timelineContext,
itemContext,
getItemProps,
getResizeProps
}) => {
const {
left: leftResizeProps,
right: rightResizeProps
} = getResizeProps()
return <h1 {...getItemProps(item.itemProps)}>{itemContext.title}</h1>
}
}
const wrapper = mount(<Timeline {...props} />)
const wrapperItems = wrapper.find('h1.rct-item')
expect(wrapperItems.length).toEqual(3)
wrapperItems.forEach((item, index) => {
expect(item.text()).toEqual(items[index].title)
})
})
})