Skip to content

Commit fbc80e0

Browse files
XhmikosRbryanbraun
authored andcommittedNov 29, 2021
AnchorSpec.js: minor tweaks
* switch to append/remove * use querySelectorAll * use startsWith
1 parent 26c66cf commit fbc80e0

File tree

1 file changed

+36
-36
lines changed

1 file changed

+36
-36
lines changed
 

‎test/spec/AnchorSpec.js

+36-36
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
/* eslint-env jasmine */
1+
/* eslint-env jasmine, node */
22
/* global anchors, AnchorJS */
33

4+
'use strict';
5+
46
describe('AnchorJS', function() {
5-
'use strict';
67
var el1;
78

89
beforeEach(function() {
@@ -15,11 +16,11 @@ describe('AnchorJS', function() {
1516
afterEach(function() {
1617
var baselineStyles = document.querySelector('style.anchorjs');
1718
if (baselineStyles) {
18-
document.head.removeChild(baselineStyles);
19+
baselineStyles.remove();
1920
}
2021

2122
anchors.removeAll();
22-
document.body.removeChild(el1);
23+
el1.remove();
2324
});
2425

2526
it('can detect if an element has an AnchorJS link', function() {
@@ -31,8 +32,8 @@ describe('AnchorJS', function() {
3132
expect(anchors.hasAnchorJSLink(el2)).toBe(false);
3233
expect(anchors.hasAnchorJSLink(el3)).toBe(false);
3334

34-
document.body.removeChild(el2);
35-
document.body.removeChild(el3);
35+
el2.remove();
36+
el3.remove();
3637
});
3738

3839
it('should not add an anchor link to an h1 by default', function() {
@@ -65,11 +66,11 @@ describe('AnchorJS', function() {
6566
expect(anchorLink5).not.toBe(null);
6667
expect(anchorLink6).not.toBe(null);
6768

68-
document.body.removeChild(el2);
69-
document.body.removeChild(el3);
70-
document.body.removeChild(el4);
71-
document.body.removeChild(el5);
72-
document.body.removeChild(el6);
69+
el2.remove();
70+
el3.remove();
71+
el4.remove();
72+
el5.remove();
73+
el6.remove();
7374
});
7475

7576
it('add/remove accepts a string (selector), nodelist, or array of els', function() {
@@ -90,7 +91,7 @@ describe('AnchorJS', function() {
9091
anchors.remove([document.querySelector('h2')]);
9192
expect(anchors.hasAnchorJSLink(el2)).toBe(false);
9293

93-
document.body.removeChild(el2);
94+
el2.remove();
9495
});
9596

9697
it('should set the expected default options', function() {
@@ -151,7 +152,7 @@ describe('AnchorJS', function() {
151152
styleNodes = document.head.querySelectorAll('.anchorjs');
152153
expect(styleNodes.length).toEqual(1);
153154

154-
document.body.removeChild(el2);
155+
el2.remove();
155156
});
156157

157158
it('can remove anchors, using the .remove() method.', function() {
@@ -178,7 +179,7 @@ describe('AnchorJS', function() {
178179
expect(anchors.hasAnchorJSLink(el2)).toBe(false);
179180
expect(anchors.elements.length).toBe(0);
180181

181-
document.body.removeChild(el2);
182+
el2.remove();
182183
});
183184

184185
it('can chain methods.', function() {
@@ -198,18 +199,18 @@ describe('AnchorJS', function() {
198199
id;
199200
anchors.add('h1');
200201
href = document.querySelector('.anchorjs-link').getAttribute('href');
201-
id = document.getElementsByTagName('h1')[0].getAttribute('id');
202+
id = document.querySelector('h1').getAttribute('id');
202203
expect(href).toEqual('#⚡⚡-dont-forget-url-fragments-should-be-i18n-friendly-hyphenated');
203204
expect(id).toEqual('⚡⚡-dont-forget-url-fragments-should-be-i18n-friendly-hyphenated');
204205
});
205206

206207
it('should leave existing IDs in place, and use them as the href for anchors.', function() {
207208
var href,
208209
id;
209-
document.getElementsByTagName('h1')[0].setAttribute('id', 'test-id');
210+
document.querySelector('h1').setAttribute('id', 'test-id');
210211
anchors.add('h1');
211212
href = document.querySelector('.anchorjs-link').getAttribute('href');
212-
id = document.getElementsByTagName('h1')[0].getAttribute('id');
213+
id = document.querySelector('h1').getAttribute('id');
213214
expect(href).toEqual('#test-id');
214215
expect(id).toEqual('test-id');
215216
});
@@ -218,11 +219,11 @@ describe('AnchorJS', function() {
218219
var href,
219220
dataId,
220221
id;
221-
document.getElementsByTagName('h1')[0].setAttribute('data-anchor-id', 'test-id');
222+
document.querySelector('h1').setAttribute('data-anchor-id', 'test-id');
222223
anchors.add('h1');
223224
href = document.querySelector('.anchorjs-link').getAttribute('href');
224-
dataId = document.getElementsByTagName('h1')[0].getAttribute('data-anchor-id');
225-
id = document.getElementsByTagName('h1')[0].getAttribute('id');
225+
dataId = document.querySelector('h1').getAttribute('data-anchor-id');
226+
id = document.querySelector('h1').getAttribute('id');
226227
expect(href).toEqual('#test-id');
227228
expect(dataId).toEqual('test-id');
228229
expect(id).toBe(null);
@@ -242,7 +243,7 @@ describe('AnchorJS', function() {
242243
links;
243244

244245
anchors.add('h2');
245-
tags = document.getElementsByTagName('h2');
246+
tags = document.querySelectorAll('h2');
246247
links = document.querySelectorAll('h2 > .anchorjs-link');
247248
id1 = tags[0].getAttribute('id');
248249
href1 = links[0].getAttribute('href');
@@ -258,9 +259,9 @@ describe('AnchorJS', function() {
258259
expect(id3).toEqual('example-title-2');
259260
expect(href3).toEqual('#example-title-2');
260261

261-
document.body.removeChild(el2);
262-
document.body.removeChild(el3);
263-
document.body.removeChild(el4);
262+
el2.remove();
263+
el3.remove();
264+
el4.remove();
264265
});
265266

266267
it('should create a URL-appropriate href with a custom base', function() {
@@ -295,24 +296,24 @@ describe('AnchorJS', function() {
295296
var anchorHref;
296297

297298
baseEl.setAttribute('href', document.location.hostname);
298-
document.head.appendChild(baseEl);
299+
document.head.append(baseEl);
299300

300301
anchors.add('h1');
301302
anchorHref = document.querySelector('.anchorjs-link').getAttribute('href');
302303

303304
// This produces a full link in the test environment (/context.html#my-id)
304305
// but I'll only check for the first character to ensure I'm not testing
305306
// unimportant parts of the testing framework.
306-
expect(anchorHref.charAt(0)).toEqual('/');
307+
expect(anchorHref.startsWith('/')).toBe(true);
307308

308-
document.head.removeChild(baseEl);
309+
baseEl.remove();
309310
});
310311

311312
it('produces relative anchors if no <base> tag is found', function() {
312313
var anchorHref;
313314
anchors.add('h1');
314315
anchorHref = document.querySelector('.anchorjs-link').getAttribute('href');
315-
expect(anchorHref.charAt(0)).toEqual('#');
316+
expect(anchorHref.startsWith('#')).toBe(true);
316317
});
317318

318319
describe('exposed elements list', function() {
@@ -327,9 +328,9 @@ describe('AnchorJS', function() {
327328
});
328329

329330
afterEach(function() {
330-
document.body.removeChild(el2);
331-
document.body.removeChild(el3);
332-
document.body.removeChild(el4);
331+
el2.remove();
332+
el3.remove();
333+
el4.remove();
333334
});
334335

335336
it('contains added anchors', function() {
@@ -460,15 +461,15 @@ describe('AnchorJS', function() {
460461
it('`left`, places the anchor to the left of the text.', function() {
461462
anchors.options.placement = 'left';
462463
anchors.add('h1');
463-
anchorNode = document.getElementsByTagName('h1')[0].firstChild;
464+
anchorNode = document.querySelector('h1').firstChild;
464465
expect(anchorNode.style.position).toEqual('absolute');
465466
expect(anchorNode.style.marginLeft).toEqual('-1em');
466467
});
467468

468469
it('`right`, places the anchor to the right of the text.', function() {
469470
anchors.options.placement = 'right';
470471
anchors.add('h1');
471-
anchorNode = document.getElementsByTagName('h1')[0].lastChild;
472+
anchorNode = document.querySelector('h1').lastChild;
472473
expect(anchorNode.style.position).toEqual('');
473474
expect(anchorNode.style.marginLeft).toEqual('');
474475
});
@@ -542,15 +543,14 @@ describe('AnchorJS', function() {
542543
* @return {HTMLElement} - The element you've appended.
543544
*/
544545
function appendElementToBody(tagName, text) {
545-
'use strict';
546546
var el = document.createElement(tagName),
547547
textNode;
548548

549549
if (text) {
550550
textNode = document.createTextNode(text);
551-
el.appendChild(textNode);
551+
el.append(textNode);
552552
}
553553

554-
document.body.appendChild(el);
554+
document.body.append(el);
555555
return el;
556556
}

0 commit comments

Comments
 (0)
Please sign in to comment.