|  | 
| 1 | 1 | import React from 'react'; | 
| 2 |  | -import { spy, stub } from 'sinon'; | 
|  | 2 | +import { spy } from 'sinon'; | 
| 3 | 3 | import { assert } from 'chai'; | 
| 4 |  | -import ReactDOM from 'react-dom'; | 
| 5 | 4 | import { createShallow, createMount, getClasses } from '@material-ui/core/test-utils'; | 
| 6 | 5 | import Popover from '../Popover'; | 
| 7 | 6 | import Menu from './Menu'; | 
| 8 | 7 | import MenuList from '../MenuList'; | 
| 9 | 8 | 
 | 
|  | 9 | +const MENU_LIST_HEIGHT = 100; | 
|  | 10 | + | 
| 10 | 11 | describe('<Menu />', () => { | 
| 11 | 12 |   let shallow; | 
| 12 | 13 |   let classes; | 
| @@ -126,108 +127,35 @@ describe('<Menu />', () => { | 
| 126 | 127 |     assert.strictEqual(document.activeElement, menuEl && menuEl.firstChild); | 
| 127 | 128 |   }); | 
| 128 | 129 | 
 | 
| 129 |  | -  describe('mount', () => { | 
| 130 |  | -    let wrapper; | 
| 131 |  | -    let instance; | 
| 132 |  | - | 
| 133 |  | -    let selectedItemFocusSpy; | 
| 134 |  | -    let menuListSpy; | 
| 135 |  | -    let menuListFocusSpy; | 
| 136 |  | - | 
| 137 |  | -    let elementForHandleEnter; | 
| 138 |  | - | 
| 139 |  | -    const SELECTED_ITEM_KEY = 111111; | 
| 140 |  | -    const MENU_LIST_HEIGHT = 100; | 
|  | 130 | +  it('should call props.onEntering with element if exists', () => { | 
|  | 131 | +    const onEnteringSpy = spy(); | 
|  | 132 | +    const wrapper = mount(<Menu {...defaultProps} classes={classes} onEntering={onEnteringSpy} />); | 
|  | 133 | +    const instance = wrapper.find('Menu').instance(); | 
| 141 | 134 | 
 | 
| 142 |  | -    let findDOMNodeStub; | 
| 143 |  | - | 
| 144 |  | -    before(() => { | 
| 145 |  | -      wrapper = mount(<Menu {...defaultProps} classes={classes} />); | 
| 146 |  | -      instance = wrapper.find('Menu').instance(); | 
| 147 |  | - | 
| 148 |  | -      selectedItemFocusSpy = spy(); | 
| 149 |  | -      menuListFocusSpy = spy(); | 
| 150 |  | -      menuListSpy = {}; | 
| 151 |  | -      menuListSpy.clientHeight = MENU_LIST_HEIGHT; | 
| 152 |  | -      menuListSpy.style = {}; | 
| 153 |  | -      menuListSpy.firstChild = { focus: menuListFocusSpy }; | 
| 154 |  | - | 
| 155 |  | -      findDOMNodeStub = stub(ReactDOM, 'findDOMNode').callsFake(arg => { | 
| 156 |  | -        if (arg === SELECTED_ITEM_KEY) { | 
| 157 |  | -          return { focus: selectedItemFocusSpy }; | 
| 158 |  | -        } | 
| 159 |  | -        return menuListSpy; | 
| 160 |  | -      }); | 
| 161 |  | - | 
| 162 |  | -      elementForHandleEnter = { clientHeight: MENU_LIST_HEIGHT }; | 
| 163 |  | -    }); | 
| 164 |  | - | 
| 165 |  | -    after(() => { | 
| 166 |  | -      findDOMNodeStub.restore(); | 
| 167 |  | -    }); | 
|  | 135 | +    const elementForHandleEnter = { clientHeight: MENU_LIST_HEIGHT }; | 
| 168 | 136 | 
 | 
| 169 |  | -    beforeEach(() => { | 
| 170 |  | -      menuListFocusSpy.resetHistory(); | 
| 171 |  | -      selectedItemFocusSpy.resetHistory(); | 
| 172 |  | -    }); | 
|  | 137 | +    instance.handleEntering(elementForHandleEnter); | 
|  | 138 | +    assert.strictEqual(onEnteringSpy.callCount, 1); | 
|  | 139 | +    assert.strictEqual(onEnteringSpy.calledWith(elementForHandleEnter), true); | 
|  | 140 | +  }); | 
| 173 | 141 | 
 | 
| 174 |  | -    it('should call props.onEntering with element if exists', () => { | 
| 175 |  | -      const onEnteringSpy = spy(); | 
| 176 |  | -      wrapper.setProps({ onEntering: onEnteringSpy }); | 
| 177 |  | -      instance.handleEntering(elementForHandleEnter); | 
| 178 |  | -      assert.strictEqual(onEnteringSpy.callCount, 1); | 
| 179 |  | -      assert.strictEqual(onEnteringSpy.calledWith(elementForHandleEnter), true); | 
| 180 |  | -    }); | 
|  | 142 | +  it('should call props.onEntering, disableAutoFocusItem', () => { | 
|  | 143 | +    const onEnteringSpy = spy(); | 
|  | 144 | +    const wrapper = mount( | 
|  | 145 | +      <Menu disableAutoFocusItem {...defaultProps} classes={classes} onEntering={onEnteringSpy} />, | 
|  | 146 | +    ); | 
|  | 147 | +    const instance = wrapper.find('Menu').instance(); | 
| 181 | 148 | 
 | 
| 182 |  | -    it('should call menuList focus when no menuList', () => { | 
| 183 |  | -      delete instance.menuListRef; | 
| 184 |  | -      instance.handleEntering(elementForHandleEnter); | 
| 185 |  | -      assert.strictEqual(selectedItemFocusSpy.callCount, 0); | 
| 186 |  | -      assert.strictEqual(menuListFocusSpy.callCount, 1); | 
| 187 |  | -    }); | 
|  | 149 | +    const elementForHandleEnter = { clientHeight: MENU_LIST_HEIGHT }; | 
| 188 | 150 | 
 | 
| 189 |  | -    it('should call menuList focus when menuList but no menuList.selectedItemRef ', () => { | 
| 190 |  | -      instance.menuListRef = {}; | 
| 191 |  | -      delete instance.menuListRef.selectedItemRef; | 
| 192 |  | -      instance.handleEntering(elementForHandleEnter); | 
| 193 |  | -      assert.strictEqual(selectedItemFocusSpy.callCount, 0); | 
| 194 |  | -      assert.strictEqual(menuListFocusSpy.callCount, 1); | 
| 195 |  | -    }); | 
|  | 151 | +    instance.handleEntering(elementForHandleEnter); | 
|  | 152 | +    assert.strictEqual(onEnteringSpy.callCount, 1); | 
|  | 153 | +    assert.strictEqual(onEnteringSpy.calledWith(elementForHandleEnter), true); | 
|  | 154 | +  }); | 
| 196 | 155 | 
 | 
| 197 |  | -    describe('menuList.selectedItemRef exists', () => { | 
| 198 |  | -      before(() => { | 
| 199 |  | -        instance.menuListRef = {}; | 
| 200 |  | -        instance.menuListRef.selectedItemRef = SELECTED_ITEM_KEY; | 
| 201 |  | -      }); | 
| 202 |  | - | 
| 203 |  | -      it('should call selectedItem focus when there is a menuList.selectedItemRef', () => { | 
| 204 |  | -        instance.handleEntering(elementForHandleEnter); | 
| 205 |  | -        assert.strictEqual(selectedItemFocusSpy.callCount, 1); | 
| 206 |  | -        assert.strictEqual(menuListFocusSpy.callCount, 0); | 
| 207 |  | -      }); | 
| 208 |  | - | 
| 209 |  | -      it('should not set style on list when element.clientHeight > list.clientHeight', () => { | 
| 210 |  | -        elementForHandleEnter.clientHeight = MENU_LIST_HEIGHT + 1; | 
| 211 |  | -        instance.handleEntering(elementForHandleEnter); | 
| 212 |  | -        assert.strictEqual(menuListSpy.style.paddingRight, undefined); | 
| 213 |  | -        assert.strictEqual(menuListSpy.style.width, undefined); | 
| 214 |  | -      }); | 
| 215 |  | - | 
| 216 |  | -      it('should not set style on list when element.clientHeight == list.clientHeight', () => { | 
| 217 |  | -        elementForHandleEnter.clientHeight = MENU_LIST_HEIGHT; | 
| 218 |  | -        instance.handleEntering(elementForHandleEnter); | 
| 219 |  | -        assert.strictEqual(menuListSpy.style.paddingRight, undefined); | 
| 220 |  | -        assert.strictEqual(menuListSpy.style.width, undefined); | 
| 221 |  | -      }); | 
| 222 |  | - | 
| 223 |  | -      it('should not set style on list when element.clientHeight < list.clientHeight', () => { | 
| 224 |  | -        assert.strictEqual(menuListSpy.style.paddingRight, undefined); | 
| 225 |  | -        assert.strictEqual(menuListSpy.style.width, undefined); | 
| 226 |  | -        elementForHandleEnter.clientHeight = MENU_LIST_HEIGHT - 1; | 
| 227 |  | -        instance.handleEntering(elementForHandleEnter); | 
| 228 |  | -        assert.notStrictEqual(menuListSpy.style.paddingRight, undefined); | 
| 229 |  | -        assert.notStrictEqual(menuListSpy.style.width, undefined); | 
| 230 |  | -      }); | 
| 231 |  | -    }); | 
|  | 156 | +  it('call handleListKeyDown without onClose prop', () => { | 
|  | 157 | +    const wrapper = mount(<Menu {...defaultProps} />); | 
|  | 158 | +    const instance = wrapper.find('Menu').instance(); | 
|  | 159 | +    instance.handleListKeyDown({ key: 'Tab', preventDefault: () => {} }); | 
| 232 | 160 |   }); | 
| 233 | 161 | }); | 
0 commit comments