diff --git a/components/image/__tests__/index.test.tsx b/components/image/__tests__/index.test.tsx index 62c7200..667a9f6 100644 --- a/components/image/__tests__/index.test.tsx +++ b/components/image/__tests__/index.test.tsx @@ -2,6 +2,7 @@ import React from 'react' import { mount } from 'enzyme' import { Image } from 'components' import { updateWrapper } from 'tests/utils' +import { act } from 'react-dom/test-utils' const url = 'data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUA' + 'AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO' + @@ -12,44 +13,44 @@ describe('Image', () => { let wrapper = mount() expect(wrapper).toMatchSnapshot() expect(() => wrapper.unmount()).not.toThrow() - + wrapper = mount() wrapper.find('img').at(0).simulate('load') expect(wrapper).toMatchSnapshot() expect(() => wrapper.unmount()).not.toThrow() - - wrapper = mount() + + wrapper = mount() wrapper.find('img').at(0).simulate('load') expect(wrapper).toMatchSnapshot() expect(() => wrapper.unmount()).not.toThrow() }) - + it('should work correctly with skeleton', async () => { let wrapper = mount() expect(wrapper.find('.skeleton').length).not.toBe(0) - + wrapper = mount() wrapper.find('img').at(0).simulate('load') await updateWrapper(wrapper) expect(wrapper.find('.skeleton').length).not.toBe(0) - + wrapper = mount() expect(wrapper.find('.skeleton').length).toBe(0) - - mount() + + mount() expect(wrapper.find('.skeleton').length).toBe(0) }) - + it('should remove skeleton when timeout', async () => { const animation = mount() - const NoAnimation = mount() + const NoAnimation = mount() expect(animation.find('.skeleton').length).not.toBe(0) await updateWrapper(animation, 300) await updateWrapper(NoAnimation, 300) expect(animation.find('.skeleton').length).toBe(0) expect(NoAnimation.find('.skeleton').length).toBe(0) }) - + it('should remove skeleton when image completed', async () => { Object.defineProperty((global as any).Image.prototype, 'complete', { get() { @@ -63,4 +64,28 @@ describe('Image', () => { expect((img.getDOMNode() as HTMLImageElement).complete).toEqual(true) expect(wrapper.find('.skeleton').length).toBe(0) }) + + it('should zooming image when width so small', async () => { + window.getComputedStyle = jest.fn() + .mockImplementation(() => ({ + width: '10px', + })) + const wrapper = mount( +
+ ) + expect(wrapper.find('.image').html()).toContain('height: auto;') + ;(window.getComputedStyle as jest.Mock).mockRestore() + + window.getComputedStyle = jest.fn() + .mockImplementation(() => ({ + width: '110px', + })) + act(() => { + window.dispatchEvent(new Event('resize')) + }) + await updateWrapper(wrapper) + + expect(wrapper.find('.image').html()).not.toContain('height: auto;') + ;(window.getComputedStyle as jest.Mock).mockRestore() + }) })