@@ -97,4 +97,65 @@ describe('Modal', function () {
9797 assert . match ( dialog . props . className , / \b t e s t C s s \b / ) ;
9898 } ) ;
9999
100+ describe ( 'Focused state' , function ( ) {
101+ let focusableContainer = null ;
102+ beforeEach ( function ( ) {
103+ focusableContainer = document . createElement ( 'div' ) ;
104+ focusableContainer . tabIndex = 0 ;
105+ document . body . appendChild ( focusableContainer ) ;
106+ focusableContainer . focus ( ) ;
107+ } ) ;
108+
109+ afterEach ( function ( ) {
110+ React . unmountComponentAtNode ( focusableContainer ) ;
111+ document . body . removeChild ( focusableContainer ) ;
112+ } ) ;
113+
114+ it ( 'Should focus on the Modal when it is opened' , function ( done ) {
115+ document . activeElement . should . equal ( focusableContainer ) ;
116+
117+ let doneOp = function ( ) {
118+ // focus should be back on the previous element when modal closed
119+ setTimeout ( function ( ) {
120+ document . activeElement . should . equal ( focusableContainer ) ;
121+ done ( ) ;
122+ } , 0 ) ;
123+ } ;
124+
125+ let Container = React . createClass ( {
126+ getInitialState ( ) {
127+ return { modalOpen : true } ;
128+ } ,
129+ handleCloseModal ( ) {
130+ this . setState ( { modalOpen : false } ) ;
131+ doneOp ( ) ;
132+ } ,
133+ render ( ) {
134+ if ( this . state . modalOpen ) {
135+ return (
136+ < Modal onRequestHide = { this . handleCloseModal } container = { this } >
137+ < strong > Message</ strong >
138+ </ Modal >
139+ ) ;
140+ } else {
141+ return < span /> ;
142+ }
143+ }
144+ } ) ;
145+
146+ let instance = React . render ( < Container /> , focusableContainer ) ;
147+
148+ setTimeout ( function ( ) {
149+ // modal should be focused when opened
150+ let modal = instance . getDOMNode ( ) . getElementsByClassName ( 'modal' ) [ 0 ] ;
151+ document . activeElement . should . equal ( modal ) ;
152+
153+ // close the modal
154+ let backdrop = instance . getDOMNode ( ) . getElementsByClassName ( 'modal-backdrop' ) [ 0 ] ;
155+ ReactTestUtils . Simulate . click ( backdrop ) ;
156+ } , 0 ) ;
157+ } ) ;
158+
159+ } ) ;
160+
100161} ) ;
0 commit comments