fix: Prever FileReader over URL.createObjectUrl

Apparently, URL.createObjectUrl is not supported in JSDom, see:
https://github.com/jsdom/jsdom/issues/1721

With FileReader we have the option to avoid it altogether.
This commit is contained in:
roschaefer 2020-02-20 15:42:47 +01:00
parent 9c98e30bb8
commit 9241305d43
3 changed files with 13 additions and 3 deletions

View File

@ -233,10 +233,13 @@ describe('ContributionForm.vue', () => {
}) })
it('supports adding a teaser image', async () => { it('supports adding a teaser image', async () => {
const spy = jest.spyOn(FileReader.prototype, 'readAsDataURL').mockImplementation(() => {})
expectedParams.variables.imageUpload = imageUpload expectedParams.variables.imageUpload = imageUpload
wrapper.find(ImageUploader).vm.$emit('addHeroImage', imageUpload) wrapper.find(ImageUploader).vm.$emit('addHeroImage', imageUpload)
await wrapper.find('form').trigger('submit') await wrapper.find('form').trigger('submit')
expect(mocks.$apollo.mutate).toHaveBeenCalledWith(expect.objectContaining(expectedParams)) expect(mocks.$apollo.mutate).toHaveBeenCalledWith(expect.objectContaining(expectedParams))
expect(spy).toHaveBeenCalledWith(imageUpload)
spy.mockReset()
}) })
it('content is valid with just a link', async () => { it('content is valid with just a link', async () => {

View File

@ -194,8 +194,15 @@ export default {
this.$refs.contributionForm.update('content', value) this.$refs.contributionForm.update('content', value)
}, },
addHeroImage(file) { addHeroImage(file) {
this.imageUpload = file this.formData.image = null
this.formData.image = file ? URL.createObjectURL(file) : null if (file) {
const reader = new FileReader()
reader.onload = ({ target }) => {
this.formData.image = target.result
}
this.imageUpload = file
reader.readAsDataURL(file)
}
}, },
addImageAspectRatio(aspectRatio) { addImageAspectRatio(aspectRatio) {
this.formData.imageAspectRatio = aspectRatio this.formData.imageAspectRatio = aspectRatio

View File

@ -66,7 +66,7 @@ describe('PostTeaser', () => {
} }
it('has no validation errors', () => { it('has no validation errors', () => {
const spy = jest.spyOn(global.console, 'error'); const spy = jest.spyOn(global.console, 'error')
Wrapper() Wrapper()
expect(spy).not.toBeCalled() expect(spy).not.toBeCalled()
spy.mockReset() spy.mockReset()