Skip to content

Commit 99326a6

Browse files
Add variant parameter to ImageProcessingChecker
To prevent the incorrect variant of an image being used, you can now specify a `variant` in the `ImageProcessingChecker` so that the correct variant of an image will be rendered instead of the previous behaviour (the first non-original image).
1 parent e203c18 commit 99326a6

3 files changed

Lines changed: 26 additions & 4 deletions

File tree

app/assets/javascripts/admin/modules/image-processing-checker.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ window.GOVUK.Modules = window.GOVUK.Modules || {}
99
constructor($root) {
1010
this.$root = $root
1111
this.imageLink = this.$root.dataset.imageLink
12+
this.variant = this.$root.dataset.variant || "original"
1213

1314
this.timeoutDuration =
1415
parseInt(this.$root.dataset.timeoutDuration, 10) ||
@@ -95,13 +96,13 @@ window.GOVUK.Modules = window.GOVUK.Modules || {}
9596

9697
if (imgElement) {
9798
const previewAsset = assets.find(
98-
({ variant }) => variant !== 'original'
99+
({ variant }) => variant === this.variant
99100
)
100101

101102
// images with multiple assets can have
102103
// transformed variants so we should not use
103104
// the `original` asset if this is the case
104-
imgElement.src = previewAsset ? previewAsset.url : assets[0].url
105+
imgElement.src = previewAsset.url
105106
}
106107

107108
this.updateImageStatus(this.imagePreview)

app/components/admin/edition_images/image_component.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<li data-module="image-processing-checker" data-image-link="<%= admin_edition_image_path(edition, image) %>" class="govuk-grid-row">
1+
<li data-module="image-processing-checker" data-image-link="<%= admin_edition_image_path(edition, image) %>" data-variant="s960" class="govuk-grid-row">
22
<template class="js-image-preview">
33
<div class="govuk-grid-column-one-third">
44
<img src="" alt="" class="app-view-edition-resource__preview">

spec/javascripts/admin/modules/image-processing-checker.spec.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ describe('GOVUK.Modules.ImageProcessingChecker', function () {
5555

5656
afterEach(() => imagePreview.remove())
5757

58-
it('should replace the processing status with the image preview', (done) => {
58+
it('should replace the processing status with the original image', (done) => {
5959
spyOn(window, 'fetch').and.resolveTo(okResponse)
6060
// eslint-disable-next-line no-new
6161
new GOVUK.Modules.ImageProcessingChecker(imagePreview)
@@ -64,6 +64,27 @@ describe('GOVUK.Modules.ImageProcessingChecker', function () {
6464

6565
window.setTimeout(() => {
6666
expect(imagePreview.querySelector('img'))
67+
expect(imagePreview.querySelector('img').src).toBe(
68+
'http://assets.gov.uk/media/960x640.png'
69+
)
70+
done()
71+
}, imageProcessingTimeout * 2)
72+
})
73+
74+
it('should replace the processing status with a specific image if `variant` specified', (done) => {
75+
imagePreview.dataset.variant = 's960'
76+
77+
spyOn(window, 'fetch').and.resolveTo(okResponse)
78+
// eslint-disable-next-line no-new
79+
new GOVUK.Modules.ImageProcessingChecker(imagePreview)
80+
81+
expect(imagePreview.querySelector('img')).toBe(null)
82+
83+
window.setTimeout(() => {
84+
expect(imagePreview.querySelector('img'))
85+
expect(imagePreview.querySelector('img').src).toBe(
86+
'http://assets.gov.uk/media/s960_960x640.png'
87+
)
6788
done()
6889
}, imageProcessingTimeout * 2)
6990
})

0 commit comments

Comments
 (0)