|
| 1 | +import React from 'react'; |
| 2 | + |
| 3 | +import Enzyme, { mount } from 'enzyme'; |
| 4 | +import Adapter from 'enzyme-adapter-react-16'; |
| 5 | +Enzyme.configure({ adapter: new Adapter() }); |
| 6 | + |
| 7 | +import SpatialNavigation from '../src/spatial-navigation'; |
| 8 | +import withFocusable from '../src/with-focusable'; |
| 9 | + |
| 10 | +describe('withFocusable', () => { |
| 11 | + const Component = () => <div />; |
| 12 | + const renderComponent = ({ |
| 13 | + focusPath, |
| 14 | + currentFocusPath, |
| 15 | + setFocus = jest.fn(), |
| 16 | + }) => { |
| 17 | + const EnhancedComponent = withFocusable({ focusPath })(Component); |
| 18 | + return mount( |
| 19 | + <EnhancedComponent />, |
| 20 | + { context: { currentFocusPath, setFocus } } |
| 21 | + ); |
| 22 | + }; |
| 23 | + |
| 24 | + let component; |
| 25 | + |
| 26 | + it('injects focusPath as prop', () => { |
| 27 | + component = renderComponent({ focusPath: 'focusPath' }); |
| 28 | + expect(component.find(Component).prop('focusPath')).toEqual('focusPath'); |
| 29 | + }); |
| 30 | + |
| 31 | + describe('when element focusPath is the same as currentFocusPath', () => { |
| 32 | + it('injects focused prop as true', () => { |
| 33 | + const focusPath = 'focusPath1'; |
| 34 | + component = renderComponent({ currentFocusPath: focusPath, focusPath }); |
| 35 | + |
| 36 | + expect(component.find(Component).prop('focused')).toBe(true); |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + describe('when element focusPath is different than currentFocusPath', () => { |
| 41 | + it('injects focused prop as true', () => { |
| 42 | + component = renderComponent({ |
| 43 | + currentFocusPath: 'focusPath1', |
| 44 | + focusPath: 'focusPath2', |
| 45 | + }); |
| 46 | + |
| 47 | + expect(component.find(Component).prop('focused')).toBe(false); |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + describe('about setFocus injected prop', () => { |
| 52 | + it('injects function to children', () => { |
| 53 | + component = renderComponent({ focusPath: 'focusPath' }); |
| 54 | + expect(component.find(Component).prop('setFocus')).not.toBeFalsy(); |
| 55 | + }); |
| 56 | + |
| 57 | + it('binds configured focusPath as first parameter', () => { |
| 58 | + const setFocusSpy = jest.fn(); |
| 59 | + component = renderComponent({ |
| 60 | + focusPath: 'focusPath', |
| 61 | + setFocus: setFocusSpy, |
| 62 | + }); |
| 63 | + |
| 64 | + const setFocus = component.find(Component).prop('setFocus'); |
| 65 | + setFocus(); |
| 66 | + |
| 67 | + expect(setFocusSpy).toHaveBeenCalledWith('focusPath'); |
| 68 | + }); |
| 69 | + |
| 70 | + it('sends setFocus parameter as second parameter', () => { |
| 71 | + const setFocusSpy = jest.fn(); |
| 72 | + component = renderComponent({ |
| 73 | + focusPath: 'focusPath', |
| 74 | + setFocus: setFocusSpy, |
| 75 | + }); |
| 76 | + |
| 77 | + const setFocus = component.find(Component).prop('setFocus'); |
| 78 | + setFocus('otherFocusPath'); |
| 79 | + |
| 80 | + expect(setFocusSpy).toHaveBeenCalledWith('focusPath', 'otherFocusPath'); |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + describe('lifecycle', () => { |
| 85 | + beforeEach(() => { |
| 86 | + spyOn(SpatialNavigation, 'addFocusable'); |
| 87 | + spyOn(SpatialNavigation, 'removeFocusable'); |
| 88 | + }); |
| 89 | + |
| 90 | + it('adds focusable after component mounts', () => { |
| 91 | + component = renderComponent({ focusPath: 'focusPath' }); |
| 92 | + expect(SpatialNavigation.addFocusable).toHaveBeenCalledWith('focusPath'); |
| 93 | + }); |
| 94 | + |
| 95 | + it('removes focusable after component unmounts', () => { |
| 96 | + component = renderComponent({ focusPath: 'focusPath' }); |
| 97 | + |
| 98 | + component.unmount(); |
| 99 | + expect(SpatialNavigation.removeFocusable) |
| 100 | + .toHaveBeenCalledWith('focusPath'); |
| 101 | + }); |
| 102 | + }); |
| 103 | +}); |
0 commit comments