-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathpage-load-json.js
95 lines (80 loc) · 2.67 KB
/
page-load-json.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const test = require('ava');
const puppeteer = require('puppeteer');
const getServer = require('./_get-server.js');
const page2Json = require('./html/page/2.json');
const page3Json = require('./html/page/3.json');
const port = 9009;
let server, browser, page;
test.before( async function() {
server = getServer();
server.listen( port );
browser = await puppeteer.launch();
page = await browser.newPage();
await page.goto(`http://localhost:${port}/test/html/page-load.html`);
} );
test.after( async function() {
page.close();
await browser.close();
server.close();
} );
// ------ tests ------ //
test.serial( 'page-load: page/2.json', async( t ) => {
let assertions = await page.evaluate( function( page2Data ) {
let infScroll = window.infScroll = new InfiniteScroll( '.container', {
path: 'page/{{#}}.json',
responseBody: 'json',
history: false,
} );
let eventPromises = Promise.all([
// request event
new Promise( function( resolve ) {
infScroll.once( 'request', function( path ) {
serialT.truthy( path.match('page/2.json') );
resolve();
} );
} ),
// load event
new Promise( function( resolve ) {
infScroll.once( 'load', function( response, path ) {
serialT.truthy( path.match('page/2.json') );
serialT.deepEqual( response, page2Data );
serialT.is( infScroll.loadCount, 1 );
serialT.is( infScroll.pageIndex, 2 );
resolve();
} );
} ),
]);
// load page 2
infScroll.loadNextPage();
return eventPromises.then( () => serialT.assertions );
}, page2Json );
assertions.forEach( ({ method, args }) => t[ method ]( ...args ) );
} );
test.serial( 'page-load: page/3.json', async( t ) => {
let assertions = await page.evaluate( function( page3Data ) {
let infScroll = window.infScroll;
let eventPromises = Promise.all([
// request event
new Promise( function( resolve ) {
infScroll.once( 'request', function( path ) {
serialT.truthy( path.match('page/3.json') );
resolve();
} );
} ),
// load event
new Promise( function( resolve ) {
infScroll.once( 'load', function( response, path ) {
serialT.truthy( path.match('page/3.json') );
serialT.deepEqual( response, page3Data );
serialT.is( infScroll.loadCount, 2 );
serialT.is( infScroll.pageIndex, 3 );
resolve();
} );
} ),
]);
// load page 2
infScroll.loadNextPage();
return eventPromises.then( () => serialT.assertions );
}, page3Json );
assertions.forEach( ({ method, args }) => t[ method ]( ...args ) );
} );