Skip to content

Commit 886b5b3

Browse files
authored
feat: support multiple HTML code blocks per fiddle (#112)
* add multiple JS blocks doc to readme * feat: support multiple html code blocks * pass null if html fiddles are empty
1 parent 900bcbb commit 886b5b3

File tree

6 files changed

+89
-17
lines changed

6 files changed

+89
-17
lines changed

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,46 @@ The "html" block is optional.
5454

5555
Note: after extracting the tests, this preprocessor sets [@bahmutov/cypress-fiddle](https://github.com/bahmutov/cypress-fiddle) to run them.
5656

57+
### Multiple JavaScript blocks
58+
59+
You can include multiple JS blocks in each fiddle
60+
61+
<!-- fiddle Example -->
62+
```js
63+
cy.contains('#hello', 'Hello')
64+
```
65+
66+
Some time later more test code
67+
68+
```js
69+
cy.wait(2000)
70+
```
71+
72+
<!-- fiddle.end -->
73+
74+
### Multiple HTML blocks
75+
76+
You can include multiple HTML blocks in each fiddle
77+
78+
<!-- fiddle Multiple HTML code blocks -->
79+
80+
```html
81+
<div id="greeting">Hello</div>
82+
```
83+
84+
Another HTML code block
85+
86+
```html
87+
<div id="name">World</div>
88+
```
89+
90+
```js
91+
cy.get('div#greeting').should('have.text', 'Hello')
92+
cy.get('div#name').should('have.text', 'World')
93+
```
94+
95+
<!-- fiddle-end -->
96+
5797
### Suites
5898

5999
To create suites of tests, use `/` to separate the suite name from the test name. Tests with the same suite name are grouped together automatically.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Multiple HTML code blocks
2+
3+
<!-- fiddle Multiple HTML code blocks -->
4+
5+
```html
6+
<div id="greeting">Hello</div>
7+
```
8+
9+
Another HTML code block
10+
11+
```html
12+
<div id="name">World</div>
13+
```
14+
15+
```js
16+
cy.get('div#greeting').should('have.text', 'Hello')
17+
cy.get('div#name').should('have.text', 'World')
18+
```
19+
20+
<!-- fiddle-end -->

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
},
3333
"homepage": "https://github.com/bahmutov/cypress-markdown-preprocessor#readme",
3434
"dependencies": {
35-
"@bahmutov/cypress-fiddle": "2.2.1",
35+
"@bahmutov/cypress-fiddle": "2.5.0",
3636
"@cypress/browserify-preprocessor": "3.0.2",
3737
"@textlint/markdown-to-ast": "12.2.1",
3838
"chokidar": "3.5.3",

src/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ const bundleMdFile = (filePath, outputPath) => {
3030
}
3131

3232
const specSource = source`
33-
const { testExamples } = require('${fiddleModulePath}');
34-
const fiddles = ${createTestsText};
35-
testExamples(fiddles);
36-
`
33+
const { testExamples } = require('${fiddleModulePath}');
34+
const fiddles = ${createTestsText};
35+
testExamples(fiddles);
36+
`
3737
const writtenTempFilename = tempWrite.sync(
3838
specSource,
3939
path.basename(filePath) + '.js',

src/markdown-utils.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -265,9 +265,18 @@ function extractFiddles(md) {
265265
const ast = parse(fiddle.fiddle)
266266
// console.log('markdown fiddle AST')
267267
// console.log(ast)
268-
const htmlCodeBlockMaybe = ast.children.find(
269-
(s) => isHtmlCodeBlock(s) && shouldIncludeBlock(s),
270-
)
268+
269+
const htmlMaybe = ast.children
270+
.filter(isHtmlCodeBlock)
271+
.filter(shouldIncludeBlock)
272+
.map((htmlCodeBlock) => {
273+
return {
274+
source: htmlCodeBlock.value,
275+
hide: false,
276+
}
277+
})
278+
// console.log(htmlMaybe)
279+
271280
const htmlLiveBlockMaybe = ast.children.find(
272281
(s) =>
273282
isLiveHtml(s) &&
@@ -290,15 +299,18 @@ function extractFiddles(md) {
290299
// console.log(jsMaybe)
291300
const testCode = jsMaybe.map((b) => b.value).join('\n')
292301

293-
const htmlNode = htmlLiveBlockMaybe || htmlCodeBlockMaybe
294302
const commonHtml = htmlMarkup
295303
? extractFiddleMarkup(htmlMarkup.value)
296304
: null
297305

298306
const testFiddle = formFiddleObject({
299307
name: fiddle.name,
300308
test: testCode,
301-
html: htmlNode ? htmlNode.value : null,
309+
html: htmlLiveBlockMaybe
310+
? htmlLiveBlockMaybe.value
311+
: htmlMaybe.length
312+
? htmlMaybe
313+
: null,
302314
commonHtml,
303315
only: fiddle.only,
304316
skip: fiddle.skip,

0 commit comments

Comments
 (0)