|
| 1 | +import VideoCoverFallback from '../VideoCoverFallback'; |
| 2 | + |
| 3 | +import React from 'react'; |
| 4 | +import { shallow, mount } from 'enzyme'; |
| 5 | + |
| 6 | +describe('VideoCoverFallback', () => { |
| 7 | + it('should update container ratio when mounted', () => { |
| 8 | + const spy = jest.fn(); |
| 9 | + class WithSpy extends VideoCoverFallback { |
| 10 | + updateContainerRatio = spy; |
| 11 | + } |
| 12 | + mount(<WithSpy />); |
| 13 | + expect(spy).toBeCalled(); |
| 14 | + }); |
| 15 | + |
| 16 | + it('should call onFallbackDidMount prop when mounted', () => { |
| 17 | + const spy = jest.fn(); |
| 18 | + mount(<VideoCoverFallback onFallbackDidMount={spy} />); |
| 19 | + expect(spy).toBeCalled(); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should pass this.updateContainerRatio as parameter in onFallbackWillUnmount', () => { |
| 23 | + let resizeNotifier; |
| 24 | + const wrapper = mount(<VideoCoverFallback |
| 25 | + onFallbackDidMount={result => { |
| 26 | + resizeNotifier = result; |
| 27 | + }} |
| 28 | + />); |
| 29 | + expect(resizeNotifier).toEqual(wrapper.instance().updateContainerRatio); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should initialize window-resize eventlisteners if props.remeasureOnWindowResize is set', () => { |
| 33 | + const spy = jest.fn(); |
| 34 | + class WithSpy extends VideoCoverFallback { |
| 35 | + initEventListeners = spy; |
| 36 | + } |
| 37 | + mount(<WithSpy remeasureOnWindowResize />); |
| 38 | + expect(spy).toBeCalled(); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should NOT initialize window-resize eventlisteners if props.remeasureOnWindowResize is not set', () => { |
| 42 | + const spy = jest.fn(); |
| 43 | + class WithSpy extends VideoCoverFallback { |
| 44 | + initEventListeners = spy; |
| 45 | + } |
| 46 | + mount(<WithSpy />); |
| 47 | + expect(spy).not.toBeCalled(); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should remove eventlisteners before unmount', () => { |
| 51 | + const spy = jest.fn(); |
| 52 | + class WithSpy extends VideoCoverFallback { |
| 53 | + removeEventListeners = spy; |
| 54 | + } |
| 55 | + const wrapper = mount(<WithSpy />); |
| 56 | + wrapper.unmount(); |
| 57 | + expect(spy).toBeCalled(); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should add/remove eventlisteners if props.remeasureOnWindowResize changes', () => { |
| 61 | + const addSpy = jest.fn(); |
| 62 | + const removeSpy = jest.fn(); |
| 63 | + class WithSpy extends VideoCoverFallback { |
| 64 | + initEventListeners = addSpy; |
| 65 | + removeEventListeners = removeSpy; |
| 66 | + } |
| 67 | + const wrapper = mount(<WithSpy />); |
| 68 | + expect(addSpy).not.toBeCalled(); |
| 69 | + expect(removeSpy).not.toBeCalled(); |
| 70 | + wrapper.setProps({ remeasureOnWindowResize: true }); |
| 71 | + expect(addSpy).toBeCalled(); |
| 72 | + expect(removeSpy).not.toBeCalled(); |
| 73 | + wrapper.setProps({ remeasureOnWindowResize: false }); |
| 74 | + expect(addSpy).toBeCalled(); |
| 75 | + expect(removeSpy).toBeCalled(); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should render a video tag inside a container-div', () => { |
| 79 | + const wrapper = shallow(<VideoCoverFallback />); |
| 80 | + expect(wrapper.find('div')).toExist(); |
| 81 | + expect(wrapper.find('div video')).toExist(); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should pass props.className to the container-div', () => { |
| 85 | + const wrapper = shallow(<VideoCoverFallback className="some-classname" />); |
| 86 | + expect(wrapper).toHaveClassName('some-classname'); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should invoke updateVideoRatio on loadedData media event', () => { |
| 90 | + const spy = jest.fn(); |
| 91 | + class WithSpy extends VideoCoverFallback { |
| 92 | + updateVideoRatio = spy; |
| 93 | + } |
| 94 | + const wrapper = shallow(<WithSpy />); |
| 95 | + const video = wrapper.find('video'); |
| 96 | + video.simulate('loadedData', { |
| 97 | + target: { |
| 98 | + videoWidth: 50, |
| 99 | + videoHeight: 50, |
| 100 | + }, |
| 101 | + }); |
| 102 | + expect(spy).toBeCalledWith(50, 50); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should apply all props.videoOptions to the video tag', () => { |
| 106 | + const wrapper = shallow(<VideoCoverFallback |
| 107 | + videoOptions={{ |
| 108 | + src: 'http://some-video-url.mp4', |
| 109 | + }} |
| 110 | + />); |
| 111 | + expect(wrapper.find('video')).toHaveProp('src', 'http://some-video-url.mp4'); |
| 112 | + }); |
| 113 | + |
| 114 | + describe('container-styles', () => { |
| 115 | + it('should apply props.style to the container-div', () => { |
| 116 | + const wrapper = shallow(<VideoCoverFallback |
| 117 | + style={{ |
| 118 | + backgroundColor: 'teal', |
| 119 | + lineHeight: '100px', |
| 120 | + }} |
| 121 | + />); |
| 122 | + expect(wrapper).toHaveStyle('backgroundColor', 'teal'); |
| 123 | + expect(wrapper).toHaveStyle('lineHeight', '100px'); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should set width and height to 100% by default', () => { |
| 127 | + const wrapper = shallow(<VideoCoverFallback />); |
| 128 | + expect(wrapper).toHaveStyle('height', '100%'); |
| 129 | + expect(wrapper).toHaveStyle('width', '100%'); |
| 130 | + }); |
| 131 | + |
| 132 | + it('should be possible to override width and height via props.style', () => { |
| 133 | + const wrapper = shallow(<VideoCoverFallback style={{ width: '50%', height: '50%' }} />); |
| 134 | + expect(wrapper).toHaveStyle('height', '50%'); |
| 135 | + expect(wrapper).toHaveStyle('width', '50%'); |
| 136 | + }); |
| 137 | + |
| 138 | + it('should set position relative and overflow: hidden', () => { |
| 139 | + const wrapper = shallow(<VideoCoverFallback />); |
| 140 | + expect(wrapper).toHaveStyle('position', 'relative'); |
| 141 | + expect(wrapper).toHaveStyle('overflow', 'hidden'); |
| 142 | + }); |
| 143 | + |
| 144 | + it('should not be possible to override position and overflow', () => { |
| 145 | + const wrapper = shallow(<VideoCoverFallback |
| 146 | + style={{ |
| 147 | + position: 'fixed', |
| 148 | + overflow: 'scroll', |
| 149 | + }} |
| 150 | + />); |
| 151 | + expect(wrapper).toHaveStyle('position', 'relative'); |
| 152 | + expect(wrapper).toHaveStyle('overflow', 'hidden'); |
| 153 | + }); |
| 154 | + }); |
| 155 | + |
| 156 | + // todo: maybe use generated test-data for this? |
| 157 | + describe('video-styles', () => { |
| 158 | + it('should have width auto, height 100% if innerRatio > outerRatio', () => { |
| 159 | + const wrapper = shallow(<VideoCoverFallback />); |
| 160 | + wrapper.setState({ |
| 161 | + innerRatio: 5, |
| 162 | + outerRatio: 3, |
| 163 | + }); |
| 164 | + expect(wrapper.find('video')).toHaveStyle('width', 'auto'); |
| 165 | + expect(wrapper.find('video')).toHaveStyle('height', '100%'); |
| 166 | + }); |
| 167 | + it('should have width 100%, height auto if innerRatio <= outerRatio', () => { |
| 168 | + const wrapper = shallow(<VideoCoverFallback />); |
| 169 | + wrapper.setState({ |
| 170 | + innerRatio: 3, |
| 171 | + outerRatio: 5, |
| 172 | + }); |
| 173 | + expect(wrapper.find('video')).toHaveStyle('width', '100%'); |
| 174 | + expect(wrapper.find('video')).toHaveStyle('height', 'auto'); |
| 175 | + }); |
| 176 | + }); |
| 177 | + |
| 178 | + describe('updateContainerRatio()', () => { |
| 179 | + it('should set state.outerRatio to ratio of container width/height', () => { |
| 180 | + const mockRef = { |
| 181 | + getBoundingClientRect: () => { |
| 182 | + const result = { |
| 183 | + width: 4, |
| 184 | + height: 5, |
| 185 | + }; |
| 186 | + return result; |
| 187 | + }, |
| 188 | + }; |
| 189 | + class WithRef extends VideoCoverFallback { |
| 190 | + containerRef = mockRef; |
| 191 | + } |
| 192 | + const wrapper = shallow(<WithRef />); |
| 193 | + wrapper.instance().updateContainerRatio(); |
| 194 | + expect(wrapper).toHaveState('outerRatio', 4 / 5); |
| 195 | + }); |
| 196 | + }); |
| 197 | + |
| 198 | + describe('updateVideoRatio()', () => { |
| 199 | + it('should set state.innerRatio to ratio of video width/height', () => { |
| 200 | + const wrapper = shallow(<VideoCoverFallback />); |
| 201 | + expect(wrapper).toHaveState('innerRatio', undefined); |
| 202 | + wrapper.instance().updateVideoRatio(4, 5); |
| 203 | + expect(wrapper).toHaveState('innerRatio', 4 / 5); |
| 204 | + }); |
| 205 | + }); |
| 206 | +}); |
0 commit comments