@@ -7,7 +7,7 @@ var createGraphDiv = require('../assets/create_graph_div');
77var destroyGraphDiv = require ( '../assets/destroy_graph_div' ) ;
88
99
10- describe ( 'Test shapes nodes ' , function ( ) {
10+ describe ( 'Test shapes: ' , function ( ) {
1111 'use strict' ;
1212
1313 var mock = require ( '@mocks/shapes.json' ) ;
@@ -28,26 +28,74 @@ describe('Test shapes nodes', function() {
2828 return d3 . selectAll ( '.shapelayer' ) . size ( ) ;
2929 }
3030
31- function countPaths ( ) {
31+ function countShapePaths ( ) {
3232 return d3 . selectAll ( '.shapelayer > path' ) . size ( ) ;
3333 }
3434
35- it ( 'has one *shapelayer* node' , function ( ) {
36- expect ( countShapeLayers ( ) ) . toEqual ( 1 ) ;
37- } ) ;
35+ describe ( 'DOM' , function ( ) {
36+ it ( 'has one *shapelayer* node' , function ( ) {
37+ expect ( countShapeLayers ( ) ) . toEqual ( 1 ) ;
38+ } ) ;
39+
40+ it ( 'has as many *path* nodes as there are shapes' , function ( ) {
41+ expect ( countShapePaths ( ) ) . toEqual ( mock . layout . shapes . length ) ;
42+ } ) ;
3843
39- it ( 'has as many *path* nodes as there are shapes' , function ( ) {
40- expect ( countPaths ( ) ) . toEqual ( mock . layout . shapes . length ) ;
44+ it ( 'should be able to get relayout' , function ( done ) {
45+ expect ( countShapeLayers ( ) ) . toEqual ( 1 ) ;
46+ expect ( countShapePaths ( ) ) . toEqual ( mock . layout . shapes . length ) ;
47+
48+ Plotly . relayout ( gd , { height : 200 , width : 400 } ) . then ( function ( ) {
49+ expect ( countShapeLayers ( ) ) . toEqual ( 1 ) ;
50+ expect ( countShapePaths ( ) ) . toEqual ( mock . layout . shapes . length ) ;
51+ } ) . then ( done ) ;
52+ } ) ;
4153 } ) ;
4254
43- it ( 'should be able to get relayout' , function ( done ) {
44- expect ( countShapeLayers ( ) ) . toEqual ( 1 ) ;
45- expect ( countPaths ( ) ) . toEqual ( mock . layout . shapes . length ) ;
55+ function countShapes ( gd ) {
56+ return gd . layout . shapes ?
57+ gd . layout . shapes . length :
58+ 0 ;
59+ }
60+
61+ function getLastShape ( gd ) {
62+ return gd . layout . shapes ?
63+ gd . layout . shapes [ gd . layout . shapes . length - 1 ] :
64+ null ;
65+ }
4666
47- Plotly . relayout ( gd , { height : 200 , width : 400 } ) . then ( function ( ) {
48- expect ( countShapeLayers ( ) ) . toEqual ( 1 ) ;
49- expect ( countPaths ( ) ) . toEqual ( mock . layout . shapes . length ) ;
50- done ( ) ;
67+ function getRandomShape ( ) {
68+ return {
69+ x0 : Math . random ( ) ,
70+ y0 : Math . random ( ) ,
71+ x1 : Math . random ( ) ,
72+ y1 : Math . random ( )
73+ } ;
74+ }
75+
76+ describe ( 'Plotly.relayout' , function ( ) {
77+ it ( 'should be able to add a shape' , function ( done ) {
78+ var index = countShapes ( gd ) ;
79+ var shape = getRandomShape ( ) ;
80+
81+ Plotly . relayout ( gd , 'shapes[' + index + ']' , shape ) . then ( function ( ) {
82+ expect ( getLastShape ( gd ) ) . toEqual ( shape ) ;
83+ expect ( countShapes ( gd ) ) . toEqual ( index + 1 ) ;
84+ } ) . then ( done ) ;
85+ } ) ;
86+
87+ it ( 'should be able to remove a shape' , function ( done ) {
88+ var index = countShapes ( gd ) ;
89+ var shape = getRandomShape ( ) ;
90+
91+ Plotly . relayout ( gd , 'shapes[' + index + ']' , shape ) . then ( function ( ) {
92+ expect ( getLastShape ( gd ) ) . toEqual ( shape ) ;
93+ expect ( countShapes ( gd ) ) . toEqual ( index + 1 ) ;
94+ } ) . then ( function ( ) {
95+ Plotly . relayout ( gd , 'shapes[' + index + ']' , 'remove' ) ;
96+ } ) . then ( function ( ) {
97+ expect ( countShapes ( gd ) ) . toEqual ( index ) ;
98+ } ) . then ( done ) ;
5199 } ) ;
52100 } ) ;
53101} ) ;
0 commit comments