Skip to content

Commit d7540b3

Browse files
authored
feat: add page
1 parent a5429f2 commit d7540b3

File tree

5 files changed

+92
-4
lines changed

5 files changed

+92
-4
lines changed

src/browser/adapters/puppeteer.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const puppeteerAdapter = async delegate => {
55
const page = await browser.newPage()
66

77
await delegate({
8+
context: () => page,
89
get: async uri => {
910
await page.goto(uri)
1011
return await page.content()

src/browser/browser.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import isFunction from 'inspected/schema/is-function'
44
import isRequired from 'inspected/schema/is-required'
55
import isObject from 'inspected/schema/is-object'
66

7+
import pager from './pager'
8+
79
const createAdapted = adapter => ({
810
get: async uri => {
911
if (!isRequired(isString)(uri)) {
@@ -12,12 +14,21 @@ const createAdapted = adapter => ({
1214

1315
let content = null
1416

15-
await adapter(async http => {
16-
content = await http.get(uri)
17+
await adapter(async adapted => {
18+
content = await adapted.get(uri)
1719
})
1820

1921
return content
2022
},
23+
page: async (uri, delegate) => {
24+
if (!isRequired(isString)(uri)) {
25+
throw new Error('Uri must be a string.')
26+
}
27+
28+
await adapter(async adapted => {
29+
pager(adapted.context(), uri)(delegate)
30+
})
31+
},
2132
})
2233

2334
const browser = options => adapter => {
@@ -46,11 +57,12 @@ const browser = options => adapter => {
4657
throw new Error('Unexpected value, must be a function.')
4758
}
4859

49-
await adapter(async http => {
50-
await delegate(createAdapted(async process => await process(http)))
60+
await adapter(async adapted => {
61+
await delegate(createAdapted(async process => await process(adapted)))
5162
})
5263
}
5364

65+
// Provide a subset of functions outside of 'use'
5466
const funcs = ['get']
5567

5668
funcs.forEach(f => {

src/browser/browser.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import browser from './browser'
22
import puppeteer from './adapters/puppeteer'
3+
import { Page } from 'puppeteer/lib/Page'
34

45
describe('browser', () => {
56
it('should throw when options are not an object', () => {
@@ -38,4 +39,17 @@ describe('browser', () => {
3839

3940
expect(content).toBeTruthy()
4041
})
42+
43+
it('should pass configured page function with page context', async () => {
44+
const { use } = browser()(puppeteer)
45+
46+
let result = null
47+
await use(async ({ page }) => {
48+
await page('http://www.google.com', (context, uri) => {
49+
result = { context, uri }
50+
})
51+
})
52+
53+
expect(result.context).toBeInstanceOf(Page)
54+
})
4155
})

src/browser/pager.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import isFunction from 'inspected/schema/is-function'
2+
import isNil from 'inspected/schema/is-nil'
3+
4+
const pager = (context, uri) => async delegate => {
5+
if (isNil(delegate)) {
6+
throw new Error('delegate must be specified.')
7+
}
8+
9+
if (!isFunction(delegate)) {
10+
throw new Error('delegate must be a function.')
11+
}
12+
13+
let currentContext = context
14+
let currentUri = uri
15+
16+
let result = { complete: false }
17+
while (result.complete === false) {
18+
result = await delegate(currentContext, currentUri)
19+
20+
if (!result) {
21+
break
22+
}
23+
24+
currentContext = result.context
25+
currentUri = result.uri
26+
}
27+
}
28+
29+
export default pager

src/browser/pager.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import pager from './pager'
2+
3+
describe('pager', () => {
4+
it('should throw if delegate is undefined', async () => {
5+
await expect(pager()()).rejects.toHaveProperty(
6+
'message',
7+
'delegate must be specified.'
8+
)
9+
})
10+
11+
it('should throw if delegate is null', async () => {
12+
await expect(pager()(null)).rejects.toHaveProperty(
13+
'message',
14+
'delegate must be specified.'
15+
)
16+
})
17+
18+
it('should throw if delegate is not a function', async () => {
19+
await expect(pager()(false)).rejects.toHaveProperty(
20+
'message',
21+
'delegate must be a function.'
22+
)
23+
})
24+
25+
it('should pass context and uri to delegate', async () => {
26+
let result = null
27+
await pager({ foo: 'bar' }, 'baz')(
28+
async (context, uri) => (result = { context, uri })
29+
)
30+
expect(result).toEqual({ context: { foo: 'bar' }, uri: 'baz' })
31+
})
32+
})

0 commit comments

Comments
 (0)