|
| 1 | +import React from 'react' |
| 2 | +import { shallow } from 'enzyme' |
| 3 | +import { colors } from 'styles' |
| 4 | +import { |
| 5 | + Wrapper, |
| 6 | + ReactSmartSlider, |
| 7 | + CustomScrollbar, |
| 8 | + ScrollCircle, |
| 9 | + SecondWrapper |
| 10 | +} from './ReactSmartSlider' |
| 11 | + |
| 12 | +const mockConfig = (device: string) => ({ |
| 13 | + value: device, |
| 14 | + configurable: true |
| 15 | +}) |
| 16 | +const agent = 'userAgent' |
| 17 | +const initialProps = { |
| 18 | + cols: 6, |
| 19 | + spacing: 16, |
| 20 | + circleSize: 15, |
| 21 | + barHeight: 5, |
| 22 | + circleColor: colors.primary, |
| 23 | + barColor: colors.gray.mediumGray |
| 24 | +} |
| 25 | + |
| 26 | +describe('ReactSmartSlider: lib/components', () => { |
| 27 | + it('should render itself', () => { |
| 28 | + const wrapper = shallow(<ReactSmartSlider {...initialProps}/>) |
| 29 | + |
| 30 | + expect(wrapper.find(Wrapper).exists()).toEqual(true) |
| 31 | + expect(wrapper.find(SecondWrapper).exists()).toEqual(true) |
| 32 | + expect(wrapper.find(CustomScrollbar).exists()).toEqual(true) |
| 33 | + expect(wrapper.find(ScrollCircle).exists()).toEqual(true) |
| 34 | + }) |
| 35 | + |
| 36 | + it('should not render CustomScrollbar', () => { |
| 37 | + Object.defineProperty(window.navigator, agent, mockConfig('iPhone')) |
| 38 | + |
| 39 | + const wrapper = shallow(<ReactSmartSlider {...initialProps}/>) |
| 40 | + |
| 41 | + expect(wrapper.find(CustomScrollbar).exists()).toEqual(false) |
| 42 | + expect(wrapper.find(ScrollCircle).exists()).toEqual(false) |
| 43 | + }) |
| 44 | + |
| 45 | + it('should invoke onOverflowContentScroll onScroll', () => { |
| 46 | + const onOverflowContentScrollSpy = jest.spyOn(ReactSmartSlider.prototype, 'onOverflowContentScroll') |
| 47 | + const wrapper = shallow(<ReactSmartSlider {...initialProps}/>) |
| 48 | + |
| 49 | + wrapper.find(SecondWrapper).simulate('scroll') |
| 50 | + |
| 51 | + expect(onOverflowContentScrollSpy).toHaveBeenCalled() |
| 52 | + }) |
| 53 | + |
| 54 | + it('should invoke measureContainers onLoad', () => { |
| 55 | + const measureContainersSpy = jest.spyOn(ReactSmartSlider.prototype, 'measureContainers') |
| 56 | + const wrapper = shallow(<ReactSmartSlider {...initialProps}/>) |
| 57 | + |
| 58 | + wrapper.find(SecondWrapper).simulate('load') |
| 59 | + |
| 60 | + expect(measureContainersSpy).toHaveBeenCalled() |
| 61 | + }) |
| 62 | + |
| 63 | + it('should invoke addEventListener', () => { |
| 64 | + window.addEventListener = jest.fn() |
| 65 | + |
| 66 | + shallow(<ReactSmartSlider {...initialProps}/>) |
| 67 | + |
| 68 | + expect(window.addEventListener).toHaveBeenCalled() |
| 69 | + }) |
| 70 | + |
| 71 | + it('should invoke removeEventListener', () => { |
| 72 | + window.removeEventListener = jest.fn() |
| 73 | + |
| 74 | + const wrapper = shallow(<ReactSmartSlider {...initialProps}/>) |
| 75 | + |
| 76 | + wrapper.unmount() |
| 77 | + |
| 78 | + expect(window.removeEventListener).toHaveBeenCalled() |
| 79 | + }) |
| 80 | + |
| 81 | + it('should invoke onMouseDown', () => { |
| 82 | + Object.defineProperty(window.navigator, agent, mockConfig('web')) |
| 83 | + |
| 84 | + const onMouseDownSpy = jest.spyOn(ReactSmartSlider.prototype, 'onMouseDown') |
| 85 | + const wrapper = shallow(<ReactSmartSlider {...initialProps}/>) |
| 86 | + |
| 87 | + const event = { |
| 88 | + preventDefault: jest.fn() |
| 89 | + } |
| 90 | + |
| 91 | + wrapper.find(ScrollCircle).simulate('mousedown', event) |
| 92 | + |
| 93 | + expect(onMouseDownSpy).toHaveBeenCalled() |
| 94 | + }) |
| 95 | + |
| 96 | + it('should render CustomScrollbar with props', () => { |
| 97 | + const wrapper = shallow(<ReactSmartSlider {...initialProps}/>) |
| 98 | + const bottom = (initialProps.circleSize - initialProps.barHeight) / 2 |
| 99 | + const style = { |
| 100 | + height: initialProps.barHeight, |
| 101 | + color: initialProps.barColor, |
| 102 | + bottom |
| 103 | + } |
| 104 | + |
| 105 | + expect(wrapper.find(CustomScrollbar).props().style).toEqual(style) |
| 106 | + }) |
| 107 | + |
| 108 | + it('should invoke onScrollbarClick after CustomScrollbar clicked', () => { |
| 109 | + ReactSmartSlider.prototype.onScrollbarClick = jest.fn() |
| 110 | + |
| 111 | + const onScrollbarClickSpy = jest.spyOn(ReactSmartSlider.prototype, 'onScrollbarClick') |
| 112 | + const wrapper = shallow(<ReactSmartSlider {...initialProps}/>) |
| 113 | + const event = { |
| 114 | + clientX: 123 |
| 115 | + } as React.MouseEvent |
| 116 | + |
| 117 | + wrapper.find(CustomScrollbar).simulate('click', event) |
| 118 | + |
| 119 | + expect(onScrollbarClickSpy).toHaveBeenCalled() |
| 120 | + }) |
| 121 | + |
| 122 | + it('should render ScrollCircle with props', () => { |
| 123 | + const wrapper = shallow(<ReactSmartSlider {...initialProps}/>) |
| 124 | + const style = { |
| 125 | + height: initialProps.circleSize, |
| 126 | + width: initialProps.circleSize, |
| 127 | + color: initialProps.circleColor |
| 128 | + } |
| 129 | + |
| 130 | + expect(wrapper.find(ScrollCircle).props().style).toEqual(style) |
| 131 | + }) |
| 132 | +}) |
0 commit comments