-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAnimatedDataset.test.js
More file actions
158 lines (131 loc) · 3.92 KB
/
AnimatedDataset.test.js
File metadata and controls
158 lines (131 loc) · 3.92 KB
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
import React from 'react'
import { mount } from 'enzyme'
import { AnimatedDataset } from '../src/AnimatedDataset'
console.log('React.version', React.version)
afterEach(() => {
jest.clearAllMocks()
})
describe(AnimatedDataset, () => {
const x = jest.fn(p => p.x)
const y = jest.fn(p => p.y)
const dataset = [
{ x: 0, y: 0 },
{ x: 1, y: 10 },
]
const attrs = { x, y }
it('should append element with animation disabled', () => {
const wrapper = mount(
<svg>
<AnimatedDataset
tag="rect"
dataset={dataset}
attrs={attrs}
keyFn={p => p.x}
disableAnimation
/>
</svg>
)
expect(x).toBeCalledTimes(4)
expect(y).toBeCalledTimes(4)
expect(wrapper.find('g').html()).toMatchInlineSnapshot(
`"<g><rect x=\\"0\\" y=\\"0\\"></rect><rect x=\\"1\\" y=\\"10\\"></rect></g>"`
)
})
it('should append element with animation enabled', () => {
const spyRaf = jest.spyOn(window, 'requestAnimationFrame').mockImplementation(cb => cb())
const wrapper = mount(
<svg>
<AnimatedDataset tag="rect" dataset={dataset} attrs={attrs} keyFn={p => p.x} />
</svg>
)
expect(spyRaf).toBeCalledTimes(1)
expect(x).toBeCalledTimes(4)
expect(y).toBeCalledTimes(4)
expect(wrapper.find('g').html()).toMatchInlineSnapshot(
`"<g><rect x=\\"0\\" y=\\"0\\"></rect><rect x=\\"1\\" y=\\"10\\"></rect></g>"`
)
})
it('should render empty <g> with empty dataset', () => {
const wrapper = mount(
<svg>
<AnimatedDataset dataset={[]} attrs={attrs} keyFn={p => p.x} />
</svg>
)
expect(x).toBeCalledTimes(0)
expect(y).toBeCalledTimes(0)
expect(wrapper.find('g').html()).toMatchInlineSnapshot(`"<g></g>"`)
})
it('should add text attr as child only', () => {
const wrapper = mount(
<svg>
<AnimatedDataset
dataset={['Hello World']}
tag="text"
attrs={{ text: t => t }}
keyFn={t => t}
/>
</svg>
)
expect(wrapper.find('g').html()).toMatchInlineSnapshot(`"<g><text>Hello World</text></g>"`)
})
it('should accept camel case attribute name', () => {
const wrapper = mount(
<svg>
<AnimatedDataset dataset={dataset} attrs={{ strokeWidth: 10 }} key={p => p.x} />
</svg>
)
expect(wrapper.find('g').html()).toMatchInlineSnapshot(
`"<g><rect stroke-width=\\"10\\"></rect><rect stroke-width=\\"10\\"></rect></g>"`
)
})
it('should accept events', () => {
const onClick = jest.fn()
const wrapper = mount(
<svg>
<AnimatedDataset
tag="rect"
dataset={dataset}
attrs={attrs}
key={p => p.x}
events={{ onClick }}
disableAnimation
/>
</svg>
)
expect(onClick).toBeCalledTimes(0)
const index = 1
const firstCircle = wrapper
.find('g')
.getDOMNode()
.getElementsByTagName('rect')
.item(index)
dispatch(firstCircle, new Event('click'))
expect(onClick).toBeCalledTimes(1)
const callArguments = onClick.mock.calls[0]
expect(callArguments[0]).toEqual(dataset[index])
expect(callArguments[1]).toEqual(index)
})
it('should allow nested nodes', () => {
const wrapper = mount(
<svg>
<AnimatedDataset
dataset={['Hello World', 'Goodbye World']}
tag="a"
attrs={{ href: "#test" }}
keyFn={t => t}
disableAnimation
>
<AnimatedDataset
tag="text"
attrs={{ text: t => t }}
keyFn={t => t}
disableAnimation
/>
</AnimatedDataset>
</svg>
)
expect(wrapper.find('g').first().html()).toMatchInlineSnapshot(
`"<g><a data-key=\\"Hello World\\" href=\\"#test\\"><g><text>Hello World</text></g></a><a data-key=\\"Goodbye World\\" href=\\"#test\\"><g><text>Goodbye World</text></g></a></g>"`
)
})
})