11require ( 'should' ) ;
22var _ = require ( 'lodash' ) ;
3+ var Promise = require ( 'bluebird' ) ;
34var sinon = require ( 'sinon' ) ;
5+ require ( 'sinon-as-promised' ) ( Promise ) ;
46var nock = require ( 'nock' ) ;
57var fs = require ( 'fs-extra' ) ;
6- var Promise = require ( 'bluebird' ) ;
78var Scraper = require ( '../../../lib/scraper' ) ;
89var Resource = require ( '../../../lib/resource' ) ;
910var loadHtml = require ( '../../../lib/file-handlers/html' ) ;
@@ -167,15 +168,18 @@ describe('Html handler', function () {
167168 </html>\
168169 ' ;
169170
170- var po = new Resource ( 'http://example.com' , 'index.html' ) ;
171- po . setText ( html ) ;
171+ var parentResource = new Resource ( 'http://example.com' , 'index.html' ) ;
172+ parentResource . setText ( html ) ;
173+ var updateChildSpy = sinon . spy ( parentResource , 'updateChild' ) ;
172174
173175 // order of loading is determined by order of sources in scraper options
174- loadHtml ( scraper , po ) . then ( function ( ) {
176+ loadHtml ( scraper , parentResource ) . then ( function ( ) {
175177 loadResourceSpy . calledThrice . should . be . eql ( true ) ;
176178 loadResourceSpy . args [ 0 ] [ 0 ] . url . should . be . eql ( 'http://example.com/a.jpg' ) ;
177179 loadResourceSpy . args [ 1 ] [ 0 ] . url . should . be . eql ( 'http://example.com/b.css' ) ;
178180 loadResourceSpy . args [ 2 ] [ 0 ] . url . should . be . eql ( 'http://example.com/c.js' ) ;
181+
182+ updateChildSpy . calledThrice . should . be . eql ( true ) ;
179183 done ( ) ;
180184 } ) . catch ( done ) ;
181185 } ) ;
@@ -200,11 +204,12 @@ describe('Html handler', function () {
200204 </html>\
201205 ' ;
202206
203- var po = new Resource ( 'http://example.com' , 'index.html' ) ;
204- po . setText ( html ) ;
207+ var parentResource = new Resource ( 'http://example.com' , 'index.html' ) ;
208+ parentResource . setText ( html ) ;
209+ var updateChildSpy = sinon . spy ( parentResource , 'updateChild' ) ;
205210
206- return loadHtml ( scraper , po ) . then ( function ( ) {
207- var text = po . getText ( ) ;
211+ return loadHtml ( scraper , parentResource ) . then ( function ( ) {
212+ var text = parentResource . getText ( ) ;
208213 text . should . containEql ( 'http://example.com/public/img/a.jpg' ) ;
209214 text . should . containEql ( 'http://example.com/b.css' ) ;
210215 text . should . not . containEql ( 'local/a.jpg' ) ;
@@ -213,6 +218,8 @@ describe('Html handler', function () {
213218 text . should . not . containEql ( 'scripts/c.js' ) ;
214219 text . should . containEql ( 'local/c.js' ) ;
215220
221+ updateChildSpy . calledOnce . should . be . eql ( true ) ;
222+
216223 done ( ) ;
217224 } ) . catch ( done ) ;
218225 } ) ;
@@ -325,14 +332,14 @@ describe('Html handler', function () {
325332 } ) ;
326333
327334 it ( 'should handle img tag with srcset attribute correctly' , function ( done ) {
328-
329335 var image45Stub = new Resource ( 'http://example.com/image45.jpg' , 'local/image45.jpg' ) ;
330336 var image150Stub = new Resource ( 'http://example.com/image150.jpg' , 'local/image150.jpg' ) ;
331337
332338 sinon . stub ( scraper , 'loadResource' )
333- . onFirstCall ( ) . returns ( Promise . resolve ( image45Stub ) )
334- . onSecondCall ( ) . returns ( Promise . resolve ( image150Stub ) )
335- . onThirdCall ( ) . returns ( Promise . resolve ( image45Stub ) ) ;
339+ . onFirstCall ( ) . resolves ( image45Stub )
340+ . onSecondCall ( ) . resolves ( image150Stub )
341+ . onThirdCall ( ) . resolves ( image45Stub ) ;
342+
336343
337344 var html = '\
338345 <html> \
@@ -344,11 +351,14 @@ describe('Html handler', function () {
344351 </html>\
345352 ' ;
346353
347- var po = new Resource ( 'http://example.com' , 'index.html' ) ;
348- po . setText ( html ) ;
354+ var parentResource = new Resource ( 'http://example.com' , 'index.html' ) ;
355+ parentResource . setText ( html ) ;
356+ var updateChildSpy = sinon . spy ( parentResource , 'updateChild' ) ;
349357
350- return loadHtml ( scraper , po ) . then ( function ( ) {
351- var text = po . getText ( ) ;
358+ return loadHtml ( scraper , parentResource ) . then ( function ( ) {
359+ var text = parentResource . getText ( ) ;
360+
361+ updateChildSpy . calledThrice . should . be . eql ( true ) ;
352362
353363 text . should . not . containEql ( 'http://example.com/image45.jpg' ) ;
354364 text . should . not . containEql ( 'http://example.com/image150.jpg' ) ;
@@ -363,9 +373,9 @@ describe('Html handler', function () {
363373 var image150Stub = new Resource ( 'http://example.com/image150.jpg' , 'local/image150.jpg' ) ;
364374
365375 sinon . stub ( scraper , 'loadResource' )
366- . onFirstCall ( ) . returns ( Promise . resolve ( null ) )
367- . onSecondCall ( ) . returns ( Promise . resolve ( image150Stub ) )
368- . onThirdCall ( ) . returns ( Promise . resolve ( null ) ) ;
376+ . onFirstCall ( ) . resolves ( null )
377+ . onSecondCall ( ) . resolves ( image150Stub )
378+ . onThirdCall ( ) . resolves ( null ) ;
369379
370380 var html = '\
371381 <html> \
@@ -377,11 +387,14 @@ describe('Html handler', function () {
377387 </html>\
378388 ' ;
379389
380- var po = new Resource ( 'http://example.com' , 'index.html' ) ;
381- po . setText ( html ) ;
390+ var parentResource = new Resource ( 'http://example.com' , 'index.html' ) ;
391+ parentResource . setText ( html ) ;
392+ var updateChildSpy = sinon . spy ( parentResource , 'updateChild' ) ;
382393
383- return loadHtml ( scraper , po ) . then ( function ( ) {
384- var text = po . getText ( ) ;
394+ return loadHtml ( scraper , parentResource ) . then ( function ( ) {
395+ var text = parentResource . getText ( ) ;
396+
397+ updateChildSpy . calledOnce . should . be . eql ( true ) ;
385398
386399 text . should . containEql ( 'http://example.com/image45.jpg' ) ;
387400 text . should . not . containEql ( 'src="local/image45.jpg"' ) ;
0 commit comments