This repository was archived by the owner on Dec 12, 2020. It is now read-only.
File tree Expand file tree Collapse file tree 13 files changed +216
-1
lines changed
cypress/component/basic/small-examples Expand file tree Collapse file tree 13 files changed +216
-1
lines changed Original file line number Diff line number Diff line change @@ -516,6 +516,7 @@ Spec | Description
516
516
[ Mixins] ( cypress/component/basic/mixins ) | Registering Vue mixins
517
517
[ Plugins] ( cypress/component/basic/plugins ) | Loading additional plugins
518
518
[ 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
519
520
<!-- prettier-ignore-end -->
520
521
521
522
### Advanced examples
@@ -551,7 +552,12 @@ Repo | Description
551
552
552
553
## Known problems
553
554
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 >
555
561
556
562
<a name =" bundling " />
557
563
Original file line number Diff line number Diff line change
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
+ } )
Original file line number Diff line number Diff line change
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 >
Original file line number Diff line number Diff line change
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
+ } )
Original file line number Diff line number Diff line change
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 >
Original file line number Diff line number Diff line change
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
+ } )
Original file line number Diff line number Diff line change
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 >
Original file line number Diff line number Diff line change
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.
Original file line number Diff line number Diff line change
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
+ } )
Original file line number Diff line number Diff line change
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 >
You can’t perform that action at this time.
0 commit comments