Skip to content

Instantly share code, notes, and snippets.

@lucassimon
Created June 5, 2019 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucassimon/0fe987e4579058eaf303c67748f22b6f to your computer and use it in GitHub Desktop.
Save lucassimon/0fe987e4579058eaf303c67748f22b6f to your computer and use it in GitHub Desktop.
import React from 'react'
class CtaImage extends React.Component {
constructor(props) {
super(props)
this.state = { image: null }
}
componentDidMount() {
const { ctaImage } = this.props
const image = URL.createObjectURL(ctaImage)
this.setState({ image })
}
componentWillUnmount() {
const { image } = this.state
URL.revokeObjectURL(image)
}
render() {
const { image } = this.state
return (
<div className="cta-image">
<img src={image} />
</div>
)
}
}
export default CtaImage
import React from 'react'
import {shallow} from 'enzyme'
import CtaImage from '../CtaImage'
jest.mock('../../../../../../i18next/index')
const getFakeDropzoneFile = (name = 'fake-file.jpg', size = 16, type = 'image/jpeg') => {
const blob = new Blob([''], { type })
blob.name = name
Object.defineProperty(blob, 'size', {
get() {
return size
},
})
return blob
}
const getWrapper = () => {
const props = {
ctaImage: getFakeDropzoneFile(),
}
const wrapper = shallow(<CtaImage {...props} />)
return { wrapper, props }
}
describe('CtaImage component', () => {
beforeEach(() => {
global.URL.createObjectURL = jest.fn(obj => `preview-${obj.name}`)
})
it('should render the image', () => {
const { wrapper } = getWrapper()
expect(wrapper).toMatchSnapshot()
})
xit('should create an Image Blob object in state when did mount component', () => { })
xit('should destroy an Image Blob object when unmount the component', () => { })
})
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`CtaImage component should render the image 1`] = `
<div
className="cta-image"
>
<img
src="preview-fake-file.jpg"
/>
</div>
`;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment