|
| 1 | +import React from 'react'; |
| 2 | +import { shallow, mount } from 'enzyme'; |
| 3 | +import Collapse from '../Collapse'; |
| 4 | + |
| 5 | +describe('Collapse', () => { |
| 6 | + let isOpen; |
| 7 | + let toggle; |
| 8 | + |
| 9 | + beforeEach(() => { |
| 10 | + isOpen = false; |
| 11 | + toggle = () => { isOpen = !isOpen; }; |
| 12 | + jasmine.clock().install(); |
| 13 | + }); |
| 14 | + |
| 15 | + afterEach(() => { |
| 16 | + // fast forward time for collapse to fade out |
| 17 | + jasmine.clock().tick(400); |
| 18 | + jasmine.clock().uninstall(); |
| 19 | + }); |
| 20 | + |
| 21 | + it('should render children', () => { |
| 22 | + const wrapper = shallow(<Collapse><p>hello</p></Collapse>).find('p'); |
| 23 | + expect(wrapper.text()).toBe('hello'); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should have default isOpen value', () => { |
| 27 | + const wrapper = shallow(<Collapse />); |
| 28 | + expect(wrapper.instance().props.isOpen).toEqual(false); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should render with class "collapse"', () => { |
| 32 | + const wrapper = shallow(<Collapse />); |
| 33 | + expect(wrapper.hasClass('collapse')).toEqual(true); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should render with class "in" when isOpen is true', () => { |
| 37 | + const wrapper = shallow(<Collapse isOpen />); |
| 38 | + expect(wrapper.hasClass('in')).toEqual(true); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should set height to null when isOpen is true', () => { |
| 42 | + isOpen = true; |
| 43 | + const wrapper = shallow(<Collapse isOpen={isOpen} />); |
| 44 | + expect(wrapper.state('height')).toBe(null); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should set height to 0 when isOpen is false', () => { |
| 48 | + const wrapper = shallow(<Collapse isOpen={isOpen} />); |
| 49 | + expect(wrapper.state('height')).toBe(0); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should render with class "collapse" with default collapse state', () => { |
| 53 | + const wrapper = mount(<Collapse isOpen={isOpen} />); |
| 54 | + wrapper.setState({ collapse: null }); |
| 55 | + jasmine.clock().tick(360); |
| 56 | + wrapper.update(); |
| 57 | + expect(wrapper.find('.collapse').length).toBe(1); |
| 58 | + wrapper.unmount(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should change state with { collapse: ${State} } when isOpen change to true before transition', () => { |
| 62 | + const wrapper = mount(<Collapse isOpen={isOpen} />); |
| 63 | + toggle(); |
| 64 | + wrapper.setProps({ isOpen: isOpen }); |
| 65 | + expect(wrapper.state('collapse')).toEqual('SHOW'); |
| 66 | + wrapper.unmount(); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should change state with { collapse: ${State} } when isOpen change to true after transition', () => { |
| 70 | + const wrapper = mount(<Collapse isOpen={isOpen} />); |
| 71 | + toggle(); |
| 72 | + wrapper.setProps({ isOpen: isOpen }); |
| 73 | + jasmine.clock().tick(350); |
| 74 | + expect(wrapper.state('collapse')).toEqual('SHOWN'); |
| 75 | + wrapper.unmount(); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should change state with { collapse: ${State} } when isOpen change to false before transition', () => { |
| 79 | + isOpen = true; |
| 80 | + const wrapper = mount(<Collapse isOpen={isOpen} />); |
| 81 | + toggle(); |
| 82 | + wrapper.setProps({ isOpen: isOpen }); |
| 83 | + expect(wrapper.state('collapse')).toEqual('HIDE'); |
| 84 | + wrapper.unmount(); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should change state with { collapse: ${State} } when isOpen change to false after transition', () => { |
| 88 | + isOpen = true; |
| 89 | + const wrapper = mount(<Collapse isOpen={isOpen} />); |
| 90 | + toggle(); |
| 91 | + wrapper.setProps({ isOpen: isOpen }); |
| 92 | + jasmine.clock().tick(360); |
| 93 | + expect(wrapper.state('collapse')).toEqual('HIDDEN'); |
| 94 | + wrapper.unmount(); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should set inline style to 0 when isOpen change to false', () => { |
| 98 | + isOpen = true; |
| 99 | + const wrapper = mount(<Collapse isOpen={isOpen} />); |
| 100 | + toggle(); |
| 101 | + wrapper.setProps({ isOpen: isOpen }); |
| 102 | + expect(wrapper.state('height')).toBe(0); |
| 103 | + wrapper.unmount(); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should remove inline style when isOpen change to true after transition', () => { |
| 107 | + const wrapper = mount(<Collapse isOpen={isOpen} />); |
| 108 | + toggle(); |
| 109 | + wrapper.setProps({ isOpen: isOpen }); |
| 110 | + jasmine.clock().tick(380); |
| 111 | + expect(wrapper.state('height')).toBe(null); |
| 112 | + wrapper.unmount(); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should remove timeout tag after unmount', () => { |
| 116 | + spyOn(Collapse.prototype, 'componentWillUnmount').and.callThrough(); |
| 117 | + const wrapper = mount(<Collapse isOpen={isOpen} />); |
| 118 | + wrapper.unmount(); |
| 119 | + expect(Collapse.prototype.componentWillUnmount).toHaveBeenCalled(); |
| 120 | + }); |
| 121 | +}); |
0 commit comments