Skip to content

Commit 7b487bd

Browse files
patrickhulcealexkrolick
authored andcommitted
feat: add pptr-testing-library docs (#36)
1 parent 60d19f3 commit 7b487bd

File tree

4 files changed

+245
-0
lines changed

4 files changed

+245
-0
lines changed

docs/pptr-testing-library/intro.md

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
---
2+
id: intro
3+
title: Puppeteer Testing Library
4+
---
5+
6+
[`pptr-testing-library`][gh] is a lightweight adapter allowing
7+
`dom-testing-library` to be used with [`puppeteer`][ghpuppeteer].
8+
9+
```
10+
npm install --save-dev puppeteer pptr-testing-library
11+
```
12+
13+
- [pptr-testing-library on GitHub][gh]
14+
15+
## Usage
16+
17+
```js
18+
const puppeteer = require('puppeteer')
19+
const { getDocument, queries, wait } = require('pptr-testing-library')
20+
21+
const { getByTestId, getByLabelText } = queries
22+
23+
const browser = await puppeteer.launch()
24+
const page = await browser.newPage()
25+
26+
// Grab ElementHandle for document
27+
const $document = await getDocument(page)
28+
// Your favorite query methods are available
29+
const $form = await getByTestId($document, 'my-form')
30+
// returned elements are Puppeteer ElementHandles too!
31+
const $email = await getByLabelText($form, 'Email')
32+
// interact with puppeteer like usual
33+
await $email.type('[email protected]')
34+
// waiting works too!
35+
await wait(() => getByText('Loading...'))
36+
```
37+
38+
A little too un-puppeteer for you? You can attach all the `dom-testing-library`
39+
methods directly onto puppeteer's `ElementHandle` instead!
40+
41+
```js
42+
const puppeteer = require('puppeteer')
43+
require('pptr-testing-library/extend')
44+
45+
const browser = await puppeteer.launch()
46+
const page = await browser.newPage()
47+
48+
// getDocument is added to prototype of Page
49+
const $document = await page.getDocument()
50+
// query methods are added directly to prototype of ElementHandle
51+
const $form = await $document.getByTestId('my-form')
52+
// destructing works if you explicitly call getQueriesForElement
53+
const { getByLabelText } = $form.getQueriesForElement()
54+
// ...
55+
const $email = await getByLabelText('Email')
56+
```
57+
58+
### API
59+
60+
Unique methods, not part of `dom-testing-library`.
61+
62+
- `getDocument(page: puppeteer.Page): ElementHandle` - get an ElementHandle for
63+
the document
64+
65+
### Forwarded methods
66+
67+
`dom-testing-library` is injected into the page that puppeteer is controlling on
68+
each query, so all results will be async. It's still recommended that you use
69+
puppeteer's built-in methods for interaction rather than `fireEvent`.
70+
71+
## Known Limitations
72+
73+
- `waitForElement` method is not exposed. Puppeteer has its own set of wait
74+
utilities that somewhat conflict with the style used in `dom-testing-library`.
75+
See
76+
[the issue on GitHub](https://github.com/patrickhulce/pptr-testing-library/issues/3).
77+
- `fireEvent` method is not exposed, use puppeteer's built-ins instead.
78+
- `expect` assertion extensions are not available.
79+
80+
[gh]: https://github.com/patrickhulce/pptr-testing-library
81+
[ghpuppeteer]: https://github.com/GoogleChrome/puppeteer

website/pages/en/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,11 @@ class Index extends React.Component {
210210
title:
211211
'[ReasonReact Testing Library](./docs/bs-react-testing-library/intro)',
212212
},
213+
{
214+
image: `${baseUrl}img/puppeteer-275x275.png`,
215+
imageAlign: 'top',
216+
title: '[Puppeteer Testing Library](./pptr)',
217+
},
213218
{
214219
image: `${baseUrl}img/construction-128x128.png`,
215220
imageAlign: 'top',

website/pages/en/pptr.js

+159
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
/**
2+
* Copyright (c) 2017-present, Facebook, Inc.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
const React = require('react')
9+
10+
const CompLibrary = require('../../core/CompLibrary.js')
11+
12+
const MarkdownBlock = CompLibrary.MarkdownBlock /* Used to read markdown */
13+
const Container = CompLibrary.Container
14+
const GridBlock = CompLibrary.GridBlock
15+
16+
class HomeSplash extends React.Component {
17+
render() {
18+
const { siteConfig, language = '' } = this.props
19+
const { baseUrl, docsUrl } = siteConfig
20+
const docsPart = `${docsUrl ? `${docsUrl}/` : ''}`
21+
const langPart = `${language ? `${language}/` : ''}`
22+
const docUrl = doc => `${baseUrl}${docsPart}${langPart}${doc}`
23+
24+
const SplashContainer = props => (
25+
<div className="homeContainer">
26+
<div className="homeSplashFade">
27+
<div className="wrapper homeWrapper">{props.children}</div>
28+
</div>
29+
</div>
30+
)
31+
32+
const Logo = props => (
33+
<div className="projectLogo">
34+
<img src={props.img_src} alt="Project Logo" />
35+
</div>
36+
)
37+
38+
const ProjectTitle = () => (
39+
<div>
40+
<h2 className="projectTitle">Puppeteer Testing Library</h2>
41+
<div className="projectTaglineWrapper">
42+
<p className="projectTagline">
43+
Simple and complete Puppeteer DOM testing utilities that encourage
44+
good testing practices
45+
</p>
46+
</div>
47+
</div>
48+
)
49+
50+
const PromoSection = props => (
51+
<div className="section promoSection">
52+
<div className="promoRow">
53+
<div className="pluginRowBlock">{props.children}</div>
54+
</div>
55+
</div>
56+
)
57+
58+
const Button = props => (
59+
<div className="pluginWrapper buttonWrapper">
60+
<a className="button" href={props.href} target={props.target}>
61+
{props.children}
62+
</a>
63+
</div>
64+
)
65+
66+
return (
67+
<SplashContainer>
68+
<Logo img_src={`${baseUrl}img/puppeteer-275x275.png`} />
69+
<div className="inner">
70+
<ProjectTitle siteConfig={siteConfig} />
71+
<PromoSection>
72+
<Button href={docUrl('pptr-testing-library/intro')}>
73+
Read the Docs
74+
</Button>
75+
</PromoSection>
76+
</div>
77+
</SplashContainer>
78+
)
79+
}
80+
}
81+
82+
class Index extends React.Component {
83+
render() {
84+
const { config: siteConfig, language = '' } = this.props
85+
const { baseUrl } = siteConfig
86+
87+
const Block = props => (
88+
<Container
89+
padding={['bottom', 'top']}
90+
id={props.id}
91+
background={props.background}
92+
>
93+
<GridBlock
94+
align={props.align || 'center'}
95+
imageAlign={props.imageAlign || 'center'}
96+
contents={props.children}
97+
layout={props.layout}
98+
/>
99+
</Container>
100+
)
101+
102+
const FeatureCallout = () => (
103+
<Container className="" background={'light'} padding={['top', 'bottom']}>
104+
<div style={{ textAlign: 'center' }}>
105+
<p>
106+
<i>
107+
The more your tests resemble the way your software is used, <br />
108+
the more confidence they can give you.
109+
</i>
110+
</p>
111+
<MarkdownBlock>
112+
`npm install --save-dev pptr-testing-library`
113+
</MarkdownBlock>
114+
</div>
115+
</Container>
116+
)
117+
118+
const Features = () => (
119+
<React.Fragment>
120+
<Block layout="threeColumn">
121+
{[
122+
{
123+
content:
124+
'Tests only break when your app breaks, not implementation details',
125+
image: `${baseUrl}img/wrench-128x128.png`,
126+
imageAlign: 'top',
127+
title: 'Write Maintainable Tests',
128+
},
129+
{
130+
content: 'Interact with your app the same way as your users',
131+
image: `${baseUrl}img/check-128x128.png`,
132+
imageAlign: 'top',
133+
title: 'Develop with Confidence',
134+
},
135+
{
136+
content:
137+
'Built-in selectors use semantic HTML and ARIA roles to help you write inclusive code',
138+
image: `${baseUrl}img/tada-128x128.png`,
139+
imageAlign: 'top',
140+
title: 'Accessible by Default',
141+
},
142+
]}
143+
</Block>
144+
</React.Fragment>
145+
)
146+
147+
return (
148+
<div>
149+
<HomeSplash siteConfig={siteConfig} language={language} />
150+
<div className="mainContainer">
151+
<FeatureCallout />
152+
<Features />
153+
</div>
154+
</div>
155+
)
156+
}
157+
}
158+
159+
module.exports = Index
20.8 KB
Loading

0 commit comments

Comments
 (0)