|
| 1 | +import React, { Component, Children } from 'react'; |
| 2 | +import renderer from 'react-test-renderer'; |
| 3 | +import TextField from '@material-ui/core/TextField'; |
| 4 | +import { shallow, mount } from 'enzyme'; |
| 5 | +import Select from '@material-ui/core/Select'; |
| 6 | +import Button from '@material-ui/core/Button'; |
| 7 | +import ConnectedRoutesContainer, { RoutesContainer } from '../RoutesContainer.jsx'; |
| 8 | +import { styles } from '../LeftComponentContainer.jsx'; |
| 9 | +import RouteDisplay from '../../components/RouteDisplay.jsx'; |
| 10 | + |
| 11 | +describe('Routes Container Tests', () => { |
| 12 | + let wrapper; |
| 13 | + const addRoute = jest.fn(); |
| 14 | + |
| 15 | + const props = { |
| 16 | + classes: {}, |
| 17 | + addRoute, |
| 18 | + deleteRoute: () => null, |
| 19 | + setSelectableRoutes: () => null, |
| 20 | + setVisible: () => null, |
| 21 | + component: { |
| 22 | + id: '3', |
| 23 | + selectableRoutes: [ |
| 24 | + { |
| 25 | + id: '1', |
| 26 | + title: 'route1', |
| 27 | + }, |
| 28 | + { |
| 29 | + id: '2', |
| 30 | + title: 'route2', |
| 31 | + }, |
| 32 | + ], |
| 33 | + routes: [ |
| 34 | + { |
| 35 | + routeCompId: '3', |
| 36 | + title: 'route3', |
| 37 | + }, |
| 38 | + { |
| 39 | + routeCompId: '4', |
| 40 | + title: 'route4', |
| 41 | + }, |
| 42 | + ], |
| 43 | + }, |
| 44 | + }; |
| 45 | + |
| 46 | + beforeAll(() => { |
| 47 | + wrapper = mount(<RoutesContainer {...props} />); |
| 48 | + }); |
| 49 | + |
| 50 | + it('snapshot test', () => { |
| 51 | + const tree = renderer.create(<RoutesContainer {...props} />).toJSON(); |
| 52 | + expect(tree).toMatchSnapshot(); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should contain an input field that calls handleUpdate when changed', () => { |
| 56 | + const inputField = wrapper.find(TextField); |
| 57 | + expect(inputField).toHaveLength(1); |
| 58 | + const instance = wrapper.instance(); |
| 59 | + |
| 60 | + expect(inputField.prop('onChange')).toBeDefined(); |
| 61 | + expect(inputField.prop('onChange')).toBeInstanceOf(Function); |
| 62 | + expect(inputField.prop('onChange')).toEqual(instance.handleChange); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should have a handleUpdate function which sets the state to the current string', () => { |
| 66 | + expect(wrapper.state().pathName).toBe(''); |
| 67 | + wrapper.find(TextField).prop('onChange')({ target: { value: 'a' }, preventDefault: () => null }); |
| 68 | + expect(wrapper.state().pathName).toBe('a'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should contain two RoutesContainer MUI components', () => { |
| 72 | + expect(wrapper.find(RouteDisplay)).toHaveLength(2); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should contain three route child options', () => { |
| 76 | + const optionslist = wrapper.find('option'); |
| 77 | + expect(optionslist).toHaveLength(3); |
| 78 | + expect(optionslist.at(0).text()).toBe('None'); |
| 79 | + expect(optionslist.at(1).text()).toBe('route1'); |
| 80 | + expect(optionslist.at(2).text()).toBe('route2'); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should set the selectedRouteId in state when routeChild is selected', () => { |
| 84 | + const selectComp = wrapper.find('Select'); |
| 85 | + selectComp.prop('onChange')({ target: { value: '2' } }); |
| 86 | + expect(wrapper.state().selectedRouteId).toBe('2'); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should run handleAddRoute when the add button is clicked', () => { |
| 90 | + const instance = wrapper.instance(); |
| 91 | + const buttonClickHandler = wrapper.find(Button).prop('onClick'); |
| 92 | + expect(buttonClickHandler).toEqual(instance.handleAddRoute); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should pass route object into AddRoute when the add button is clicked', () => { |
| 96 | + wrapper.find(Button).prop('onClick')(); |
| 97 | + expect(addRoute).toBeCalledWith({ path: 'a', routeCompId: '2', routerCompId: '3' }); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should contain two RoutesContainer MUI components', () => { |
| 101 | + expect(wrapper.find(RouteDisplay)).toHaveLength(2); |
| 102 | + }); |
| 103 | +}); |
0 commit comments