Skip to content

Commit cff2511

Browse files
authored
Merge pull request #5253 from alphagov/remove-jquery-from-specs
Remove jQuery from specs
2 parents e7291e9 + 76f83b2 commit cff2511

File tree

2 files changed

+38
-48
lines changed

2 files changed

+38
-48
lines changed

spec/javascripts/components/global-banner-spec.js

Lines changed: 37 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* eslint-env jasmine, jquery */
1+
/* eslint-env jasmine */
22
/* global GOVUK */
33
describe('Global banner module', function () {
44
'use strict'
@@ -11,30 +11,32 @@ describe('Global banner module', function () {
1111
}
1212

1313
beforeEach(function () {
14+
element = document.createElement('div')
15+
element.id = 'global-banner'
16+
element.dataset.module = 'global-banner'
17+
element.dataset.bannerVersion = 5
18+
element.innerHTML = '<a href="/register-to-vote" class="govuk-link js-call-to-action">Register to Vote</a>'
19+
1420
window.GOVUK.setConsentCookie({ settings: true })
1521
document.cookie = 'global_banner_seen=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;'
1622
})
1723

1824
afterEach(function () {
1925
window.GOVUK.setConsentCookie({ settings: null })
20-
$('#global-banner').remove()
26+
element.remove()
2127
})
2228

2329
describe('global banner default', function () {
2430
beforeEach(function () {
25-
element = $(
26-
'<div id="global-banner" data-module="global-banner" data-banner-version="5">' +
27-
'<a href="/register-to-vote" class="govuk-link js-call-to-action">Register to Vote</a>' +
28-
'</div>'
29-
)
31+
document.body.appendChild(element)
3032

3133
document.cookie = 'global_banner_seen=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;'
3234
})
3335

3436
it('sets basic global_banner_seen cookie if not already set', function () {
3537
expect(GOVUK.getCookie('global_banner_seen')).toBeNull()
3638

37-
globalBanner = new GOVUK.Modules.GlobalBanner(element[0])
39+
globalBanner = new GOVUK.Modules.GlobalBanner(element)
3840
globalBanner.init()
3941

4042
expect(parseCookie(GOVUK.getCookie('global_banner_seen')).count).toBe(0)
@@ -44,7 +46,7 @@ describe('Global banner module', function () {
4446
it('sets basic global_banner_seen cookie if existing one is malformed', function () {
4547
GOVUK.setCookie('global_banner_seen', 1)
4648

47-
globalBanner = new GOVUK.Modules.GlobalBanner(element[0])
49+
globalBanner = new GOVUK.Modules.GlobalBanner(element)
4850
globalBanner.init()
4951

5052
expect(parseCookie(GOVUK.getCookie('global_banner_seen')).count).toBe(0)
@@ -54,19 +56,16 @@ describe('Global banner module', function () {
5456

5557
describe('global banner interactions', function () {
5658
beforeEach(function () {
57-
element = $(
58-
'<div id="global-banner" data-module="global-banner" data-banner-version="6">' +
59-
'<a href="/register-to-vote" class="govuk-link js-call-to-action">Register to Vote</a>' +
60-
'</div>'
61-
)
59+
element.dataset.bannerVersion = 6
60+
61+
document.body.appendChild(element)
6262

63-
$(document.body).append(element)
6463
document.cookie = 'global_banner_seen=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;'
6564
})
6665

6766
it('increments view count', function () {
6867
GOVUK.setCookie('global_banner_seen', JSON.stringify({ count: 1, version: 6 }))
69-
globalBanner = new GOVUK.Modules.GlobalBanner(element[0])
68+
globalBanner = new GOVUK.Modules.GlobalBanner(element)
7069
globalBanner.init()
7170

7271
expect(parseCookie(GOVUK.getCookie('global_banner_seen')).count).toBe(2)
@@ -76,35 +75,31 @@ describe('Global banner module', function () {
7675

7776
describe('always on', function () {
7877
beforeEach(function () {
78+
element.dataset.bannerVersion = 6
79+
80+
document.body.appendChild(element)
81+
7982
document.cookie = 'global_banner_seen=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;'
8083
})
8184

8285
it('does not increment view count when on', function () {
83-
element = $(
84-
'<div id="global-banner" data-module="global-banner" data-banner-version="11" data-global-banner-permanent="true">' +
85-
'<a href="/register-to-vote" class="govuk-link js-call-to-action">Register to Vote</a>' +
86-
'</div>'
87-
)
88-
$(document.body).append(element)
86+
element.dataset.bannerVersion = 11
87+
element.dataset.globalBannerPermanent = 'true'
8988

9089
GOVUK.setCookie('global_banner_seen', JSON.stringify({ count: 2, version: 11 }))
91-
globalBanner = new GOVUK.Modules.GlobalBanner(element[0])
90+
globalBanner = new GOVUK.Modules.GlobalBanner(element)
9291
globalBanner.init()
9392

9493
expect(parseCookie(GOVUK.getCookie('global_banner_seen')).count).toBe(2)
9594
expect(parseCookie(GOVUK.getCookie('global_banner_seen')).version).toBe(11)
9695
})
9796

9897
it('continues to increment view count when off', function () {
99-
element = $(
100-
'<div id="global-banner" data-module="global-banner" data-banner-version="11" data-global-banner-permanent="false">' +
101-
'<a href="/register-to-vote" class="govuk-link js-call-to-action">Register to Vote</a>' +
102-
'</div>'
103-
)
104-
$(document.body).append(element)
98+
element.dataset.bannerVersion = 11
99+
element.dataset.globalBannerPermanent = 'false'
105100

106101
GOVUK.setCookie('global_banner_seen', JSON.stringify({ count: 2, version: 11 }))
107-
globalBanner = new GOVUK.Modules.GlobalBanner(element[0])
102+
globalBanner = new GOVUK.Modules.GlobalBanner(element)
108103
globalBanner.init()
109104

110105
expect(parseCookie(GOVUK.getCookie('global_banner_seen')).count).toBe(3)
@@ -114,11 +109,11 @@ describe('Global banner module', function () {
114109

115110
describe('on initialise for cookies', function () {
116111
function expectGlobalBannerToShow () {
117-
expect($('#global-banner').hasClass('gem-c-global-banner--visible')).toBe(true)
112+
expect(document.querySelector('#global-banner')).toHaveClass('gem-c-global-banner--visible')
118113
}
119114

120115
function expectGa4AttributeToExist () {
121-
expect($('#global-banner').attr('data-ga4-global-banner')).toBe('')
116+
expect(document.querySelector('#global-banner').getAttribute('data-ga4-global-banner')).toBe('')
122117
}
123118

124119
function deleteAllCookies () {
@@ -133,19 +128,14 @@ describe('Global banner module', function () {
133128
}
134129

135130
beforeEach(function () {
136-
element = $(
137-
'<div id="global-banner" data-module="global-banner" data-banner-version="5">' +
138-
'<a href="/register-to-vote" class="govuk-link js-call-to-action">Register to Vote</a>' +
139-
'</div>'
140-
)
141-
$('html').append(element)
142-
$('html').removeClass('show-global-banner')
131+
document.body.appendChild(element)
132+
143133
deleteAllCookies()
144134
window.GOVUK.setConsentCookie({ settings: true })
145135
})
146136

147137
it('sets global_banner_seen cookie', function () {
148-
globalBanner = new GOVUK.Modules.GlobalBanner(element[0])
138+
globalBanner = new GOVUK.Modules.GlobalBanner(element)
149139
globalBanner.init()
150140

151141
expect(parseCookie(GOVUK.getCookie('global_banner_seen')).count).toBe(0)
@@ -156,7 +146,7 @@ describe('Global banner module', function () {
156146

157147
it('sets cookie to default value if current cookie is old (prior to versioning mechanism)', function () {
158148
GOVUK.setCookie('global_banner_seen', 1)
159-
globalBanner = new GOVUK.Modules.GlobalBanner(element[0])
149+
globalBanner = new GOVUK.Modules.GlobalBanner(element)
160150
globalBanner.init()
161151

162152
expect(parseCookie(GOVUK.getCookie('global_banner_seen')).count).toBe(0)
@@ -168,7 +158,7 @@ describe('Global banner module', function () {
168158

169159
it('resets cookie if version number is out of date, if count below 3', function () {
170160
GOVUK.setCookie('global_banner_seen', JSON.stringify({ count: 1, version: 1 }))
171-
globalBanner = new GOVUK.Modules.GlobalBanner(element[0])
161+
globalBanner = new GOVUK.Modules.GlobalBanner(element)
172162
globalBanner.init()
173163

174164
expect(parseCookie(GOVUK.getCookie('global_banner_seen')).count).toBe(0)
@@ -179,7 +169,7 @@ describe('Global banner module', function () {
179169

180170
it('resets cookie if version number is out of date, if count above 3', function () {
181171
GOVUK.setCookie('global_banner_seen', JSON.stringify({ count: 10, version: 1 }))
182-
globalBanner = new GOVUK.Modules.GlobalBanner(element[0])
172+
globalBanner = new GOVUK.Modules.GlobalBanner(element)
183173
globalBanner.init()
184174

185175
expect(parseCookie(GOVUK.getCookie('global_banner_seen')).count).toBe(0)
@@ -190,7 +180,7 @@ describe('Global banner module', function () {
190180

191181
it('makes banner visible if view count is less than 3', function () {
192182
GOVUK.setCookie('global_banner_seen', JSON.stringify({ count: 1, version: 5 }))
193-
globalBanner = new GOVUK.Modules.GlobalBanner(element[0])
183+
globalBanner = new GOVUK.Modules.GlobalBanner(element)
194184
globalBanner.init()
195185

196186
expectGlobalBannerToShow()
@@ -199,15 +189,15 @@ describe('Global banner module', function () {
199189

200190
it('keeps the banner hidden if view count is 3 or more', function () {
201191
GOVUK.setCookie('global_banner_seen', JSON.stringify({ count: 3, version: 5 }))
202-
globalBanner = new GOVUK.Modules.GlobalBanner(element[0])
192+
globalBanner = new GOVUK.Modules.GlobalBanner(element)
203193
globalBanner.init()
204194

205-
expect($('#global-banner').hasClass('gem-c-global-banner--visible')).toBe(false)
195+
expect(document.querySelector('#global-banner')).not.toHaveClass('gem-c-global-banner--visible')
206196
})
207197

208198
it('will not set the count higher than 3', function () {
209199
GOVUK.setCookie('global_banner_seen', JSON.stringify({ count: 3, version: 5 }))
210-
globalBanner = new GOVUK.Modules.GlobalBanner(element[0])
200+
globalBanner = new GOVUK.Modules.GlobalBanner(element)
211201
globalBanner.init()
212202

213203
expect(parseCookie(GOVUK.getCookie('global_banner_seen')).count).toBe(3)

spec/javascripts/components/intervention-spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* eslint-env jasmine, jquery */
1+
/* eslint-env jasmine */
22
/* global GOVUK */
33

44
describe('Intervention banner component', function () {

0 commit comments

Comments
 (0)