@@ -2,15 +2,7 @@ var Plotly = require('@lib/index');
22
33var createGraphDiv = require ( '../assets/create_graph_div' ) ;
44var 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' ) ;
146
157describe ( 'Test frame api' , function ( ) {
168 'use strict' ;
@@ -34,33 +26,39 @@ describe('Test frame api', function() {
3426 } ) ;
3527
3628 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 ( ) {
4129 expect ( gd . _frameData . _counter ) . toEqual ( 0 ) ;
4230 } ) ;
4331 } ) ;
4432
45- describe ( 'addFrames' , function ( ) {
33+ describe ( '# addFrames' , function ( ) {
4634 it ( 'names an unnamed frame' , function ( done ) {
4735 Plotly . addFrames ( gd , [ { } ] ) . then ( function ( ) {
4836 expect ( Object . keys ( h ) ) . toEqual ( [ 'frame 0' ] ) ;
49- } ) . then ( done , fail ( done ) ) ;
37+ } ) . catch ( fail ) . then ( done ) ;
5038 } ) ;
5139
5240 it ( 'creates multiple unnamed frames at the same time' , function ( done ) {
5341 Plotly . addFrames ( gd , [ { } , { } ] ) . then ( function ( ) {
5442 expect ( f ) . toEqual ( [ { name : 'frame 0' } , { name : 'frame 1' } ] ) ;
55- } ) . then ( done , fail ( done ) ) ;
43+ } ) . catch ( fail ) . then ( done ) ;
5644 } ) ;
5745
5846 it ( 'creates multiple unnamed frames in series' , function ( done ) {
5947 Plotly . addFrames ( gd , [ { } ] ) . then (
6048 Plotly . addFrames ( gd , [ { } ] )
6149 ) . then ( function ( ) {
6250 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 ) ;
6462 } ) ;
6563
6664 it ( 'inserts frames at specific indices' , function ( done ) {
@@ -70,19 +68,21 @@ describe('Test frame api', function() {
7068 frames . push ( { name : 'frame' + i } ) ;
7169 }
7270
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 ( ) {
7478 return Plotly . addFrames ( gd , [ { name : 'inserted1' } , { name : 'inserted2' } , { name : 'inserted3' } ] , [ 5 , 7 , undefined ] ) ;
7579 } ) . then ( function ( ) {
7680 expect ( f [ 5 ] ) . toEqual ( { name : 'inserted1' } ) ;
7781 expect ( f [ 7 ] ) . toEqual ( { name : 'inserted2' } ) ;
7882 expect ( f [ 12 ] ) . toEqual ( { name : 'inserted3' } ) ;
7983
8084 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 ) ;
8686 } ) ;
8787
8888 it ( 'inserts frames at specific indices (reversed)' , function ( done ) {
@@ -92,36 +92,37 @@ describe('Test frame api', function() {
9292 frames . push ( { name : 'frame' + i } ) ;
9393 }
9494
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 ( ) {
96102 return Plotly . addFrames ( gd , [ { name : 'inserted3' } , { name : 'inserted2' } , { name : 'inserted1' } ] , [ undefined , 7 , 5 ] ) ;
97103 } ) . then ( function ( ) {
98104 expect ( f [ 5 ] ) . toEqual ( { name : 'inserted1' } ) ;
99105 expect ( f [ 7 ] ) . toEqual ( { name : 'inserted2' } ) ;
100106 expect ( f [ 12 ] ) . toEqual ( { name : 'inserted3' } ) ;
101107
102108 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 ) ;
108110 } ) ;
109111
110112 it ( 'implements undo/redo' , function ( done ) {
111- Plotly . addFrames ( gd , [ { name : 'frame 0' } , { name : 'frame 1' } ] ) . then ( function ( ) {
113+ function validate ( ) {
112114 expect ( f ) . toEqual ( [ { name : 'frame 0' } , { name : 'frame 1' } ] ) ;
113115 expect ( h ) . toEqual ( { 'frame 0' : { name : 'frame 0' } , 'frame 1' : { name : 'frame 1' } } ) ;
116+ }
114117
118+ Plotly . addFrames ( gd , [ { name : 'frame 0' } , { name : 'frame 1' } ] ) . then ( validate ) . then ( function ( ) {
115119 return Plotly . Queue . undo ( gd ) ;
116120 } ) . then ( function ( ) {
117121 expect ( f ) . toEqual ( [ ] ) ;
118122 expect ( h ) . toEqual ( { } ) ;
119123
120124 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 ) ;
125126 } ) ;
126127
127128 it ( 'overwrites frames' , function ( done ) {
@@ -144,11 +145,11 @@ describe('Test frame api', function() {
144145 } ) . then ( function ( ) {
145146 expect ( f ) . toEqual ( [ { name : 'test1' } , { name : 'test2' } , { name : 'test3' } ] ) ;
146147 expect ( Object . keys ( h ) ) . toEqual ( [ 'test1' , 'test2' , 'test3' ] ) ;
147- } ) . then ( done , fail ( done ) ) ;
148+ } ) . catch ( fail ) . then ( done ) ;
148149 } ) ;
149150 } ) ;
150151
151- describe ( 'deleteFrames' , function ( ) {
152+ describe ( '# deleteFrames' , function ( ) {
152153 it ( 'deletes a frame' , function ( done ) {
153154 Plotly . addFrames ( gd , [ { name : 'frame1' } ] ) . then ( function ( ) {
154155 expect ( f ) . toEqual ( [ { name : 'frame1' } ] ) ;
@@ -167,7 +168,7 @@ describe('Test frame api', function() {
167168 } ) . then ( function ( ) {
168169 expect ( f ) . toEqual ( [ ] ) ;
169170 expect ( Object . keys ( h ) ) . toEqual ( [ ] ) ;
170- } ) . then ( done , fail ( done ) ) ;
171+ } ) . catch ( fail ) . then ( done ) ;
171172 } ) ;
172173
173174 it ( 'deletes multiple frames' , function ( done ) {
@@ -177,29 +178,25 @@ describe('Test frame api', function() {
177178 frames . push ( { name : 'frame' + i } ) ;
178179 }
179180
180- Plotly . addFrames ( gd , frames ) . then ( function ( ) {
181- return Plotly . deleteFrames ( gd , [ 2 , 8 , 4 , 6 ] ) ;
182- } ) . then ( function ( ) {
181+ function validate ( ) {
183182 var expected = [ 'frame0' , 'frame1' , 'frame3' , 'frame5' , 'frame7' , 'frame9' ] ;
184183 expect ( f . length ) . toEqual ( expected . length ) ;
185184 for ( i = 0 ; i < expected . length ; i ++ ) {
186185 expect ( f [ i ] . name ) . toEqual ( expected [ i ] ) ;
187186 }
187+ }
188188
189+ Plotly . addFrames ( gd , frames ) . then ( function ( ) {
190+ return Plotly . deleteFrames ( gd , [ 2 , 8 , 4 , 6 ] ) ;
191+ } ) . then ( validate ) . then ( function ( ) {
189192 return Plotly . Queue . undo ( gd ) ;
190193 } ) . then ( function ( ) {
191194 for ( i = 0 ; i < 10 ; i ++ ) {
192195 expect ( f [ i ] ) . toEqual ( { name : 'frame' + i } ) ;
193196 }
194197
195198 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 ) ;
203200 } ) ;
204201 } ) ;
205202} ) ;
0 commit comments