1+ /* eslint-disable no-unused-expressions */
2+
13import { readFile } from '@web/test-runner-commands' ;
24import { expect } from '@esm-bundle/chai' ;
35import {
4- config ,
5- decorateAnchors ,
6- decorateBlocks ,
7- getCurrentDomain ,
8- loadBlocks ,
9- loadElement ,
10- loadScript ,
11- loadStyle ,
12- loadTemplate ,
13- setLCPTrigger ,
6+ config ,
7+ decorateAnchors ,
8+ decorateBlocks ,
9+ getCurrentDomain ,
10+ loadBlocks ,
11+ loadElement ,
12+ loadScript ,
13+ loadStyle ,
14+ loadTemplate ,
15+ setLCPTrigger ,
1416} from '../scripts.js' ;
1517import getObjectProperty from '../utils/property.js' ;
18+
1619const ms = 100 ;
1720
1821const mock = await readFile ( { path : './scripts.mock.html' } ) ;
1922document . body . innerHTML = mock ;
2023
2124describe ( 'Anchors' , ( ) => {
22- const parent = document . querySelector ( '.anchors' ) ;
23- const anchors = decorateAnchors ( parent ) ;
24- it ( 'url maps to localhost' , ( ) => {
25- expect ( anchors [ 0 ] . href ) . to . equal ( 'http://localhost:2000/my-content' ) ;
26- } ) ;
27-
28- it ( 'url does not map to localhost' , ( ) => {
29- expect ( anchors [ 1 ] . href ) . to . equal ( 'https://www.adobe.com/' ) ;
30- } ) ;
31-
32- it ( 'svg image will unwrap anchor' , ( ) => {
33- const svg = parent . querySelector ( ':scope > img' ) ;
34- expect ( svg ) . to . exist ;
35- } ) ;
36-
37- it ( 'svg image will keep anchor' , ( ) => {
38- const svgAnchor = anchors . pop ( ) ;
39- const svg = parent . querySelector ( ':scope > a > img' ) ;
40- expect ( svg ) . to . exist ;
41- expect ( svgAnchor . href ) . to . equal ( 'http://localhost:2000/my-awesome-link' ) ;
42- } ) ;
43-
44- it ( 'domain respects port' , ( ) => {
45- const location = { protocol : 'http:' , hostname : 'localhost' }
46- const currentDomain = getCurrentDomain ( location ) ;
47- expect ( currentDomain ) . to . equal ( 'http://localhost' ) ;
48- } ) ;
25+ const parent = document . querySelector ( '.anchors' ) ;
26+ const anchors = decorateAnchors ( parent ) ;
27+ it ( 'url maps to localhost' , ( ) => {
28+ expect ( anchors [ 0 ] . href ) . to . equal ( 'http://localhost:2000/my-content' ) ;
29+ } ) ;
30+
31+ it ( 'url does not map to localhost' , ( ) => {
32+ expect ( anchors [ 1 ] . href ) . to . equal ( 'https://www.adobe.com/' ) ;
33+ } ) ;
34+
35+ it ( 'svg image will unwrap anchor' , ( ) => {
36+ const svg = parent . querySelector ( ':scope > img' ) ;
37+ expect ( svg ) . to . exist ;
38+ } ) ;
39+
40+ it ( 'svg image will keep anchor' , ( ) => {
41+ const svgAnchor = anchors . pop ( ) ;
42+ const svg = parent . querySelector ( ':scope > a > img' ) ;
43+ expect ( svg ) . to . exist ;
44+ expect ( svgAnchor . href ) . to . equal ( 'http://localhost:2000/my-awesome-link' ) ;
45+ } ) ;
46+
47+ it ( 'domain respects port' , ( ) => {
48+ const location = { protocol : 'http:' , hostname : 'localhost' } ;
49+ const currentDomain = getCurrentDomain ( location ) ;
50+ expect ( currentDomain ) . to . equal ( 'http://localhost' ) ;
51+ } ) ;
4952} ) ;
5053
5154describe ( 'Block variations' , ( ) => {
52- const parent = document . querySelector ( '.variations' ) ;
53- const blocks = decorateBlocks ( parent ) ;
54- it ( 'url maps to localhost' , ( ) => {
55- expect ( blocks [ 0 ] . classList . contains ( 'marquee' ) ) . to . be . true ;
56- } ) ;
55+ const parent = document . querySelector ( '.variations' ) ;
56+ const blocks = decorateBlocks ( parent ) ;
57+ it ( 'url maps to localhost' , ( ) => {
58+ expect ( blocks [ 0 ] . classList . contains ( 'marquee' ) ) . to . be . true ;
59+ } ) ;
5760} ) ;
5861
5962describe ( 'Script loading' , async ( ) => {
60- function callback ( ) { window . scriptCallback = true ; } ;
61- const script = await loadScript ( '/__tests__/block.mock.js' , callback , 'module' ) ;
62- it ( 'script element exists' , ( ) => {
63- expect ( script ) . to . exist ;
64- } ) ;
65-
66- it ( 'script calls back' , async ( ) => {
67- const loaded = await getObjectProperty ( 'scriptCallback' , ms ) ;
68- expect ( loaded ) . to . be . true ;
69- } ) ;
70-
71- it ( 'block mock can run' , async ( ) => {
72- const loaded = await getObjectProperty ( 'feature.loaded' , ms ) ;
73- expect ( loaded ) . to . be . true ;
74- } ) ;
63+ function callback ( ) { window . scriptCallback = true ; }
64+ const script = await loadScript ( '/__tests__/block.mock.js' , callback , 'module' ) ;
65+ it ( 'script element exists' , ( ) => {
66+ expect ( script ) . to . exist ;
67+ } ) ;
68+
69+ it ( 'script calls back' , async ( ) => {
70+ const loaded = await getObjectProperty ( 'scriptCallback' , ms ) ;
71+ expect ( loaded ) . to . be . true ;
72+ } ) ;
73+
74+ it ( 'block mock can run' , async ( ) => {
75+ const loaded = await getObjectProperty ( 'feature.loaded' , ms ) ;
76+ expect ( loaded ) . to . be . true ;
77+ } ) ;
7578} ) ;
7679
7780describe ( 'Style loading' , async ( ) => {
78- function callback ( ) { window . styleCallback = true ; } ;
79- function callbackTwo ( ) { window . styleCallbackTwo = true ; } ;
80- const style = await loadStyle ( '/__tests__/block.mock.css' , callback ) ;
81- it ( 'style element exists' , ( ) => {
82- expect ( style ) . to . exist ;
83- } ) ;
84-
85- it ( 'style calls back' , async ( ) => {
86- const loaded = await getObjectProperty ( 'styleCallback' , ms ) ;
87- expect ( loaded ) . to . be . true ;
88- } ) ;
89-
90- it ( 'only one style' , async ( ) => {
91- const style = await loadStyle ( '/__tests__/block.mock.css' , callbackTwo ) ;
92- expect ( style ) . to . exist ;
93- expect ( window . styleCallbackTwo ) . to . be . true ;
94- } ) ;
81+ function callback ( ) { window . styleCallback = true ; }
82+ function callbackTwo ( ) { window . styleCallbackTwo = true ; }
83+ const style = await loadStyle ( '/__tests__/block.mock.css' , callback ) ;
84+ it ( 'style element exists' , ( ) => {
85+ expect ( style ) . to . exist ;
86+ } ) ;
87+
88+ it ( 'style calls back' , async ( ) => {
89+ const loaded = await getObjectProperty ( 'styleCallback' , ms ) ;
90+ expect ( loaded ) . to . be . true ;
91+ } ) ;
92+
93+ it ( 'only one style' , async ( ) => {
94+ const oneStyle = await loadStyle ( '/__tests__/block.mock.css' , callbackTwo ) ;
95+ expect ( oneStyle ) . to . exist ;
96+ expect ( window . styleCallbackTwo ) . to . be . true ;
97+ } ) ;
9598} ) ;
9699
97100describe ( 'Template loading' , async ( ) => {
98- const meta = document . createElement ( 'meta' ) ;
99- meta . setAttribute ( 'name' , 'template' ) ;
100- meta . setAttribute ( 'content' , 'product' ) ;
101- document . head . append ( meta ) ;
102-
103- it ( 'template doesnt exist' , ( ) => {
104- const noTemplate = loadTemplate ( { } ) ;
105- expect ( noTemplate ) . to . not . exist ;
106- } ) ;
107-
108- it ( 'template conf doesnt exist' , ( ) => {
109- const template = loadTemplate ( config ) ;
110- expect ( template ) . to . not . exist ;
111- } ) ;
112-
113- it ( 'template conf exists' , ( ) => {
114- meta . setAttribute ( 'content' , 'docs' ) ;
115- loadTemplate ( config ) ;
116- expect ( document . body . classList . contains ( 'has-Template' ) ) . to . be . true ;
117- } ) ;
118-
119- it ( 'template has name' , ( ) => {
120- meta . setAttribute ( 'content' , 'docs' ) ;
121- loadTemplate ( config ) ;
122- expect ( document . body . classList . contains ( 'docs-template' ) ) . to . be . true ;
123- } ) ;
101+ const meta = document . createElement ( 'meta' ) ;
102+ meta . setAttribute ( 'name' , 'template' ) ;
103+ meta . setAttribute ( 'content' , 'product' ) ;
104+ document . head . append ( meta ) ;
105+
106+ it ( 'template doesnt exist' , ( ) => {
107+ const noTemplate = loadTemplate ( { } ) ;
108+ expect ( noTemplate ) . to . not . exist ;
109+ } ) ;
110+
111+ it ( 'template conf doesnt exist' , ( ) => {
112+ const template = loadTemplate ( config ) ;
113+ expect ( template ) . to . not . exist ;
114+ } ) ;
115+
116+ it ( 'template conf exists' , ( ) => {
117+ meta . setAttribute ( 'content' , 'docs' ) ;
118+ loadTemplate ( config ) ;
119+ expect ( document . body . classList . contains ( 'has-Template' ) ) . to . be . true ;
120+ } ) ;
121+
122+ it ( 'template has name' , ( ) => {
123+ meta . setAttribute ( 'content' , 'docs' ) ;
124+ loadTemplate ( config ) ;
125+ expect ( document . body . classList . contains ( 'docs-template' ) ) . to . be . true ;
126+ } ) ;
124127} ) ;
125128
126129describe ( 'Block loading' , async ( ) => {
127- const marquee = document . querySelector ( '.marquee' ) ;
128-
129- it ( 'block has a block select' , ( ) => {
130- const { blockSelect } = marquee . dataset ;
131- expect ( blockSelect ) . to . exist ;
132- } ) ;
133-
134- it ( 'block is loaded with only js' , async ( ) => {
135- await loadElement ( marquee , config . blocks [ '.marquee' ] ) ;
136- expect ( marquee . classList . contains ( 'is-Loaded' ) ) . to . be . true ;
137- } ) ;
138-
139- it ( 'block is loaded with css' , async ( ) => {
140- config . blocks [ '.marquee' ] . styles = 'marquee.css' ;
141- await loadElement ( marquee , config . blocks [ '.marquee' ] ) ;
142- expect ( marquee . classList . contains ( 'is-Loaded' ) ) . to . be . true ;
143- } ) ;
130+ const marquee = document . querySelector ( '.marquee' ) ;
131+
132+ it ( 'block has a block select' , ( ) => {
133+ const { blockSelect } = marquee . dataset ;
134+ expect ( blockSelect ) . to . exist ;
135+ } ) ;
136+
137+ it ( 'block is loaded with only js' , async ( ) => {
138+ await loadElement ( marquee , config . blocks [ '.marquee' ] ) ;
139+ expect ( marquee . classList . contains ( 'is-Loaded' ) ) . to . be . true ;
140+ } ) ;
141+
142+ it ( 'block is loaded with css' , async ( ) => {
143+ config . blocks [ '.marquee' ] . styles = 'marquee.css' ;
144+ await loadElement ( marquee , config . blocks [ '.marquee' ] ) ;
145+ expect ( marquee . classList . contains ( 'is-Loaded' ) ) . to . be . true ;
146+ } ) ;
144147} ) ;
145148
146149describe ( 'Post LCP' , ( ) => {
147- const img = document . createElement ( 'img' ) ;
148- document . body . append ( img ) ;
149-
150- it ( 'LCP loads when there is no image' , ( ) => {
151- const lcp = document . querySelector ( 'img' ) ;
152- setLCPTrigger ( lcp ) ;
153- expect ( window . lcpComplete ) . to . be . true ;
154- } ) ;
155-
156- it ( 'LCP loads when there is a bad image' , async ( ) => {
157- img . src = '/__tests__/nope.mock.png' ;
158- setLCPTrigger ( img ) ;
159- const lcpError = await getObjectProperty ( 'lcpError' , ms ) ;
160- expect ( lcpError ) . to . be . true ;
161- } ) ;
162-
163- it ( 'LCP loads when there is a good image' , async ( ) => {
164- img . src = '/__tests__/block.mock.png' ;
165- const lcpLoad = await getObjectProperty ( 'lcpLoad' , ms ) ;
166- expect ( lcpLoad ) . to . be . true ;
167- } ) ;
150+ const img = document . createElement ( 'img' ) ;
151+ document . body . append ( img ) ;
152+
153+ it ( 'LCP loads when there is no image' , ( ) => {
154+ const lcp = document . querySelector ( 'img' ) ;
155+ setLCPTrigger ( lcp ) ;
156+ expect ( window . lcpComplete ) . to . be . true ;
157+ } ) ;
158+
159+ it ( 'LCP loads when there is a bad image' , async ( ) => {
160+ img . src = '/__tests__/nope.mock.png' ;
161+ setLCPTrigger ( img ) ;
162+ const lcpError = await getObjectProperty ( 'lcpError' , ms ) ;
163+ expect ( lcpError ) . to . be . true ;
164+ } ) ;
165+
166+ it ( 'LCP loads when there is a good image' , async ( ) => {
167+ img . src = '/__tests__/block.mock.png' ;
168+ const lcpLoad = await getObjectProperty ( 'lcpLoad' , ms ) ;
169+ expect ( lcpLoad ) . to . be . true ;
170+ } ) ;
168171} ) ;
169172
170173const getLazyElement = ( selector , timeout ) => new Promise ( ( resolve ) => {
171- let i = 0 ;
172- const interval = 10 ;
173- const refreshId = setInterval ( ( ) => {
174- const el = document . querySelector ( selector ) ;
175- if ( el !== null && typeof el !== 'undefined' ) {
176- resolve ( el ) ;
177- clearInterval ( refreshId ) ;
178- } else if ( i >= timeout ) {
179- resolve ( null ) ;
180- clearInterval ( refreshId ) ;
181- }
182- i += interval ;
183- } , interval ) ;
174+ let i = 0 ;
175+ const interval = 10 ;
176+ const refreshId = setInterval ( ( ) => {
177+ const el = document . querySelector ( selector ) ;
178+ if ( el !== null && typeof el !== 'undefined' ) {
179+ resolve ( el ) ;
180+ clearInterval ( refreshId ) ;
181+ } else if ( i >= timeout ) {
182+ resolve ( null ) ;
183+ clearInterval ( refreshId ) ;
184+ }
185+ i += interval ;
186+ } , interval ) ;
184187} ) ;
185188
186189describe ( 'Lazy loading' , async ( ) => {
187- it ( 'youtube is loaded' , async ( ) => {
188- const blocks = decorateBlocks ( document ) ;
189- await loadBlocks ( blocks , config ) ;
190- const iframe = await getLazyElement ( 'iframe' , ms ) ;
191- expect ( iframe ) . to . exist ;
192- } ) ;
190+ it ( 'youtube is loaded' , async ( ) => {
191+ const blocks = decorateBlocks ( document ) ;
192+ await loadBlocks ( blocks , config ) ;
193+ const iframe = await getLazyElement ( 'iframe' , ms ) ;
194+ expect ( iframe ) . to . exist ;
195+ } ) ;
193196} ) ;
194197
195198describe ( 'Object property' , async ( ) => {
196- const loaded = await getObjectProperty ( 'nope' , ms ) ;
197- expect ( loaded ) . to . be . null ;
198- } ) ;
199+ const loaded = await getObjectProperty ( 'nope' , ms ) ;
200+ expect ( loaded ) . to . be . null ;
201+ } ) ;
0 commit comments