This repository has been archived by the owner on Jul 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
d3-selection-multi-test.ts
198 lines (162 loc) · 4.26 KB
/
d3-selection-multi-test.ts
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
/**
* Typescript definition tests for d3/d3-selection-multi module
*
* Note: These tests are intended to test the definitions only
* in the sense of typing and call signature consistency. They
* are not intended as functional tests.
*/
import { Selection, ArrayLike } from '../../src/d3-selection';
import { Transition } from '../../src/d3-transition';
import * as d3SelectionMulti from '../../src/d3-selection-multi';
let selection: Selection<HTMLAnchorElement, string, null, undefined>;
// Selection.attrs
// Simple object
selection = selection.attrs({
foo: 1,
bar: '2',
baz: true,
});
// Function values
selection = selection.attrs({
foo: () => 1,
bar: (d) => d,
baz: (d, i) => i !== 0,
bat: function () {
return this.href;
},
});
// Function that returns a map
selection = selection.attrs(function (d) {
return this.id ? null : { id: d };
});
// Selection.styles
// Each test is repeated twice: once without the priority, and another time with
// Simple object
selection = selection.styles({
top: 0,
color: 'red',
});
selection = selection.styles({
top: 0,
color: 'red',
}, 'important');
// Function values
selection = selection.styles({
top: (d, i) => i + 'px',
color: d => d,
});
selection = selection.styles({
top: (d, i) => i + 'px',
color: d => d,
}, 'important');
// Functions that return a map
selection.styles(function (d) {
return this.id ? { color: 'red' } : { color: d };
});
selection = selection.styles(function (d) {
return this.id ? { color: 'red' } : { color: d };
}, 'important');
// Selection.properties
// Simple object
selection = selection.properties({
foo: 1,
bar: 'bar',
});
// Function values
selection = selection.properties({
foo: (d, i) => i,
bar: d => d,
});
// Function that returns an object
selection = selection.properties(function (d) {
return this.href ? null : { href: d };
});
let transition: Transition<HTMLAnchorElement, string, null, undefined>;
// Transition.attrs
// Simple object
transition = transition.attrs({
foo: 1,
bar: '2',
baz: true,
});
// Function values
transition = transition.attrs({
foo: () => 1,
bar: (d) => d,
baz: (d, i) => i !== 0,
bat: function () {
return this.href;
},
});
// Function that returns a map
transition = transition.attrs(function (d) {
return this.id ? null : { id: d };
});
// Transition.styles
// As above, the tests are repeated with the priority 'important' passed in
// Simple object
transition = transition.styles({
top: 0,
color: 'red',
});
transition = transition.styles({
top: 0,
color: 'red',
}, 'important');
// Function values
transition = transition.styles({
top: (d, i) => i + 'px',
color: d => d,
});
transition = transition.styles({
top: (d, i) => i + 'px',
color: d => d,
}, 'important');
// Function that returns a map
transition = transition.styles(function (d) {
return this.id ? { color: 'red' } : { color: d };
});
transition = transition.styles(function (d) {
return this.id ? { color: 'red' } : { color: d };
}, 'important');
// Test ValueMap interface ----------------------------------------------
interface SampleDatum {
name: string;
r: number;
filled: boolean;
}
let valueMap: d3SelectionMulti.ValueMap<SVGCircleElement, SampleDatum>;
valueMap = {
foo1: 'test',
foo2: 2,
foo3: true,
foo4: null,
bar1: function (d) {
let that: SVGCircleElement = this;
let datum: SampleDatum = d;
return d.name;
},
bar2: function (d, i) {
let that: SVGCircleElement = this;
let datum: SampleDatum = d;
let index: number = i;
return d.r;
},
bar3: function (d, i, g) {
let that: SVGCircleElement = this;
let datum: SampleDatum = d;
let index: number = i;
let group: Array<SVGCircleElement> | ArrayLike<SVGCircleElement> = g;
return d.filled;
}
};
// valueMap = { // fails, as an array is not a permissible value type
// foo1: [1, 2]
// };
// valueMap = { // fails, as a ValueFunction returning an array is not a permissible value type
// foo1: function (d) {
// let that: SVGCircleElement = this;
// let datum: SampleDatum = d;
// return [1, 2];
// }
// };