Skip to content

Commit

Permalink
feat(puppet-strings): Switch to native ES modules
Browse files Browse the repository at this point in the history
BREAKING CHANGE: This package now exports ES modules instead of CJS
  • Loading branch information
vinsonchuong committed Nov 1, 2020
1 parent 0226d43 commit 7f69e13
Show file tree
Hide file tree
Showing 46 changed files with 3,214 additions and 5,611 deletions.
15 changes: 0 additions & 15 deletions .flowconfig

This file was deleted.

1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
/dist
/node_modules
/yarn-error.log
7 changes: 1 addition & 6 deletions actions/click-element/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/* @flow */
import type { Element } from 'puppet-strings'

export default async function({
puppeteer: { elementHandle }
}: Element): Promise<void> {
export default async function ({puppeteer: {elementHandle}}) {
await elementHandle.click()
}
21 changes: 8 additions & 13 deletions actions/click-element/index.test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
/* @flow */
import ava from 'ava'
import {
withChrome,
withDirectory,
writeFile
} from 'puppet-strings/test/helpers'
import { openTab, findElement, clickElement } from 'puppet-strings'
import test from 'ava'
import {withChrome, withDirectory, writeFile} from '../../test/helpers/index.js'
import {openTab, findElement, clickElement} from '../../index.js'

withChrome()
const test = withDirectory(ava)
withChrome(test)
withDirectory(test)

test('clicking an element', async t => {
const { browser } = global
const { directory } = t.context
test('clicking an element', async (t) => {
const {browser} = global
const {directory} = t.context

const htmlPath = await writeFile(
directory,
Expand Down
7 changes: 1 addition & 6 deletions actions/close-browser/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/* @flow */
import type { Browser } from 'puppet-strings'

export default async function({
puppeteer: { browser }
}: Browser): Promise<void> {
export default async function ({puppeteer: {browser}}) {
await browser.close()
}
11 changes: 5 additions & 6 deletions actions/close-browser/index.test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
/* @flow */
import test from 'ava'
import {
findProcess,
waitForProcess,
withChromePath
} from 'puppet-strings/test/helpers'
import { openBrowser, closeBrowser } from 'puppet-strings'
} from '../../test/helpers/index.js'
import {openBrowser, closeBrowser} from '../../index.js'

withChromePath()
withChromePath(test)

test('closing a browser', async t => {
test('closing a browser', async (t) => {
const browser = await openBrowser(global.chromePath)
const {
puppeteer: { browser: puppeteerBrowser }
puppeteer: {browser: puppeteerBrowser}
} = browser

t.truthy(await waitForProcess(puppeteerBrowser.process()))
Expand Down
5 changes: 1 addition & 4 deletions actions/close-tab/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/* @flow */
import type { Tab } from 'puppet-strings'

export default async function({ puppeteer: { page } }: Tab): Promise<void> {
export default async function ({puppeteer: {page}}) {
await page.close()
}
15 changes: 7 additions & 8 deletions actions/close-tab/index.test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
/* @flow */
import ava from 'ava'
import test from 'ava'
import {
withChromePerTest,
withDirectory,
writeFile
} from 'puppet-strings/test/helpers'
import { openTab, closeTab } from 'puppet-strings'
} from '../../test/helpers/index.js'
import {openTab, closeTab} from '../../index.js'

const ava2 = withDirectory(ava)
const test = withChromePerTest(ava2)
withDirectory(test)
withChromePerTest(test)

test('closing tabs', async t => {
const { browser, directory } = t.context
test('closing tabs', async (t) => {
const {browser, directory} = t.context

const filePath = await writeFile(
directory,
Expand Down
11 changes: 2 additions & 9 deletions actions/eval-in-tab/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
/* @flow */
/* eslint-disable no-new-func, flowtype/no-weak-types */
import type { Tab } from 'puppet-strings'

export default async function(
{ puppeteer: { page } }: Tab,
args: Array<any>,
functionBody: string
): Promise<any> {
export default async function ({puppeteer: {page}}, args, functionBody) {
try {
// eslint-disable-next-line no-new-func
return await page.evaluate(new Function(functionBody), ...args)
} catch (error) {
throw new Error(`Failed to evaluate code in tab: ${error.message}`)
Expand Down
34 changes: 14 additions & 20 deletions actions/eval-in-tab/index.test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
/* @flow */
import ava from 'ava'
import {
withChrome,
withDirectory,
writeFile
} from 'puppet-strings/test/helpers'
import { openTab, evalInTab } from 'puppet-strings'
import test from 'ava'
import {withChrome, withDirectory, writeFile} from '../../test/helpers/index.js'
import {openTab, evalInTab} from '../../index.js'

withChrome()
const test = withDirectory(ava)
withChrome(test)
withDirectory(test)

test('executing code within the browser', async t => {
const { browser } = global
const { directory } = t.context
test('executing code within the browser', async (t) => {
const {browser} = global
const {directory} = t.context

const filePath = await writeFile(
directory,
Expand All @@ -36,9 +31,9 @@ test('executing code within the browser', async t => {
t.is(text, 'Hello World!')
})

test('propagating error messages', async t => {
const { browser } = global
const { directory } = t.context
test('propagating error messages', async (t) => {
const {browser} = global
const {directory} = t.context

const filePath = await writeFile(
directory,
Expand All @@ -51,8 +46,7 @@ test('propagating error messages', async t => {
)
const tab = await openTab(browser, `file://${filePath}`)

await t.throwsAsync(
evalInTab(tab, [], 'throw new Error("Error Message")'),
/Error: Error Message/
)
await t.throwsAsync(evalInTab(tab, [], 'throw new Error("Error Message")'), {
message: /Error: Error Message/
})
})
10 changes: 2 additions & 8 deletions actions/fill-in-element/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
/* @flow */
/* eslint-disable no-new-func */
import type { Element } from 'puppet-strings'

export default async function(
{ puppeteer: { frame, elementHandle } }: Element,
text: string
): Promise<void> {
export default async function ({puppeteer: {frame, elementHandle}}, text) {
await elementHandle.type(text)
await frame.evaluate(
// eslint-disable-next-line no-new-func
new Function(`
const [element] = arguments
element.blur()
Expand Down
27 changes: 11 additions & 16 deletions actions/fill-in-element/index.test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
/* @flow */
import ava from 'ava'
import {
withChrome,
withDirectory,
writeFile
} from 'puppet-strings/test/helpers'
import { openTab, findElement, fillInElement } from 'puppet-strings'
import test from 'ava'
import {withChrome, withDirectory, writeFile} from '../../test/helpers/index.js'
import {openTab, findElement, fillInElement} from '../../index.js'

withChrome()
const test = withDirectory(ava)
withChrome(test)
withDirectory(test)

test('filling in an element', async t => {
const { browser } = global
const { directory } = t.context
test('filling in an element', async (t) => {
const {browser} = global
const {directory} = t.context

const htmlPath = await writeFile(
directory,
Expand All @@ -32,9 +27,9 @@ test('filling in an element', async t => {
t.is((await findElement(tab, 'span')).innerText, 'Hello')
})

test('filling in an element in an iframe', async t => {
const { browser } = global
const { directory } = t.context
test('filling in an element in an iframe', async (t) => {
const {browser} = global
const {directory} = t.context

const htmlPath = await writeFile(
directory,
Expand Down
26 changes: 9 additions & 17 deletions actions/find-element/index.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,30 @@
/* @flow */
/* eslint-disable no-new-func */
import type { Tab, Element } from 'puppet-strings'

export default async function(
tab: Tab,
selector: string,
text: ?string,
{ timeout = 5000 }: { timeout?: number } = {}
): Promise<Element> {
export default async function (tab, selector, text, {timeout = 5000} = {}) {
const {
puppeteer: { browser, page }
puppeteer: {browser, page}
} = tab

try {
const [frame, elementHandle] = await Promise.race(
page.frames().map(async frame => [
page.frames().map(async (frame) => [
frame,
text
? await frame.waitForFunction(
// eslint-disable-next-line no-new-func
new Function(`
const [selector, text] = arguments
return Array.from(document.querySelectorAll(selector))
.find(e => e.textContent.includes(text))
`),
{ timeout },
{timeout},
selector,
text
)
: await frame.waitForSelector(selector, { timeout })
: await frame.waitForSelector(selector, {timeout})
])
)

const metadata = await frame.evaluate(
// eslint-disable-next-line no-new-func
new Function(`
const [element] = arguments
Expand All @@ -44,15 +37,14 @@ export default async function(
outerHTML: element.outerHTML
}
`),
// $FlowFixMe
elementHandle
)

return {
...metadata,
puppeteer: { browser, page, frame, elementHandle }
puppeteer: {browser, page, frame, elementHandle}
}
} catch (error) {
} catch {
throw new Error(
`Could not find element with selector "${selector}"${
text ? ` and text "${text}"` : ''
Expand Down
Loading

0 comments on commit 7f69e13

Please sign in to comment.