@@ -2,15 +2,7 @@ var Plotly = require('@lib/index');
2
2
3
3
var createGraphDiv = require ( '../assets/create_graph_div' ) ;
4
4
var destroyGraphDiv = require ( '../assets/destroy_graph_div' ) ;
5
-
6
- // A helper function so that failed tests don't simply stall:
7
- function fail ( done ) {
8
- return function ( err ) {
9
- console . error ( err . toString ( ) ) ;
10
- expect ( err . toString ( ) ) . toBe ( true ) ;
11
- done ( ) ;
12
- } ;
13
- }
5
+ var fail = require ( '../assets/fail_test' ) ;
14
6
15
7
describe ( 'Test frame api' , function ( ) {
16
8
'use strict' ;
@@ -34,33 +26,39 @@ describe('Test frame api', function() {
34
26
} ) ;
35
27
36
28
it ( 'creates an empty lookup table for frames' , function ( ) {
37
- expect ( gd . _frameData . _frameHash ) . toEqual ( { } ) ;
38
- } ) ;
39
-
40
- it ( 'initializes a name counter to zero' , function ( ) {
41
29
expect ( gd . _frameData . _counter ) . toEqual ( 0 ) ;
42
30
} ) ;
43
31
} ) ;
44
32
45
- describe ( 'addFrames' , function ( ) {
33
+ describe ( '# addFrames' , function ( ) {
46
34
it ( 'names an unnamed frame' , function ( done ) {
47
35
Plotly . addFrames ( gd , [ { } ] ) . then ( function ( ) {
48
36
expect ( Object . keys ( h ) ) . toEqual ( [ 'frame 0' ] ) ;
49
- } ) . then ( done , fail ( done ) ) ;
37
+ } ) . catch ( fail ) . then ( done ) ;
50
38
} ) ;
51
39
52
40
it ( 'creates multiple unnamed frames at the same time' , function ( done ) {
53
41
Plotly . addFrames ( gd , [ { } , { } ] ) . then ( function ( ) {
54
42
expect ( f ) . toEqual ( [ { name : 'frame 0' } , { name : 'frame 1' } ] ) ;
55
- } ) . then ( done , fail ( done ) ) ;
43
+ } ) . catch ( fail ) . then ( done ) ;
56
44
} ) ;
57
45
58
46
it ( 'creates multiple unnamed frames in series' , function ( done ) {
59
47
Plotly . addFrames ( gd , [ { } ] ) . then (
60
48
Plotly . addFrames ( gd , [ { } ] )
61
49
) . then ( function ( ) {
62
50
expect ( f ) . toEqual ( [ { name : 'frame 0' } , { name : 'frame 1' } ] ) ;
63
- } ) . then ( done , fail ( done ) ) ;
51
+ } ) . catch ( fail ) . then ( done ) ;
52
+ } ) ;
53
+
54
+ it ( 'avoids name collisions' , function ( done ) {
55
+ Plotly . addFrames ( gd , [ { name : 'frame 0' } , { name : 'frame 2' } ] ) . then ( function ( ) {
56
+ expect ( f ) . toEqual ( [ { name : 'frame 0' } , { name : 'frame 2' } ] ) ;
57
+
58
+ return Plotly . addFrames ( gd , [ { } , { name : 'foobar' } , { } ] ) ;
59
+ } ) . then ( function ( ) {
60
+ expect ( f ) . toEqual ( [ { name : 'frame 0' } , { name : 'frame 2' } , { name : 'frame 1' } , { name : 'foobar' } , { name : 'frame 3' } ] ) ;
61
+ } ) . catch ( fail ) . then ( done ) ;
64
62
} ) ;
65
63
66
64
it ( 'inserts frames at specific indices' , function ( done ) {
@@ -70,19 +68,21 @@ describe('Test frame api', function() {
70
68
frames . push ( { name : 'frame' + i } ) ;
71
69
}
72
70
73
- Plotly . addFrames ( gd , frames ) . then ( function ( ) {
71
+ function validate ( ) {
72
+ for ( i = 0 ; i < 10 ; i ++ ) {
73
+ expect ( f [ i ] ) . toEqual ( { name : 'frame' + i } ) ;
74
+ }
75
+ }
76
+
77
+ Plotly . addFrames ( gd , frames ) . then ( validate ) . then ( function ( ) {
74
78
return Plotly . addFrames ( gd , [ { name : 'inserted1' } , { name : 'inserted2' } , { name : 'inserted3' } ] , [ 5 , 7 , undefined ] ) ;
75
79
} ) . then ( function ( ) {
76
80
expect ( f [ 5 ] ) . toEqual ( { name : 'inserted1' } ) ;
77
81
expect ( f [ 7 ] ) . toEqual ( { name : 'inserted2' } ) ;
78
82
expect ( f [ 12 ] ) . toEqual ( { name : 'inserted3' } ) ;
79
83
80
84
return Plotly . Queue . undo ( gd ) ;
81
- } ) . then ( function ( ) {
82
- for ( i = 0 ; i < 10 ; i ++ ) {
83
- expect ( f [ i ] ) . toEqual ( { name : 'frame' + i } ) ;
84
- }
85
- } ) . then ( done , fail ( done ) ) ;
85
+ } ) . then ( validate ) . catch ( fail ) . then ( done ) ;
86
86
} ) ;
87
87
88
88
it ( 'inserts frames at specific indices (reversed)' , function ( done ) {
@@ -92,36 +92,37 @@ describe('Test frame api', function() {
92
92
frames . push ( { name : 'frame' + i } ) ;
93
93
}
94
94
95
- Plotly . addFrames ( gd , frames ) . then ( function ( ) {
95
+ function validate ( ) {
96
+ for ( i = 0 ; i < 10 ; i ++ ) {
97
+ expect ( f [ i ] ) . toEqual ( { name : 'frame' + i } ) ;
98
+ }
99
+ }
100
+
101
+ Plotly . addFrames ( gd , frames ) . then ( validate ) . then ( function ( ) {
96
102
return Plotly . addFrames ( gd , [ { name : 'inserted3' } , { name : 'inserted2' } , { name : 'inserted1' } ] , [ undefined , 7 , 5 ] ) ;
97
103
} ) . then ( function ( ) {
98
104
expect ( f [ 5 ] ) . toEqual ( { name : 'inserted1' } ) ;
99
105
expect ( f [ 7 ] ) . toEqual ( { name : 'inserted2' } ) ;
100
106
expect ( f [ 12 ] ) . toEqual ( { name : 'inserted3' } ) ;
101
107
102
108
return Plotly . Queue . undo ( gd ) ;
103
- } ) . then ( function ( ) {
104
- for ( i = 0 ; i < 10 ; i ++ ) {
105
- expect ( f [ i ] ) . toEqual ( { name : 'frame' + i } ) ;
106
- }
107
- } ) . then ( done , fail ( done ) ) ;
109
+ } ) . then ( validate ) . catch ( fail ) . then ( done ) ;
108
110
} ) ;
109
111
110
112
it ( 'implements undo/redo' , function ( done ) {
111
- Plotly . addFrames ( gd , [ { name : 'frame 0' } , { name : 'frame 1' } ] ) . then ( function ( ) {
113
+ function validate ( ) {
112
114
expect ( f ) . toEqual ( [ { name : 'frame 0' } , { name : 'frame 1' } ] ) ;
113
115
expect ( h ) . toEqual ( { 'frame 0' : { name : 'frame 0' } , 'frame 1' : { name : 'frame 1' } } ) ;
116
+ }
114
117
118
+ Plotly . addFrames ( gd , [ { name : 'frame 0' } , { name : 'frame 1' } ] ) . then ( validate ) . then ( function ( ) {
115
119
return Plotly . Queue . undo ( gd ) ;
116
120
} ) . then ( function ( ) {
117
121
expect ( f ) . toEqual ( [ ] ) ;
118
122
expect ( h ) . toEqual ( { } ) ;
119
123
120
124
return Plotly . Queue . redo ( gd ) ;
121
- } ) . then ( function ( ) {
122
- expect ( f ) . toEqual ( [ { name : 'frame 0' } , { name : 'frame 1' } ] ) ;
123
- expect ( h ) . toEqual ( { 'frame 0' : { name : 'frame 0' } , 'frame 1' : { name : 'frame 1' } } ) ;
124
- } ) . then ( done , fail ( done ) ) ;
125
+ } ) . then ( validate ) . catch ( fail ) . then ( done ) ;
125
126
} ) ;
126
127
127
128
it ( 'overwrites frames' , function ( done ) {
@@ -144,11 +145,11 @@ describe('Test frame api', function() {
144
145
} ) . then ( function ( ) {
145
146
expect ( f ) . toEqual ( [ { name : 'test1' } , { name : 'test2' } , { name : 'test3' } ] ) ;
146
147
expect ( Object . keys ( h ) ) . toEqual ( [ 'test1' , 'test2' , 'test3' ] ) ;
147
- } ) . then ( done , fail ( done ) ) ;
148
+ } ) . catch ( fail ) . then ( done ) ;
148
149
} ) ;
149
150
} ) ;
150
151
151
- describe ( 'deleteFrames' , function ( ) {
152
+ describe ( '# deleteFrames' , function ( ) {
152
153
it ( 'deletes a frame' , function ( done ) {
153
154
Plotly . addFrames ( gd , [ { name : 'frame1' } ] ) . then ( function ( ) {
154
155
expect ( f ) . toEqual ( [ { name : 'frame1' } ] ) ;
@@ -167,7 +168,7 @@ describe('Test frame api', function() {
167
168
} ) . then ( function ( ) {
168
169
expect ( f ) . toEqual ( [ ] ) ;
169
170
expect ( Object . keys ( h ) ) . toEqual ( [ ] ) ;
170
- } ) . then ( done , fail ( done ) ) ;
171
+ } ) . catch ( fail ) . then ( done ) ;
171
172
} ) ;
172
173
173
174
it ( 'deletes multiple frames' , function ( done ) {
@@ -177,29 +178,25 @@ describe('Test frame api', function() {
177
178
frames . push ( { name : 'frame' + i } ) ;
178
179
}
179
180
180
- Plotly . addFrames ( gd , frames ) . then ( function ( ) {
181
- return Plotly . deleteFrames ( gd , [ 2 , 8 , 4 , 6 ] ) ;
182
- } ) . then ( function ( ) {
181
+ function validate ( ) {
183
182
var expected = [ 'frame0' , 'frame1' , 'frame3' , 'frame5' , 'frame7' , 'frame9' ] ;
184
183
expect ( f . length ) . toEqual ( expected . length ) ;
185
184
for ( i = 0 ; i < expected . length ; i ++ ) {
186
185
expect ( f [ i ] . name ) . toEqual ( expected [ i ] ) ;
187
186
}
187
+ }
188
188
189
+ Plotly . addFrames ( gd , frames ) . then ( function ( ) {
190
+ return Plotly . deleteFrames ( gd , [ 2 , 8 , 4 , 6 ] ) ;
191
+ } ) . then ( validate ) . then ( function ( ) {
189
192
return Plotly . Queue . undo ( gd ) ;
190
193
} ) . then ( function ( ) {
191
194
for ( i = 0 ; i < 10 ; i ++ ) {
192
195
expect ( f [ i ] ) . toEqual ( { name : 'frame' + i } ) ;
193
196
}
194
197
195
198
return Plotly . Queue . redo ( gd ) ;
196
- } ) . then ( function ( ) {
197
- var expected = [ 'frame0' , 'frame1' , 'frame3' , 'frame5' , 'frame7' , 'frame9' ] ;
198
- expect ( f . length ) . toEqual ( expected . length ) ;
199
- for ( i = 0 ; i < expected . length ; i ++ ) {
200
- expect ( f [ i ] . name ) . toEqual ( expected [ i ] ) ;
201
- }
202
- } ) . then ( done , fail ( done ) ) ;
199
+ } ) . then ( validate ) . catch ( fail ) . then ( done ) ;
203
200
} ) ;
204
201
} ) ;
205
202
} ) ;
0 commit comments