import React from 'react'; import { noop } from 'lodash'; import { mount } from 'enzyme'; import Button from '@material-ui/core/Button'; import AttachMoneyIcon from '@material-ui/icons/Apps'; import SectionTree, { SectionTreeTab } from './SectionTree'; describe('SectionTree', function() { const testSectionTree = { children: { overview: { label: 'Overview', iconClassName: 'icon-overview' }, billing: { label: 'Billing Dashboard', icon: }, engines: { label: 'Engines', iconClassName: 'icon-engines', children: { documentation: { label: 'Documentation', children: { api: { label: 'API' } } }, deployments: { label: 'Deployments' } } } } }; const defaultProps = { sections: testSectionTree, activePath: [], onNavigate: noop }; it('should render SectionTreeTabs for the root children by default', function() { const wrapper = mount(); // console.log(wrapper.find('SectionTreeTab').debug()) expect( wrapper.containsAllMatchingElements([ /* eslint-disable react/jsx-key */ , , ]) ).toBeTruthy(); }); it('should render a SectionTreeTab for each child of the props.activePath tree node', function() { let wrapper = mount( ); expect( wrapper.containsAllMatchingElements([ /* eslint-disable react/jsx-key */ , ]) ).toBeTruthy(); // deep wrapper = mount( ); expect( wrapper.containsMatchingElement() ).toBeTruthy(); }); it('Should render a header tab with back button if >0 levels deep', function() { let wrapper = mount(); expect( wrapper.containsMatchingElement( ) ).toBeFalsy(); wrapper = mount(); expect( wrapper.containsMatchingElement( ) ).toBeTruthy(); }); it('should show the label of the current path on the back button', function() { let wrapper = mount( ); expect( wrapper.containsMatchingElement( ) ).toBeTruthy(); wrapper = mount( ); expect( wrapper.containsMatchingElement( ) ).toBeTruthy(); }); it( 'should render the root and show the current item as selected when the currently ' + 'selected root item has no children', function() { let wrapper = mount( ); expect( wrapper.containsMatchingElement( ) ).toBeTruthy(); } ); it( 'should render the parent path and show the current item as selected when the ' + 'currently selected item has no children', function() { let wrapper = mount( ); expect( wrapper.containsMatchingElement( ) ).toBeTruthy(); wrapper = mount( ); expect( wrapper.containsMatchingElement() ).toBeTruthy(); } ); it('should not show a back button when displaying the root, unless root is the last-matched path', function() { let wrapper = mount(); expect( wrapper.containsMatchingElement( ) ).toBeFalsy(); // root is last matched wrapper = mount( ); expect( wrapper.containsMatchingElement( ) ).toBeTruthy(); }); it('should show a back button with the item name when the currently selected item has no children', function() { let wrapper = mount( ); expect( wrapper.containsMatchingElement( ) ).toBeTruthy(); wrapper = mount( ); expect( wrapper.containsMatchingElement( ) ).toBeTruthy(); }); it('should, given a partial path, render the items of the deepest matching path', function() { const wrapper = mount( ); expect( wrapper.containsMatchingElement() ).toBeTruthy(); }); it( 'should, given a partial path, render a back button that navigates to the ' + 'parent of the deepest matching path, with the label of the deepest matching path', function() { const handler = jest.fn(); const wrapper = mount( ); expect( wrapper.containsMatchingElement( ) ).toBeTruthy(); wrapper .find('SectionTreeTab[data-testtarget="back-button"]') .first() .simulate('click'); expect(handler).toHaveBeenCalledWith(['engines']); } ); it('should, given a partial path, handle navigation to siblings on the deepest matched path', function() { const handler = jest.fn(); const wrapper = mount( ); expect( wrapper.containsAllMatchingElements([ , ]) ).toBeTruthy(); wrapper.find('SectionTreeTab[label="Deployments"]').simulate('click'); expect(handler).toHaveBeenCalledWith(['engines', 'deployments']); }); it('should call props.onNavigate for backward navigation, dropping the last path index', function() { const handler = jest.fn(); const wrapper = mount( ); wrapper .find('SectionTreeTab') .first() .simulate('click'); expect(handler).toHaveBeenCalledWith(['engines']); }); it('should call props.onNavigate for backward navigation, dropping the last path index (no children)', function() { const handler = jest.fn(); const wrapper = mount( ); wrapper .find('SectionTreeTab[data-testtarget="back-button"]') .first() .simulate('click'); expect(handler).toHaveBeenCalledWith(['engines']); }); it('should call props.onNavigate for navigation between sibling paths', function() { const handler = jest.fn(); const wrapper = mount( ); wrapper.find('SectionTreeTab[label="Engines"]').simulate('click'); expect(handler).toHaveBeenCalledWith(['engines']); wrapper.find('SectionTreeTab[label="Billing Dashboard"]').simulate('click'); expect(handler).toHaveBeenCalledWith(['billing']); }); }); describe('SectionTreeTab', function() { const defaultProps = { label: 'test-label', id: 'some-id' }; it('shows props.label', function() { const wrapper = mount( ); expect(wrapper.text()).toMatch(/test-label/); }); it('shows props.leftIcon', function() { const wrapper = mount( } /> ); expect( wrapper.containsMatchingElement(
) ).toBeTruthy(); }); it('shows props.rightIcon', function() { const wrapper = mount( } /> ); expect( wrapper.containsMatchingElement(
) ).toBeTruthy(); }); it('adds selected styling with props.selected', function() { const wrapper = mount(); expect(wrapper.find(Button).first().props().classes.root).toContain('selected'); }); it('calls props.onClick with props.id', function() { const handler = jest.fn(); const wrapper = mount( ); wrapper.simulate('click'); expect(handler).toHaveBeenCalledWith('some-id'); }); });