@@ -12,18 +12,96 @@ const script = new Script(
1212 readFileSync ( path . join ( __dirname , '..' , 'dist' , 'lazyframe.min.js' ) )
1313) ;
1414
15- const dom = new JSDOM ( `` , {
16- url : 'http://localhost:3000/' ,
17- referrer : 'http://localhost:3000/' ,
18- contentType : 'text/html' ,
19- includeNodeLocations : true ,
20- resources : 'usable' ,
21- runScripts : 'dangerously' ,
22- virtualConsole
23- } ) ;
15+ test . beforeEach ( t => {
16+ const dom = new JSDOM ( `` , {
17+ includeNodeLocations : true ,
18+ resources : 'usable' ,
19+ runScripts : 'dangerously' ,
20+ virtualConsole
21+ } ) ;
22+
23+ dom . runVMScript ( script ) ;
24+ global . document = dom . window . document ;
25+ global . window = dom . window ;
26+ } )
2427
25- dom . runVMScript ( script ) ;
28+ const createDomNode = ( vendor , src , title , thumbnail ) => {
29+ const node = document . createElement ( 'div' ) ;
30+ node . classList . add ( 'lazyframe' ) ;
31+ node . setAttribute ( 'data-src' , src )
32+ if ( vendor ) {
33+ node . setAttribute ( 'data-vendor' , vendor )
34+ }
35+ if ( title ) {
36+ node . setAttribute ( 'data-title' , title ) ;
37+ }
38+ if ( thumbnail ) {
39+ node . setAttribute ( 'data-thumbnail' , thumbnail ) ;
40+ }
41+ document . body . appendChild ( node ) ;
42+ return node ;
43+ }
2644
2745test ( 'should expose lazyframe()' , ( t ) => {
28- t . true ( typeof dom . window . lazyframe === 'function' ) ;
46+ t . true ( typeof window . lazyframe === 'function' ) ;
2947} ) ;
48+
49+ test ( 'should initialize one node with a string selector' , ( t ) => {
50+ createDomNode ( 'youtube' , 'http://www.youtube.com/embed/iwGFalTRHDA/?rel=0' )
51+ window . lazyframe ( '.lazyframe' ) ;
52+ t . is ( document . querySelectorAll ( '.lazyframe--loaded' ) . length , 1 ) ;
53+ } )
54+
55+ test ( 'should initialize mulitple nodes with a string selector' , ( t ) => {
56+ createDomNode ( 'youtube' , 'http://www.youtube.com/embed/iwGFalTRHDB/?rel=0' )
57+ createDomNode ( 'youtube' , 'http://www.youtube.com/embed/iwGFalTRHDC/?rel=0' )
58+ window . lazyframe ( '.lazyframe' ) ;
59+ t . is ( document . querySelectorAll ( '.lazyframe--loaded' ) . length , 2 ) ;
60+ } )
61+
62+ test ( 'should initialize with a single node' , ( t ) => {
63+ const node = createDomNode ( 'youtube' , 'http://www.youtube.com/embed/iwGFalTRHDB/?rel=0' )
64+ window . lazyframe ( node ) ;
65+ t . is ( document . querySelectorAll ( '.lazyframe--loaded' ) . length , 1 ) ;
66+ } )
67+
68+ test ( 'should initialize with a nodelist' , ( t ) => {
69+ createDomNode ( 'youtube' , 'http://www.youtube.com/embed/iwGFalTRHDB/?rel=0' )
70+ createDomNode ( 'youtube' , 'http://www.youtube.com/embed/iwGFalTRHDC/?rel=0' )
71+ const nodes = document . querySelectorAll ( '.lazyframe' )
72+ window . lazyframe ( nodes ) ;
73+ t . is ( document . querySelectorAll ( '.lazyframe--loaded' ) . length , 2 ) ;
74+ } )
75+
76+ test ( 'should append an iframe on click' , ( t ) => {
77+ const node = createDomNode ( 'youtube' , 'http://www.youtube.com/embed/iwGFalTRHDB/?rel=0' )
78+ window . lazyframe ( '.lazyframe' ) ;
79+ node . click ( ) ;
80+
81+ t . assert ( node . querySelector ( 'iframe' ) )
82+ } )
83+
84+ test ( 'should call onAppend callback function' , ( t ) => {
85+ let i = 0 ;
86+ const node1 = createDomNode ( 'youtube' , 'http://www.youtube.com/embed/iwGFalTRHDB/?rel=0' )
87+ const node2 = createDomNode ( 'youtube' , 'http://www.youtube.com/embed/iwGFalTRHDB/?rel=0' )
88+ window . lazyframe ( '.lazyframe' , {
89+ onAppend ( ) {
90+ i ++ ;
91+ }
92+ } ) ;
93+ node1 . click ( ) ;
94+ node2 . click ( ) ;
95+
96+ t . is ( i , 2 )
97+ } )
98+
99+ test ( 'should use data-title' , ( t ) => {
100+ const title = 'custom title'
101+ const node = createDomNode ( 'youtube' , 'http://www.youtube.com/embed/iwGFalTRHDB/?rel=0' , title )
102+
103+ window . lazyframe ( '.lazyframe' ) ;
104+
105+ node . click ( )
106+ t . is ( document . querySelector ( '.lazyframe__title' ) . textContent , title )
107+ } )
0 commit comments