@@ -12,18 +12,80 @@ 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 ) => {
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+ document . body . appendChild ( node ) ;
36+ return node ;
37+ }
2638
2739test ( 'should expose lazyframe()' , ( t ) => {
28- t . true ( typeof dom . window . lazyframe === 'function' ) ;
40+ t . true ( typeof window . lazyframe === 'function' ) ;
2941} ) ;
42+
43+ test ( 'should initialize one node with a string selector' , ( t ) => {
44+ createDomNode ( 'youtube' , 'http://www.youtube.com/embed/iwGFalTRHDA/?rel=0' )
45+ window . lazyframe ( '.lazyframe' ) ;
46+ t . is ( document . querySelectorAll ( '.lazyframe--loaded' ) . length , 1 ) ;
47+ } )
48+
49+ test ( 'should initialize mulitple nodes with a string selector' , ( t ) => {
50+ createDomNode ( 'youtube' , 'http://www.youtube.com/embed/iwGFalTRHDB/?rel=0' )
51+ createDomNode ( 'youtube' , 'http://www.youtube.com/embed/iwGFalTRHDC/?rel=0' )
52+ window . lazyframe ( '.lazyframe' ) ;
53+ t . is ( document . querySelectorAll ( '.lazyframe--loaded' ) . length , 2 ) ;
54+ } )
55+
56+ test ( 'should initialize with a single node' , ( t ) => {
57+ const node = createDomNode ( 'youtube' , 'http://www.youtube.com/embed/iwGFalTRHDB/?rel=0' )
58+ window . lazyframe ( node ) ;
59+ t . is ( document . querySelectorAll ( '.lazyframe--loaded' ) . length , 1 ) ;
60+ } )
61+
62+ test ( 'should initialize with a nodelist' , ( t ) => {
63+ createDomNode ( 'youtube' , 'http://www.youtube.com/embed/iwGFalTRHDB/?rel=0' )
64+ createDomNode ( 'youtube' , 'http://www.youtube.com/embed/iwGFalTRHDC/?rel=0' )
65+ const nodes = document . querySelectorAll ( '.lazyframe' )
66+ window . lazyframe ( nodes ) ;
67+ t . is ( document . querySelectorAll ( '.lazyframe--loaded' ) . length , 2 ) ;
68+ } )
69+
70+ test ( 'should append an iframe on click' , ( t ) => {
71+ const node = createDomNode ( 'youtube' , 'http://www.youtube.com/embed/iwGFalTRHDB/?rel=0' )
72+ window . lazyframe ( '.lazyframe' ) ;
73+ node . click ( ) ;
74+
75+ t . assert ( node . querySelector ( 'iframe' ) )
76+ } )
77+
78+ test ( 'should call onAppend callback function' , ( t ) => {
79+ let i = 0 ;
80+ const node1 = createDomNode ( 'youtube' , 'http://www.youtube.com/embed/iwGFalTRHDB/?rel=0' )
81+ const node2 = createDomNode ( 'youtube' , 'http://www.youtube.com/embed/iwGFalTRHDB/?rel=0' )
82+ window . lazyframe ( '.lazyframe' , {
83+ onAppend ( ) {
84+ i ++ ;
85+ }
86+ } ) ;
87+ node1 . click ( ) ;
88+ node2 . click ( ) ;
89+
90+ t . is ( i , 2 )
91+ } )
0 commit comments