Skip to content

Commit 39fa24d

Browse files
authored
feat: switch the new fiddle (#94)
* feat: add export Markdown to JavaScript bin * show the exported JS file * adding more markdown specs * feat: switch to using my fiddle
1 parent 84b131b commit 39fa24d

24 files changed

+1482
-767
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Add list item
2+
3+
In this example, we do not know how many items are in the list initially. After clicking a button we want to confirm that the list has increased by one item.
4+
5+
<!-- fiddle "Adds an item" -->
6+
7+
```html
8+
<ol id="list">
9+
<li>first</li>
10+
<li>second</li>
11+
</ol>
12+
<button id="add">Add an item</button>
13+
<script>
14+
let list = document.getElementById('list')
15+
document.getElementById('add').addEventListener('click', () => {
16+
const li = document.createElement('li')
17+
li.textContent = 'added'
18+
list.appendChild(li)
19+
})
20+
</script>
21+
```
22+
23+
```js
24+
// we do not know how many items are in the list
25+
// so first we need to find out and store this information
26+
let N = 0
27+
cy.get('#list li')
28+
.its('length')
29+
.then((n) => {
30+
N = n
31+
// we can even confirm there are a few items if needed
32+
expect(N).to.be.gt(0)
33+
})
34+
// now add an item
35+
cy.get('#add')
36+
.click()
37+
.then(() => {
38+
// by the time this callback runs the N value is set
39+
// and we can use it directly in the assertion
40+
cy.get('#list li').should('have.length', N + 1)
41+
})
42+
```
43+
44+
```js skip
45+
throw new Error('This code block should be ignored')
46+
```
47+
48+
<!-- fiddle-end -->
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
## cy.contains example
2+
3+
Trying to check [https://twitter.com/Kmaschta/status/1189120060054540289](https://twitter.com/Kmaschta/status/1189120060054540289)
4+
5+
To run this fiddle in this repo:
6+
7+
```
8+
npm run cy:md
9+
```
10+
11+
Then click on "contains-example.md" spec file.
12+
13+
<!-- fiddle cy.contains one -->
14+
```html
15+
<div id="app">nothing here</div>
16+
<script>
17+
setTimeout(() => {
18+
document.getElementById('app').innerText = 'something'
19+
}, 2000)
20+
</script>
21+
```
22+
23+
Here is a test
24+
25+
```js
26+
cy.contains('#app', 'something')
27+
// this command will only run AFTER cy.contains finishes
28+
cy.get('#app').should('be.visible')
29+
```
30+
31+
<!-- fiddle-end -->
32+
33+
<!-- fiddle split fiddle -->
34+
This fiddle has common HTML code
35+
36+
```html
37+
<div id="app">nothing here</div>
38+
<script>
39+
setTimeout(() => {
40+
document.getElementById('app').innerText = 'something'
41+
}, 2000)
42+
</script>
43+
```
44+
45+
Then first assertion
46+
47+
```js
48+
cy.contains('#app', 'something')
49+
```
50+
51+
Then more test code here
52+
53+
```js
54+
// this command will only run AFTER cy.contains finishes
55+
cy.get('#app').should('be.visible')
56+
```
57+
58+
<!-- fiddle-end -->

cypress/integration/css-example.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Getting the full CSS class list for [issue 8592](https://github.com/cypress-io/cypress/issues/8592)
2+
3+
<!-- fiddle -->
4+
5+
```html
6+
<div class="profiler__header">
7+
<div class="c-accordion c-accordion-active collapsed" id="accordion">
8+
things inside
9+
</div>
10+
</div>
11+
```
12+
13+
```js
14+
cy.get('#accordion').then($el => {
15+
console.log($el)
16+
console.log('class name "%s"', $el[0].className)
17+
})
18+
```
19+
20+
<!-- fiddle-end -->
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Does [`cy.invoke`](https://on.cypress.io/invoke) retry?
2+
3+
<!-- fiddle -->
4+
5+
```js
6+
let g = 'hello'
7+
const o = {
8+
greeting () {
9+
return g
10+
}
11+
}
12+
setTimeout(() => {
13+
g = 'bye'
14+
}, 1000)
15+
16+
// initially the o.greeting() should return "hello"
17+
// and after 1 second should return "bye" the assertion expects
18+
cy.wrap(o).invoke('greeting').should('equal', 'bye')
19+
```
20+
21+
<!-- fiddle-end -->
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Example where a `<select>` element gets dynamic list
2+
3+
<!-- fiddle Dynamic select -->
4+
5+
```html
6+
<select id="my-select"></select>
7+
8+
<script>
9+
// populate the select element dynamically
10+
const s = document.getElementById('my-select')
11+
const addOption = (x) => () => {
12+
s.innerHTML += `
13+
<option value=${x.toLowerCase()}>${x}</option>
14+
`
15+
}
16+
setTimeout(addOption('Alpha'), 500)
17+
setTimeout(addOption('Beta'), 1000)
18+
setTimeout(addOption('Gamma'), 1500)
19+
</script>
20+
```
21+
22+
```js
23+
cy.get('#my-select option').should('have.length', 3)
24+
// now that the select has 3 options, let's pick "Beta"
25+
cy.get('#my-select').select('beta')
26+
```
27+
28+
<!-- fiddle-end -->
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
Stop `cy.each` iteration https://github.com/cypress-io/cypress/issues/8652
2+
3+
<!-- fiddle -->
4+
5+
Let's say we have a list of items
6+
7+
```html
8+
<ul>
9+
<li>Apples</li>
10+
<li>Bananas</li>
11+
<li>Grapes</li>
12+
</ul>
13+
```
14+
15+
```js
16+
// iterate through all fruits
17+
cy.spy(console, 'log').as('console')
18+
cy.get('li').each(($el) => {
19+
console.log($el.text())
20+
// prints "Apples", "Bananas", "Grapes"
21+
})
22+
cy.get('@console').should('have.been.calledThrice').invoke('resetHistory')
23+
24+
// iterate over fruits, but stop when reach Bananas
25+
cy.get('li').each(($el) => {
26+
if ($el.text() === 'Bananas') {
27+
// stop iteration
28+
return false
29+
}
30+
console.log($el.text())
31+
// prints "Apples"
32+
})
33+
cy.get('@console').should('have.been.calledOnce').invoke('resetHistory')
34+
```
35+
36+
<!-- fiddle-end -->
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Hidden fiddle
2+
3+
Imagine this is a "normal" Markdown page. Can we embed a Cypress test _inside the Markdown_ that is hidden by default? Sure we can - using GitHub-flavored Markdown `<details>` section.
4+
5+
<details>
6+
<summary>Cypress test for the current page</summary>
7+
<!--
8+
make sure to have a blank line before the code block,
9+
otherwise code block is not properly rendered
10+
-->
11+
<!-- fiddle -->
12+
13+
```js
14+
expect('foo').to.equal('foo')
15+
```
16+
17+
<!-- fiddle-end -->
18+
</details>

cypress/integration/live-html.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Live HTML
2+
3+
<!-- fiddle includes live html -->
4+
5+
<div id="live-block">Live Block</div>
6+
7+
```js
8+
cy.contains('#live-block', 'Live Block').should('be.visible')
9+
```
10+
<!-- fiddle-end -->
11+
12+
<!-- fiddle includes html block -->
13+
```html
14+
<div id="my-block">Block</div>
15+
```
16+
```js
17+
cy.contains('#my-block', 'Block').should('be.visible')
18+
```
19+
<!-- fiddle-end -->
20+
21+
<!-- fiddle includes both live and html block -->
22+
```html
23+
<div id="my-block">Block</div>
24+
```
25+
26+
<div id="live-block">Live Block</div>
27+
28+
```js
29+
// when including both live HTML block and
30+
// html code block, the live HTML block wins
31+
cy.contains('#live-block', 'Live Block').should('be.visible')
32+
cy.contains('#my-block', 'Block').should('not.exist')
33+
```
34+
<!-- fiddle-end -->

cypress/integration/merging._md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<!--
2+
this fiddle should create (inside top level describe "nested fiddles")
3+
4+
describe("parent suite", () => {
5+
it("test A", () => {})
6+
it("test B", () => {})
7+
})
8+
-->
9+
10+
<!-- fiddle parent suite / test A -->
11+
```js
12+
expect('test A').to.equal('test A')
13+
// check the current test
14+
expect(this.test.title, 'test title').to.equal('test A')
15+
expect(this.test.parent.title, 'parent title').to.equal('parent suite')
16+
expect(this.test.parent.tests.length, 'parent suite has 2 tests').to.equal(2)
17+
```
18+
<!-- fiddle-end -->
19+
20+
<!-- fiddle parent suite / test B -->
21+
```js
22+
expect('test B').to.equal('test B')
23+
// check the current test
24+
expect(this.test.title, 'test title').to.equal('test B')
25+
expect(this.test.parent.title, 'parent title').to.equal('parent suite')
26+
expect(this.test.parent.tests.length, 'parent suite has 2 tests').to.equal(2)
27+
```
28+
<!-- fiddle-end -->
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# nested fiddles
2+
3+
<!--
4+
this fiddle should create (inside top level describe "nested fiddles")
5+
6+
describe("parent suite", () => {
7+
context("child suite", () => {
8+
it("test itself", () => {}
9+
})
10+
})
11+
-->
12+
13+
<!-- fiddle parent suite / child suite / test itself -->
14+
15+
```js
16+
expect('fiddle').to.equal('fiddle')
17+
// check the current test
18+
expect(this.test.title, 'test title').to.equal('test itself')
19+
expect(this.test.parent.title, 'child suite').to.equal('child suite')
20+
expect(this.test.parent.parent.title, 'parent suite').to.equal(
21+
'parent suite',
22+
)
23+
expect(
24+
this.test.parent.parent.parent.title,
25+
'top level suite',
26+
).to.equal('nested fiddles')
27+
```
28+
29+
<!-- fiddle-end -->

0 commit comments

Comments
 (0)