Skip to content
This repository was archived by the owner on Dec 12, 2020. It is now read-only.

Commit bf0ace3

Browse files
authored
Add example from the blog post (#363)
1 parent aa023eb commit bf0ace3

File tree

13 files changed

+216
-1
lines changed

13 files changed

+216
-1
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,7 @@ Spec | Description
516516
[Mixins](cypress/component/basic/mixins) | Registering Vue mixins
517517
[Plugins](cypress/component/basic/plugins) | Loading additional plugins
518518
[Props](cypress/component/basic/props) | Pass props to the component during mount
519+
[Small examples](cypress/component/basic/small-examples) | A few small examples testing forms, buttons
519520
<!-- prettier-ignore-end -->
520521

521522
### Advanced examples
@@ -551,7 +552,12 @@ Repo | Description
551552

552553
## Known problems
553554

554-
See issues labeled [v2](https://github.com/bahmutov/cypress-vue-unit-test/labels/v2)
555+
<details id="slots">
556+
<summary>Slots not supported</summary>
557+
558+
See issue [#364](https://github.com/bahmutov/cypress-vue-unit-test/issues/364)
559+
560+
</details>
555561

556562
<a name="bundling"/>
557563

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/// <reference types="cypress" />
2+
3+
import AppInput from './AppInput.vue'
4+
import { mount } from 'cypress-vue-unit-test'
5+
6+
it('renders label', () => {
7+
mount(AppInput, {
8+
propsData: {
9+
name: 'username',
10+
inputId: 'username',
11+
},
12+
// passing slots to the component #364
13+
slots: {
14+
label: `<label for="username">Enter Username</label>`,
15+
},
16+
})
17+
18+
// input element is present
19+
cy.get('input#username')
20+
21+
// Get input field by label text we passed as slot
22+
// enable once #364 is working
23+
// cy.contains('label', 'Enter Username')
24+
})
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<template>
2+
<div>
3+
<slot name="label"></slot>
4+
<input type="text" :name="name" :id="inputId" />
5+
</div>
6+
</template>
7+
<script>
8+
export default {
9+
props: {
10+
name: {
11+
required: true,
12+
type: String,
13+
},
14+
inputId: {
15+
required: true,
16+
type: String,
17+
},
18+
},
19+
};
20+
</script>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/// <reference types="cypress" />
2+
3+
import Counter from './Counter.vue'
4+
import { mount } from 'cypress-vue-unit-test'
5+
6+
describe('Counter', () => {
7+
it('renders correctly', () => {
8+
mount(Counter)
9+
cy.contains('Count: 0')
10+
})
11+
12+
it('correctly responds to button clicks', () => {
13+
mount(Counter)
14+
cy.contains('Count: 0')
15+
16+
// save button aliases
17+
cy.contains('button', 'Add').as('add')
18+
cy.contains('button', 'Subtract').as('subtract')
19+
20+
cy.get('@add').click()
21+
cy.contains('Count: 1')
22+
23+
cy.get('@subtract').click()
24+
cy.contains('Count: 0')
25+
26+
cy.get('@add').click().click().click().click()
27+
cy.contains('Count: 4')
28+
29+
cy.get('@subtract').click().click()
30+
cy.contains('Count: 2')
31+
})
32+
})
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<template>
2+
<div>
3+
<h3>Count: {{ counter }}</h3>
4+
<div class="button-container">
5+
<button @click="increment">Add</button>
6+
<button @click="decrement">Subtract</button>
7+
</div>
8+
</div>
9+
</template>
10+
11+
<script>
12+
export default {
13+
name: 'Counter',
14+
data() {
15+
return {
16+
counter: 0,
17+
};
18+
},
19+
methods: {
20+
increment() {
21+
this.counter++;
22+
},
23+
decrement() {
24+
this.counter--;
25+
},
26+
},
27+
};
28+
</script>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/// <reference types="cypress" />
2+
3+
import Form from './Form.vue'
4+
import { mount } from 'cypress-vue-unit-test'
5+
6+
describe('Form', () => {
7+
const getByLabelText = (text) => {
8+
return cy
9+
.contains('label', text)
10+
.invoke('attr', 'for')
11+
.then((id) => {
12+
return cy.get('input#' + id)
13+
})
14+
}
15+
it('User can type and see output on the screen', () => {
16+
mount(Form)
17+
// save references to input fields
18+
getByLabelText('Name').as('name')
19+
getByLabelText('Email').as('email')
20+
cy.contains('Submit').as('submit')
21+
22+
// initially the submit button is disabled
23+
cy.get('@submit').should('be.disabled')
24+
25+
// Update the name field.
26+
cy.get('@name').type('James John')
27+
cy.get('@submit').should('be.disabled')
28+
29+
// Add email.
30+
cy.get('@email').type('james@example.com')
31+
cy.get('@submit').should('not.be.disabled')
32+
})
33+
})
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<template>
2+
<form action="">
3+
<label for="name">Name</label>
4+
<input type="text" name="name" id="name" v-model="name" />
5+
<label for="email">Email</label>
6+
<input type="email" name="email" id="email" v-model="email" />
7+
<button type="submit" :disabled="!hasValidFields">Submit</button>
8+
</form>
9+
</template>
10+
<script>
11+
export default {
12+
name: 'AppForm',
13+
data() {
14+
return {
15+
name: '',
16+
email: '',
17+
};
18+
},
19+
computed: {
20+
hasValidFields() {
21+
return Boolean(this.email && this.name);
22+
},
23+
},
24+
};
25+
</script>
26+
<style></style>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Small examples
2+
3+
These examples come from the blog post [Testing Vue components with Vue Testing Library](https://blog.logrocket.com/testing-vue-components-with-vue-testing-library/) and the tests are written using `cypress-vue-unit-test`.
4+
5+
Tests for [Counter.vue](Counter.vue) are in [Counter.spec.js](Counter.spec.js)
6+
7+
![Counter spec](images/counter.png)
8+
9+
Tests for [Repeater.vue](Repeater.vue) are in [Repeater.spec.js](Repeater.spec.js)
10+
11+
![Repeater spec](images/repeater.png)
12+
13+
Tests for [Form.vue](Form.vue) are in [Form.spec.js](Form.spec.js)
14+
15+
![Form spec](images/form.gif)
16+
17+
Tests for [AppInput.vue](AppInput.vue) in [AppInput.spec.js](AppInput.spec.js) show how to pass a `slot` template to a component while mounting it.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// <reference types="cypress" />
2+
3+
import Repeater from './Repeater.vue'
4+
import { mount } from 'cypress-vue-unit-test'
5+
6+
describe('Repeater', () => {
7+
it('User can type and see output on the screen', () => {
8+
mount(Repeater)
9+
cy.get('#item').type('Cypress')
10+
cy.contains('You typed: Cypress')
11+
})
12+
})
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<template>
2+
<section>
3+
<label for="item">Start Typing</label>
4+
<input type="text" v-model="item" id="item" />
5+
<div>You typed: {{ item }}</div>
6+
</section>
7+
</template>
8+
<script>
9+
export default {
10+
data() {
11+
return {
12+
item: '',
13+
};
14+
},
15+
};
16+
</script>
17+
<style></style>

0 commit comments

Comments
 (0)