|
1 | | -import React from 'react' |
2 | | -import Image from '..' |
3 | | -import { shallow } from 'enzyme' |
4 | 1 |
|
5 | | -describe('Image', () => { |
6 | | - it('Image Mounted', () => { |
7 | | - const result = shallow(<Image src='test.png'/>) |
8 | | - expect(result.instance().props.src).toEqual('test.png') |
| 2 | +import React from 'react' |
| 3 | +import ImageComponent from '..' |
| 4 | +import { shallow, mount } from 'enzyme' |
| 5 | +import ErrorIcon from '../../ErrorIcon' |
| 6 | +function getComp(prop) { |
| 7 | + return <ImageComponent src="test.png" {...prop}/> |
| 8 | +} |
| 9 | +describe('test when IntersectionObserver is false', () => { |
| 10 | + it('Image set src prop', () => { |
| 11 | + const result = mount(getComp()) |
| 12 | + expect(result.props().src).toEqual('test.png') |
| 13 | + result.setProps({ src: 'test2.png' }) |
| 14 | + expect(result.props().src).toEqual('test2.png') |
| 15 | + }) |
| 16 | + it('test onClick', () => { |
| 17 | + const onClick = jest.fn().mockReturnValue(null) |
| 18 | + const result = shallow(getComp({onClick})) |
| 19 | + result.find('img').simulate('click') |
| 20 | + expect(onClick).toBeCalledTimes(1) |
| 21 | + }) |
| 22 | + it('test onLoad, and LoadingIcon should hide', () => { |
| 23 | + const onLoad = jest.fn().mockReturnValue(null) |
| 24 | + const result = mount(getComp({onLoad})) |
| 25 | + expect(result.instance().state.isLoading).toBe(true) |
| 26 | + result.find('img').simulate('load') |
| 27 | + expect(onLoad).toBeCalledTimes(1) |
| 28 | + expect(result.instance().state.isLoading).toBe(false) |
| 29 | + expect(result.find('.mask-loading').length).toEqual(0) |
| 30 | + }) |
| 31 | + it('test onError, and ErrorIcon should show', () => { |
| 32 | + const onError = jest.fn().mockReturnValue(null) |
| 33 | + const result = mount(getComp({onError})) |
| 34 | + expect(result.instance().state.isError).toBe(false) |
| 35 | + result.find('img').simulate('error') |
| 36 | + expect(onError).toBeCalledTimes(1) |
| 37 | + expect(result.instance().state.isError).toBe(true) |
| 38 | + expect(result.find(ErrorIcon).length).toEqual(1) |
| 39 | + }) |
| 40 | + it('test delete', () => { |
| 41 | + const onDelete = jest.fn().mockReturnValue(null) |
| 42 | + const result = mount(getComp({onDelete})) |
| 43 | + result.find('.react-image-icon').simulate('click') |
| 44 | + expect(onDelete).toBeCalledTimes(1) |
| 45 | + }) |
| 46 | + it('test group & preview prop', () => { |
| 47 | + const group = '100' |
| 48 | + const result = mount(getComp({group})) |
| 49 | + expect(result.find('.mask-img').prop('data-img-group')).toEqual('100') |
| 50 | + }) |
| 51 | + it('data-index-group is "null" when preview is false', () => { |
| 52 | + const result = mount( |
| 53 | + getComp({group: '100', preview: false}) |
| 54 | + ) |
| 55 | + expect(result.find('.mask-img').prop('data-img-group')).toEqual(null) |
| 56 | + }) |
| 57 | + it('test className prop', () => { |
| 58 | + const className = 'test' |
| 59 | + const result = shallow( |
| 60 | + getComp({className}) |
| 61 | + ) |
| 62 | + expect(result.find('.mask-img').hasClass('test')).toBe(true) |
| 63 | + }) |
| 64 | + it('test "style", "width" and "height" props', () => { |
| 65 | + const style = { background: 'red', testStyle: '1' } |
| 66 | + let result = shallow( |
| 67 | + getComp({width: 120, style}) |
| 68 | + ) |
| 69 | + expect(result.instance().style).toEqual({ |
| 70 | + width: '120px', |
| 71 | + height: 'initial', |
| 72 | + background: 'red', |
| 73 | + testStyle: '1' |
| 74 | + }) |
| 75 | + result = shallow(getComp({height: 100})) |
| 76 | + expect(result.instance().style).toEqual({ |
| 77 | + width: '100px', |
| 78 | + height: '100px' |
| 79 | + }) |
| 80 | + }) |
| 81 | + it('test img style, style.display is "none" when "isLoading" or "isError" is true', () => { |
| 82 | + const imgStyle = { testImgStyle: 'test' } |
| 83 | + const result = shallow( |
| 84 | + getComp({ |
| 85 | + imgProps: {style: imgStyle}, |
| 86 | + objectFit: 'scale-down' |
| 87 | + }) |
| 88 | + ) |
| 89 | + const commonExpect = { |
| 90 | + objectFit: 'scale-down', |
| 91 | + testImgStyle: 'test' |
| 92 | + } |
| 93 | + expect(result.instance().imgStyle).toEqual({ |
| 94 | + display: 'none', |
| 95 | + ...commonExpect |
| 96 | + }) |
| 97 | + result.find('img').simulate('load') |
| 98 | + expect(result.instance().imgStyle).toEqual({ |
| 99 | + display: '', |
| 100 | + ...commonExpect |
| 101 | + }) |
| 102 | + }) |
| 103 | + it('test img prop', () => { |
| 104 | + const imgProps = { 'data-index': 10 } |
| 105 | + const result = shallow(getComp({imgProps})) |
| 106 | + expect(result.find('img').prop('data-index')).toEqual(10) |
| 107 | + }) |
| 108 | + it('test mask prop', () => { |
| 109 | + const mask = false |
| 110 | + const result = shallow(getComp({mask})) |
| 111 | + expect(result.find('.mask-img').hasClass('mask')).toBeFalsy() |
| 112 | + }) |
| 113 | + it('test refDom', () => { |
| 114 | + const result = mount(<ImageComponent src="test.png" />) |
| 115 | + expect(result.instance().refDom.current).toBeTruthy() |
9 | 116 | }) |
10 | 117 | }) |
0 commit comments