-
Notifications
You must be signed in to change notification settings - Fork 197
Expand file tree
/
Copy pathimage-processing-checker.spec.js
More file actions
155 lines (130 loc) · 4.37 KB
/
image-processing-checker.spec.js
File metadata and controls
155 lines (130 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
describe('GOVUK.Modules.ImageProcessingChecker', function () {
let imagePreview
const imageProcessingTimeout = 1
const responseJSON = {
image_data: {
assets: [
{
variant: 'original',
url: 'http://assets.gov.uk/media/960x640.png'
},
{
variant: 's960',
url: 'http://assets.gov.uk/media/s960_960x640.png'
}
],
all_assets_uploaded: true
}
}
const notReadyJSON = JSON.parse(JSON.stringify(responseJSON))
notReadyJSON.image_data.all_assets_uploaded = false
const okResponse = new Response(JSON.stringify(responseJSON), {
status: 200,
statusText: 'OK'
})
const okNotReadyResponse = new Response(JSON.stringify(notReadyJSON), {
status: 200,
statusText: 'OK'
})
beforeEach(() => {
imagePreview = document.createElement('div')
imagePreview.dataset.module = 'image-processing-checker'
imagePreview.dataset.imageLink = 'https://placeholder.com/image/1'
imagePreview.dataset.timeoutDuration = imageProcessingTimeout
imagePreview.innerHTML = `
<template class="js-image-preview">
<img src="" />
</template>
<div class="js-image-processing-status">
<span class="js-image-processing-preview-tag">
PROCESSING
</span>
</div>
<template class="js-image-preview-failure">
<span class="js-image-processing-failure-tag">
FAILED
</span>
</template>
`
document.body.appendChild(imagePreview)
})
afterEach(() => imagePreview.remove())
it('should replace the processing status with the original image', (done) => {
spyOn(window, 'fetch').and.resolveTo(okResponse)
// eslint-disable-next-line no-new
new GOVUK.Modules.ImageProcessingChecker(imagePreview)
expect(imagePreview.querySelector('img')).toBe(null)
window.setTimeout(() => {
expect(imagePreview.querySelector('img'))
expect(imagePreview.querySelector('img').src).toBe(
'http://assets.gov.uk/media/960x640.png'
)
done()
}, imageProcessingTimeout * 2)
})
it('should replace the processing status with a specific image if `variant` specified', (done) => {
imagePreview.dataset.variant = 's960'
spyOn(window, 'fetch').and.resolveTo(okResponse)
// eslint-disable-next-line no-new
new GOVUK.Modules.ImageProcessingChecker(imagePreview)
expect(imagePreview.querySelector('img')).toBe(null)
window.setTimeout(() => {
expect(imagePreview.querySelector('img'))
expect(imagePreview.querySelector('img').src).toBe(
'http://assets.gov.uk/media/s960_960x640.png'
)
done()
}, imageProcessingTimeout * 2)
})
it('should replace the processing status with the image preview after multiple attempts', (done) => {
let calls = 4
const responder = () => {
if (calls > 0) {
calls -= 1
return new Promise((resolve) => {
resolve(okNotReadyResponse)
})
} else {
return new Promise((resolve) => {
resolve(okResponse)
})
}
}
spyOn(window, 'fetch').and.callFake(responder)
// eslint-disable-next-line no-new
new GOVUK.Modules.ImageProcessingChecker(imagePreview)
expect(imagePreview.querySelector('img')).toBe(null)
window.setTimeout(() => {
expect(imagePreview.querySelector('img')).toBeTruthy()
done()
}, 100)
})
it('should render error if URL does not return 200', (done) => {
const errorResponse = new Response(JSON.stringify(responseJSON), {
status: 404,
statusText: 'OK'
})
spyOn(window, 'fetch').and.resolveTo(errorResponse)
// eslint-disable-next-line no-new
new GOVUK.Modules.ImageProcessingChecker(imagePreview)
expect(imagePreview.querySelector('img')).toBe(null)
window.setTimeout(() => {
expect(
imagePreview.querySelector('.js-image-processing-failure-tag')
).toBeTruthy()
done()
}, 100)
})
it('should render error if image is not ready in time', (done) => {
spyOn(window, 'fetch').and.resolveTo(okNotReadyResponse)
// eslint-disable-next-line no-new
new GOVUK.Modules.ImageProcessingChecker(imagePreview)
expect(imagePreview.querySelector('img')).toBe(null)
window.setTimeout(() => {
expect(
imagePreview.querySelector('.js-image-processing-failure-tag')
).toBeTruthy()
done()
}, 100)
})
})