1
- /* eslint-env jasmine */
1
+ /* eslint-env jasmine, node */
2
2
/* global anchors, AnchorJS */
3
3
4
+ 'use strict' ;
5
+
4
6
describe ( 'AnchorJS' , function ( ) {
5
- 'use strict' ;
6
7
var el1 ;
7
8
8
9
beforeEach ( function ( ) {
@@ -15,11 +16,11 @@ describe('AnchorJS', function() {
15
16
afterEach ( function ( ) {
16
17
var baselineStyles = document . querySelector ( 'style.anchorjs' ) ;
17
18
if ( baselineStyles ) {
18
- document . head . removeChild ( baselineStyles ) ;
19
+ baselineStyles . remove ( ) ;
19
20
}
20
21
21
22
anchors . removeAll ( ) ;
22
- document . body . removeChild ( el1 ) ;
23
+ el1 . remove ( ) ;
23
24
} ) ;
24
25
25
26
it ( 'can detect if an element has an AnchorJS link' , function ( ) {
@@ -31,8 +32,8 @@ describe('AnchorJS', function() {
31
32
expect ( anchors . hasAnchorJSLink ( el2 ) ) . toBe ( false ) ;
32
33
expect ( anchors . hasAnchorJSLink ( el3 ) ) . toBe ( false ) ;
33
34
34
- document . body . removeChild ( el2 ) ;
35
- document . body . removeChild ( el3 ) ;
35
+ el2 . remove ( ) ;
36
+ el3 . remove ( ) ;
36
37
} ) ;
37
38
38
39
it ( 'should not add an anchor link to an h1 by default' , function ( ) {
@@ -65,11 +66,11 @@ describe('AnchorJS', function() {
65
66
expect ( anchorLink5 ) . not . toBe ( null ) ;
66
67
expect ( anchorLink6 ) . not . toBe ( null ) ;
67
68
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 ( ) ;
73
74
} ) ;
74
75
75
76
it ( 'add/remove accepts a string (selector), nodelist, or array of els' , function ( ) {
@@ -90,7 +91,7 @@ describe('AnchorJS', function() {
90
91
anchors . remove ( [ document . querySelector ( 'h2' ) ] ) ;
91
92
expect ( anchors . hasAnchorJSLink ( el2 ) ) . toBe ( false ) ;
92
93
93
- document . body . removeChild ( el2 ) ;
94
+ el2 . remove ( ) ;
94
95
} ) ;
95
96
96
97
it ( 'should set the expected default options' , function ( ) {
@@ -151,7 +152,7 @@ describe('AnchorJS', function() {
151
152
styleNodes = document . head . querySelectorAll ( '.anchorjs' ) ;
152
153
expect ( styleNodes . length ) . toEqual ( 1 ) ;
153
154
154
- document . body . removeChild ( el2 ) ;
155
+ el2 . remove ( ) ;
155
156
} ) ;
156
157
157
158
it ( 'can remove anchors, using the .remove() method.' , function ( ) {
@@ -178,7 +179,7 @@ describe('AnchorJS', function() {
178
179
expect ( anchors . hasAnchorJSLink ( el2 ) ) . toBe ( false ) ;
179
180
expect ( anchors . elements . length ) . toBe ( 0 ) ;
180
181
181
- document . body . removeChild ( el2 ) ;
182
+ el2 . remove ( ) ;
182
183
} ) ;
183
184
184
185
it ( 'can chain methods.' , function ( ) {
@@ -198,18 +199,18 @@ describe('AnchorJS', function() {
198
199
id ;
199
200
anchors . add ( 'h1' ) ;
200
201
href = document . querySelector ( '.anchorjs-link' ) . getAttribute ( 'href' ) ;
201
- id = document . getElementsByTagName ( 'h1' ) [ 0 ] . getAttribute ( 'id' ) ;
202
+ id = document . querySelector ( 'h1' ) . getAttribute ( 'id' ) ;
202
203
expect ( href ) . toEqual ( '#⚡⚡-dont-forget-url-fragments-should-be-i18n-friendly-hyphenated' ) ;
203
204
expect ( id ) . toEqual ( '⚡⚡-dont-forget-url-fragments-should-be-i18n-friendly-hyphenated' ) ;
204
205
} ) ;
205
206
206
207
it ( 'should leave existing IDs in place, and use them as the href for anchors.' , function ( ) {
207
208
var href ,
208
209
id ;
209
- document . getElementsByTagName ( 'h1' ) [ 0 ] . setAttribute ( 'id' , 'test-id' ) ;
210
+ document . querySelector ( 'h1' ) . setAttribute ( 'id' , 'test-id' ) ;
210
211
anchors . add ( 'h1' ) ;
211
212
href = document . querySelector ( '.anchorjs-link' ) . getAttribute ( 'href' ) ;
212
- id = document . getElementsByTagName ( 'h1' ) [ 0 ] . getAttribute ( 'id' ) ;
213
+ id = document . querySelector ( 'h1' ) . getAttribute ( 'id' ) ;
213
214
expect ( href ) . toEqual ( '#test-id' ) ;
214
215
expect ( id ) . toEqual ( 'test-id' ) ;
215
216
} ) ;
@@ -218,11 +219,11 @@ describe('AnchorJS', function() {
218
219
var href ,
219
220
dataId ,
220
221
id ;
221
- document . getElementsByTagName ( 'h1' ) [ 0 ] . setAttribute ( 'data-anchor-id' , 'test-id' ) ;
222
+ document . querySelector ( 'h1' ) . setAttribute ( 'data-anchor-id' , 'test-id' ) ;
222
223
anchors . add ( 'h1' ) ;
223
224
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' ) ;
226
227
expect ( href ) . toEqual ( '#test-id' ) ;
227
228
expect ( dataId ) . toEqual ( 'test-id' ) ;
228
229
expect ( id ) . toBe ( null ) ;
@@ -242,7 +243,7 @@ describe('AnchorJS', function() {
242
243
links ;
243
244
244
245
anchors . add ( 'h2' ) ;
245
- tags = document . getElementsByTagName ( 'h2' ) ;
246
+ tags = document . querySelectorAll ( 'h2' ) ;
246
247
links = document . querySelectorAll ( 'h2 > .anchorjs-link' ) ;
247
248
id1 = tags [ 0 ] . getAttribute ( 'id' ) ;
248
249
href1 = links [ 0 ] . getAttribute ( 'href' ) ;
@@ -258,9 +259,9 @@ describe('AnchorJS', function() {
258
259
expect ( id3 ) . toEqual ( 'example-title-2' ) ;
259
260
expect ( href3 ) . toEqual ( '#example-title-2' ) ;
260
261
261
- document . body . removeChild ( el2 ) ;
262
- document . body . removeChild ( el3 ) ;
263
- document . body . removeChild ( el4 ) ;
262
+ el2 . remove ( ) ;
263
+ el3 . remove ( ) ;
264
+ el4 . remove ( ) ;
264
265
} ) ;
265
266
266
267
it ( 'should create a URL-appropriate href with a custom base' , function ( ) {
@@ -295,24 +296,24 @@ describe('AnchorJS', function() {
295
296
var anchorHref ;
296
297
297
298
baseEl . setAttribute ( 'href' , document . location . hostname ) ;
298
- document . head . appendChild ( baseEl ) ;
299
+ document . head . append ( baseEl ) ;
299
300
300
301
anchors . add ( 'h1' ) ;
301
302
anchorHref = document . querySelector ( '.anchorjs-link' ) . getAttribute ( 'href' ) ;
302
303
303
304
// This produces a full link in the test environment (/context.html#my-id)
304
305
// but I'll only check for the first character to ensure I'm not testing
305
306
// unimportant parts of the testing framework.
306
- expect ( anchorHref . charAt ( 0 ) ) . toEqual ( '/' ) ;
307
+ expect ( anchorHref . startsWith ( '/' ) ) . toBe ( true ) ;
307
308
308
- document . head . removeChild ( baseEl ) ;
309
+ baseEl . remove ( ) ;
309
310
} ) ;
310
311
311
312
it ( 'produces relative anchors if no <base> tag is found' , function ( ) {
312
313
var anchorHref ;
313
314
anchors . add ( 'h1' ) ;
314
315
anchorHref = document . querySelector ( '.anchorjs-link' ) . getAttribute ( 'href' ) ;
315
- expect ( anchorHref . charAt ( 0 ) ) . toEqual ( '#' ) ;
316
+ expect ( anchorHref . startsWith ( '#' ) ) . toBe ( true ) ;
316
317
} ) ;
317
318
318
319
describe ( 'exposed elements list' , function ( ) {
@@ -327,9 +328,9 @@ describe('AnchorJS', function() {
327
328
} ) ;
328
329
329
330
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 ( ) ;
333
334
} ) ;
334
335
335
336
it ( 'contains added anchors' , function ( ) {
@@ -460,15 +461,15 @@ describe('AnchorJS', function() {
460
461
it ( '`left`, places the anchor to the left of the text.' , function ( ) {
461
462
anchors . options . placement = 'left' ;
462
463
anchors . add ( 'h1' ) ;
463
- anchorNode = document . getElementsByTagName ( 'h1' ) [ 0 ] . firstChild ;
464
+ anchorNode = document . querySelector ( 'h1' ) . firstChild ;
464
465
expect ( anchorNode . style . position ) . toEqual ( 'absolute' ) ;
465
466
expect ( anchorNode . style . marginLeft ) . toEqual ( '-1em' ) ;
466
467
} ) ;
467
468
468
469
it ( '`right`, places the anchor to the right of the text.' , function ( ) {
469
470
anchors . options . placement = 'right' ;
470
471
anchors . add ( 'h1' ) ;
471
- anchorNode = document . getElementsByTagName ( 'h1' ) [ 0 ] . lastChild ;
472
+ anchorNode = document . querySelector ( 'h1' ) . lastChild ;
472
473
expect ( anchorNode . style . position ) . toEqual ( '' ) ;
473
474
expect ( anchorNode . style . marginLeft ) . toEqual ( '' ) ;
474
475
} ) ;
@@ -542,15 +543,14 @@ describe('AnchorJS', function() {
542
543
* @return {HTMLElement } - The element you've appended.
543
544
*/
544
545
function appendElementToBody ( tagName , text ) {
545
- 'use strict' ;
546
546
var el = document . createElement ( tagName ) ,
547
547
textNode ;
548
548
549
549
if ( text ) {
550
550
textNode = document . createTextNode ( text ) ;
551
- el . appendChild ( textNode ) ;
551
+ el . append ( textNode ) ;
552
552
}
553
553
554
- document . body . appendChild ( el ) ;
554
+ document . body . append ( el ) ;
555
555
return el ;
556
556
}
0 commit comments