Skip to content

Commit 3c55dfc

Browse files
committed
feat(visible): Add visible() and parent() methods on Wrapper (#327)
1 parent fce6e6e commit 3c55dfc

File tree

7 files changed

+52
-1
lines changed

7 files changed

+52
-1
lines changed

flow/wrapper.flow.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ declare interface BaseWrapper { // eslint-disable-line no-undef
1313
emitted(event?: string): { [name: string]: Array<Array<any>> } | Array<Array<any>> | void,
1414
emittedByOrder(): Array<{ name: string; args: Array<any> }> | void,
1515
exists(): boolean,
16+
visible(): boolean,
17+
parent(): Wrapper,
1618
hasAttribute(attribute: string, value: string): boolean | void,
1719
hasClass(className: string): boolean | void,
1820
hasProp(prop: string, value: string): boolean | void,

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
],
1111
"scripts": {
1212
"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",
1414
"coverage": "cross-env NODE_ENV=coverage nyc --reporter=lcov --reporter=text npm run test:unit",
1515
"docs": "cd docs && gitbook install && gitbook serve",
1616
"docs:deploy": "build/update-docs.sh",

src/wrappers/error-wrapper.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ export default class ErrorWrapper implements BaseWrapper {
3636
return false
3737
}
3838

39+
visible (): boolean {
40+
return false
41+
}
42+
3943
hasAttribute (): void {
4044
throwError(`find did not return ${this.selector}, cannot call hasAttribute() on empty Wrapper`)
4145
}

src/wrappers/wrapper-array.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ export default class WrapperArray implements BaseWrapper {
4242
return this.length > 0 && this.wrappers.every(wrapper => wrapper.exists())
4343
}
4444

45+
visible (): boolean {
46+
return this.length > 0 && this.wrappers.every(wrapper => wrapper.visible())
47+
}
48+
4549
emitted (): void {
4650
this.throwErrorIfWrappersIsEmpty('emitted')
4751

src/wrappers/wrapper.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,14 @@ export default class Wrapper implements BaseWrapper {
110110
return this._emittedByOrder
111111
}
112112

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+
113121
/**
114122
* Utility to check wrapper exists. Returns true as Wrapper always exists
115123
*/
@@ -120,6 +128,24 @@ export default class Wrapper implements BaseWrapper {
120128
return true
121129
}
122130

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+
123149
/**
124150
* Checks if wrapper has an attribute with matching value
125151
*/
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
})

types/index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type RefSelector = {
4545
interface BaseWrapper {
4646
contains (selector: Selector): boolean
4747
exists (): boolean
48+
visible (): boolean
4849

4950
attributes(): { [name: string]: string } | void
5051
classes(): Array<string> | void
@@ -73,6 +74,8 @@ interface Wrapper<V extends Vue> extends BaseWrapper {
7374
readonly element: HTMLElement
7475
readonly options: WrapperOptions
7576

77+
parent (): Wrapper<Vue> | null
78+
7679
find<R extends Vue> (selector: VueClass<R>): Wrapper<R>
7780
find<R extends Vue> (selector: ComponentOptions<R>): Wrapper<R>
7881
find (selector: FunctionalComponentOptions): Wrapper<Vue>

0 commit comments

Comments
 (0)