11import should from 'should' // eslint-disable-line no-unused-vars
22import DropdownComponent from './Dropdown'
33import assert from 'assert'
4+ import sinon from 'sinon'
45
56describe ( 'Text component' , function ( ) {
67 beforeEach ( function ( ) {
@@ -23,9 +24,15 @@ describe('Text component', function() {
2324
2425 const call_render_with = ( args ) => {
2526 const d3 = require ( 'd3' )
26- const render = DropdownComponent ( args . args ) ( d3 . selection ( ) )
27+ const init_variable = sinon . spy ( )
28+ const set_variable = sinon . spy ( )
29+ const component_args = Object . assign ( args . args , {
30+ 'init_variable' : init_variable ,
31+ 'set_variable' : set_variable ,
32+ } )
33+ const render = DropdownComponent ( component_args ) ( d3 . selection ( ) )
2734 render ( args . data )
28- return { render }
35+ return { render, init_variable , set_variable , d3 }
2936 }
3037
3138 it ( 'creates select item' , ( ) => {
@@ -89,5 +96,48 @@ describe('Text component', function() {
8996 assert . equal ( d3 . select ( 'option.ds--select-option' ) . node ( ) . value , - 56 )
9097 } )
9198
99+ it ( 'init_variable has to be called with default value' , ( ) => {
100+ const my_spy = sinon . spy ( )
101+ const { init_variable } = call_render_with ( { 'args' : { 'variable' : 'my_var' , 'default' : my_spy } ,
102+ 'data' : [ { 'text' : 'foo' , 'value' : 0 } ] } )
103+ init_variable . should . be . calledWith ( 'my_var' , my_spy )
104+ } )
105+
106+ it ( 'init_variable has to be called with default value 2' , ( ) => {
107+ const my_spy = sinon . spy ( )
108+ const { init_variable } = call_render_with ( { 'args' : { 'variable' : 'my_var2' , 'default' : my_spy } ,
109+ 'data' : [ { 'text' : 'foo' , 'value' : 0 } ] } )
110+ init_variable . should . be . calledWith ( 'my_var2' , my_spy )
111+ } )
112+
113+ it ( 'set_variable should not be called before change' , ( ) => {
114+ const { set_variable } = call_render_with ( { 'args' : { 'variable' : 'my_var' , 'default' : '' } ,
115+ 'data' : [ { 'text' : 'foo' , 'value' : 0 } ] } )
116+ set_variable . should . not . be . called ( )
117+ } )
118+
119+ it ( 'set_variable should be called after change' , ( ) => {
120+ const { d3, set_variable } = call_render_with ( { 'args' : { 'variable' : 'var' , 'default' : '' } ,
121+ 'data' : [
122+ { 'text' : 'foo' , 'value' : 'fo' } ,
123+ { 'text' : 'bar' , 'value' : 'bar' } ,
124+ ] } )
125+ d3 . select ( 'select' ) . property ( 'value' , 'fo' )
126+ d3 . select ( 'select' ) . dispatch ( 'change' )
127+ set_variable . should . be . calledWith ( 'var' , 'fo' )
128+ } )
129+
130+ it ( 'set_variable should be called after change 2' , ( ) => {
131+ const { d3, set_variable } = call_render_with ( { 'args' : { 'variable' : 'my_var' , 'default' : '' } ,
132+ 'data' : [
133+ { 'text' : 'foo' , 'value' : 'fo' } ,
134+ { 'text' : 'bar' , 'value' : 'baz' } ,
135+ ] } )
136+ d3 . select ( 'select' ) . property ( 'value' , 'fo' )
137+ d3 . select ( 'select' ) . property ( 'value' , 'baz' )
138+ d3 . select ( 'select' ) . dispatch ( 'change' )
139+ set_variable . should . be . calledWith ( 'my_var' , 'baz' )
140+ } )
141+
92142} )
93143
0 commit comments