11import test from 'ava'
22import React from 'react'
3- import { spy , stub } from 'sinon'
3+ import { spy , stub , assert } from 'sinon'
44import { mount } from 'enzyme'
55import DropdownTreeSelect from './index'
66
@@ -164,6 +164,7 @@ test('should set current focus as selected on tab out for simpleSelect', t => {
164164
165165test ( 'should scroll on keyboard navigation' , t => {
166166 const largeTree = [ ...Array ( 150 ) . keys ( ) ] . map ( i => node ( `id${ i } ` , `label${ i } ` ) )
167+ const scrollIntoView = ( Element . prototype . scrollIntoView = spy ( ) )
167168 const wrapper = mount ( < DropdownTreeSelect data = { largeTree } showDropdown = "initial" /> )
168169 const getElementById = stub ( document , 'getElementById' )
169170 const contentNode = wrapper . find ( '.dropdown-content' ) . getDOMNode ( )
@@ -172,25 +173,26 @@ test('should scroll on keyboard navigation', t => {
172173
173174 triggerOnKeyboardKeyDown ( wrapper , [ 'ArrowUp' ] )
174175 largeTree . forEach ( ( n , index ) => {
175- getElementById . withArgs ( `${ n . id } _li` ) . returns ( { offsetTop : index , clientHeight : 1 } )
176+ getElementById . withArgs ( `${ n . id } _li` ) . returns ( { offsetTop : index , clientHeight : 1 , scrollIntoView } )
176177 } )
177178
178179 triggerOnKeyboardKeyDown ( wrapper , [ 'ArrowUp' ] )
179180 t . deepEqual ( wrapper . find ( 'li.focused' ) . text ( ) , 'label148' )
180- t . notDeepEqual ( contentNode . scrollTop , 0 )
181+ assert . calledOnce ( scrollIntoView )
181182
182183 getElementById . restore ( )
183184} )
184185
185186test ( 'should only scroll on keyboard navigation' , t => {
186187 const largeTree = [ ...Array ( 150 ) . keys ( ) ] . map ( i => node ( `id${ i } ` , `label${ i } ` ) )
187- const wrapper = mount ( < DropdownTreeSelect data = { largeTree } showDropdown = "initial" /> )
188188 const getElementById = stub ( document , 'getElementById' )
189+ const scrollIntoView = ( Element . prototype . scrollIntoView = spy ( ) )
190+ const wrapper = mount ( < DropdownTreeSelect data = { largeTree } showDropdown = "initial" /> )
189191 const contentNode = wrapper . find ( '.dropdown-content' ) . getDOMNode ( )
190192
191193 triggerOnKeyboardKeyDown ( wrapper , [ 'ArrowUp' ] )
192194 largeTree . forEach ( ( n , index ) => {
193- getElementById . withArgs ( `${ n . id } _li` ) . returns ( { offsetTop : index , clientHeight : 1 } )
195+ getElementById . withArgs ( `${ n . id } _li` ) . returns ( { offsetTop : index , clientHeight : 1 , scrollIntoView } )
194196 } )
195197
196198 triggerOnKeyboardKeyDown ( wrapper , [ 'ArrowUp' ] )
@@ -207,7 +209,8 @@ test('should only scroll on keyboard navigation', t => {
207209
208210 // Verify scroll is restored to previous position after keyboard nav
209211 triggerOnKeyboardKeyDown ( wrapper , [ 'ArrowUp' , 'ArrowDown' ] )
210- t . deepEqual ( contentNode . scrollTop , scrollTop )
212+ // Called once for each input, 3 in this case.
213+ assert . calledThrice ( scrollIntoView )
211214
212215 getElementById . restore ( )
213216} )
0 commit comments