File tree Expand file tree Collapse file tree 7 files changed +52
-1
lines changed
test/unit/specs/mount/Wrapper Expand file tree Collapse file tree 7 files changed +52
-1
lines changed Original file line number Diff line number Diff line change @@ -13,6 +13,8 @@ declare interface BaseWrapper { // eslint-disable-line no-undef
13
13
emitted ( event ? : string ) : { [ name : string ] : Array < Array < any >> } | Array < Array < any >> | void ,
14
14
emittedByOrder ( ) : Array < { name : string ; args: Array < any > } > | void ,
15
15
exists ( ) : boolean ,
16
+ visible ( ) : boolean ,
17
+ parent ( ) : Wrapper ,
16
18
hasAttribute ( attribute : string , value : string ) : boolean | void ,
17
19
hasClass ( className : string ) : boolean | void ,
18
20
hasProp ( prop : string , value : string ) : boolean | void ,
Original file line number Diff line number Diff line change 10
10
],
11
11
"scripts" : {
12
12
"build" : " node build/build.js" ,
13
- "build:test" : " NODE_ENV=test node build/build.js" ,
13
+ "build:test" : " cross-env NODE_ENV=test node build/build.js" ,
14
14
"coverage" : " cross-env NODE_ENV=coverage nyc --reporter=lcov --reporter=text npm run test:unit" ,
15
15
"docs" : " cd docs && gitbook install && gitbook serve" ,
16
16
"docs:deploy" : " build/update-docs.sh" ,
Original file line number Diff line number Diff line change @@ -36,6 +36,10 @@ export default class ErrorWrapper implements BaseWrapper {
36
36
return false
37
37
}
38
38
39
+ visible ( ) : boolean {
40
+ return false
41
+ }
42
+
39
43
hasAttribute ( ) : void {
40
44
throwError ( `find did not return ${ this . selector } , cannot call hasAttribute() on empty Wrapper` )
41
45
}
Original file line number Diff line number Diff line change @@ -42,6 +42,10 @@ export default class WrapperArray implements BaseWrapper {
42
42
return this . length > 0 && this . wrappers . every ( wrapper => wrapper . exists ( ) )
43
43
}
44
44
45
+ visible ( ) : boolean {
46
+ return this . length > 0 && this . wrappers . every ( wrapper => wrapper . visible ( ) )
47
+ }
48
+
45
49
emitted ( ) : void {
46
50
this . throwErrorIfWrappersIsEmpty ( 'emitted' )
47
51
Original file line number Diff line number Diff line change @@ -110,6 +110,14 @@ export default class Wrapper implements BaseWrapper {
110
110
return this . _emittedByOrder
111
111
}
112
112
113
+ parent ( ) : Wrapper | ErrorWrapper {
114
+ const parentVNode = this . vnode . parent
115
+ if ( parentVNode ) {
116
+ return createWrapper ( parentVNode , this . update , this . options )
117
+ }
118
+ return new ErrorWrapper ( 'No parent found' )
119
+ }
120
+
113
121
/**
114
122
* Utility to check wrapper exists. Returns true as Wrapper always exists
115
123
*/
@@ -120,6 +128,24 @@ export default class Wrapper implements BaseWrapper {
120
128
return true
121
129
}
122
130
131
+ /**
132
+ * Utility to check wrapper is visible. Returns false if a parent element has display: none or visibility: hidden style.
133
+ */
134
+ visible ( ) : boolean {
135
+ if ( ! this . exists ( ) ) {
136
+ return false
137
+ }
138
+
139
+ let parent = this
140
+ while ( parent . exists ( ) ) {
141
+ if ( parent . element . style . visibility === 'hidden' || parent . element . style . display === 'none' ) {
142
+ return false
143
+ }
144
+ parent = parent . parent
145
+ }
146
+ return true
147
+ }
148
+
123
149
/**
124
150
* Checks if wrapper has an attribute with matching value
125
151
*/
Original file line number Diff line number Diff line change
1
+ import { compileToFunctions } from 'vue-template-compiler'
2
+ import { mount } from '~vue-test-utils'
3
+ import { functionalSFCsSupported } from '~resources/test-utils'
4
+
5
+ describe ( 'parent' , ( ) => {
6
+ it ( 'returns parent element' , ( ) => {
7
+ const compiled = compileToFunctions ( '<div class="parent"><div class="child"></div></div>' )
8
+ const wrapper = mount ( compiled )
9
+ const child = wrapper . find ( '.child' )
10
+ expect ( child . parent ( ) . classes ( ) ) . to . include ( 'parent' )
11
+ } )
12
+ } )
Original file line number Diff line number Diff line change @@ -45,6 +45,7 @@ type RefSelector = {
45
45
interface BaseWrapper {
46
46
contains ( selector : Selector ) : boolean
47
47
exists ( ) : boolean
48
+ visible ( ) : boolean
48
49
49
50
attributes ( ) : { [ name : string ] : string } | void
50
51
classes ( ) : Array < string > | void
@@ -73,6 +74,8 @@ interface Wrapper<V extends Vue> extends BaseWrapper {
73
74
readonly element : HTMLElement
74
75
readonly options : WrapperOptions
75
76
77
+ parent ( ) : Wrapper < Vue > | null
78
+
76
79
find < R extends Vue > ( selector : VueClass < R > ) : Wrapper < R >
77
80
find < R extends Vue > ( selector : ComponentOptions < R > ) : Wrapper < R >
78
81
find ( selector : FunctionalComponentOptions ) : Wrapper < Vue >
You can’t perform that action at this time.
0 commit comments