@@ -19,10 +19,39 @@ describe('DetailsRenderer', () => {
19
19
let renderer ;
20
20
21
21
function createRenderer ( options ) {
22
- const { document } = new jsdom . JSDOM ( reportAssets . REPORT_TEMPLATES ) . window ;
23
- const dom = new DOM ( document ) ;
22
+ const { window } = new jsdom . JSDOM ( reportAssets . REPORT_TEMPLATES ) ;
23
+ const dom = new DOM ( window . document ) ;
24
24
renderer = new DetailsRenderer ( dom , options ) ;
25
25
renderer . setTemplateContext ( dom . document ( ) ) ;
26
+ global . window = window ;
27
+
28
+ // hack to avoid setting .href directly.
29
+ const anchorElem = window . document . createElement ( 'a' ) ;
30
+ const propDesc = Object . getOwnPropertyDescriptor ( Object . getPrototypeOf ( anchorElem ) , 'href' ) ;
31
+ Object . defineProperty ( Object . getPrototypeOf ( anchorElem ) , 'href' , {
32
+ enumerable : propDesc . enumerable ,
33
+ configurable : propDesc . configurable ,
34
+ get : propDesc . get ,
35
+ set : value => {
36
+ const err = new Error ( ) ;
37
+
38
+ // Was .href set from safelySetHref ?
39
+ let isFound = false ;
40
+ const prevPrepare = Error . prepareStackTrace ;
41
+ Error . prepareStackTrace = ( error , stack ) => {
42
+ isFound = stack . some ( callsite => callsite . getFunctionName ( ) . includes ( 'safelySetHref' ) ) ;
43
+ } ;
44
+ const _ = err . stack ; // Triggers call to prepareStackTrace
45
+ Error . prepareStackTrace = prevPrepare ;
46
+
47
+
48
+ if ( isFound ) {
49
+ propDesc . set . call ( propDesc , value ) ;
50
+ } else {
51
+ throw new Error ( 'Setting .href directly is verboten!' ) ;
52
+ }
53
+ } ,
54
+ } ) ;
26
55
}
27
56
28
57
beforeAll ( ( ) => {
@@ -313,7 +342,7 @@ describe('DetailsRenderer', () => {
313
342
assert . strictEqual ( thumbnailEl . alt , '' ) ;
314
343
} ) ;
315
344
316
- it ( 'renders link values' , ( ) => {
345
+ it . only ( 'renders link values' , ( ) => {
317
346
const linkText = 'Example Site' ;
318
347
const linkUrl = 'https://example.com/' ;
319
348
const link = {
@@ -355,7 +384,7 @@ describe('DetailsRenderer', () => {
355
384
assert . equal ( linkEl . textContent , linkText ) ;
356
385
} ) ;
357
386
358
- it ( 'renders link value as text if URL is invalid' , ( ) => {
387
+ it . only ( 'renders link value as text if URL is invalid' , ( ) => {
359
388
const linkText = 'Invalid Link' ;
360
389
const linkUrl = 'link nonsense' ;
361
390
const link = {
@@ -368,9 +397,9 @@ describe('DetailsRenderer', () => {
368
397
headings : [ { key : 'content' , itemType : 'link' , text : 'Heading' } ] ,
369
398
items : [ { content : link } ] ,
370
399
} ;
371
-
372
400
const el = renderer . render ( details ) ;
373
401
const linkEl = el . querySelector ( 'td.lh-table-column--link > .lh-text' ) ;
402
+
374
403
assert . equal ( linkEl . localName , 'div' ) ;
375
404
assert . equal ( linkEl . textContent , linkText ) ;
376
405
} ) ;
0 commit comments