Skip to content

feat(visible): Add visible() method on Wrapper (#327) #335

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/en/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
* [text](api/wrapper/text.md)
* [trigger](api/wrapper/trigger.md)
* [update](api/wrapper/update.md)
* [visible](api/wrapper/visible.md)
* [WrapperArray](api/wrapper-array/README.md)
* [at](api/wrapper-array/at.md)
* [contains](api/wrapper-array/contains.md)
Expand All @@ -67,6 +68,7 @@
* [setProps](api/wrapper-array/setProps.md)
* [trigger](api/wrapper-array/trigger.md)
* [update](api/wrapper-array/update.md)
* [visible](api/wrapper-array/visible.md)
* [components](api/components/README.md)
* [TransitionStub](api/components/TransitionStub.md)
* [TransitionGroupStub](api/components/TransitionGroupStub.md)
Expand Down
2 changes: 2 additions & 0 deletions docs/en/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
* [text](api/wrapper/text.md)
* [trigger](api/wrapper/trigger.md)
* [update](api/wrapper/update.md)
* [visible](api/wrapper/visible.md)
* [WrapperArray](api/wrapper-array/README.md)
* [at](api/wrapper-array/at.md)
* [contains](api/wrapper-array/contains.md)
Expand All @@ -63,6 +64,7 @@
* [setProps](api/wrapper-array/setProps.md)
* [trigger](api/wrapper-array/trigger.md)
* [update](api/wrapper-array/update.md)
* [visible](api/wrapper-array/visible.md)
* [components](api/components/README.md)
* [TransitionStub](api/components/TransitionStub.md)
* [TransitionGroupStub](api/components/TransitionGroupStub.md)
Expand Down
22 changes: 22 additions & 0 deletions docs/en/api/wrapper-array/visible.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# visible()

Assert every `Wrapper` in `WrapperArray` is visible.

Returns false if at least one ancestor element has `display: none` or `visibility: hidden` style.

This can be used to assert that a component is hidden by `v-show`.

- **Returns:** `{boolean}`

- **Example:**

```js
import { mount } from 'vue-test-utils'
import { expect } from 'chai'
import Foo from './Foo.vue'

const wrapper = mount(Foo)
expect(wrapper.visible()).toBe(true)
expect(wrapper.findAll('.is-not-visible').visible()).toBe(false)
expect(wrapper.findAll('.is-visible').visible()).toBe(true)
```
21 changes: 21 additions & 0 deletions docs/en/api/wrapper/visible.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# visible()

Assert `Wrapper` or `WrapperArray` is visible.

Returns false if an ancestor element has `display: none` or `visibility: hidden` style.

Copy link
Member

@eddyerburgh eddyerburgh Jan 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add that this is useful for testing whether v-show is working correctly

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

This can be used to assert that a component is hidden by `v-show`.

- **Returns:** `{boolean}`

- **Example:**

```js
import { mount } from 'vue-test-utils'
import { expect } from 'chai'
import Foo from './Foo.vue'

const wrapper = mount(Foo)
expect(wrapper.visible()).toBe(true)
expect(wrapper.find('.is-not-visible').visible()).toBe(false)
```
1 change: 1 addition & 0 deletions flow/wrapper.flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ declare interface BaseWrapper { // eslint-disable-line no-undef
emitted(event?: string): { [name: string]: Array<Array<any>> } | Array<Array<any>> | void,
emittedByOrder(): Array<{ name: string; args: Array<any> }> | void,
exists(): boolean,
visible(): boolean | void,
hasAttribute(attribute: string, value: string): boolean | void,
hasClass(className: string): boolean | void,
hasProp(prop: string, value: string): boolean | void,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
],
"scripts": {
"build": "node build/build.js",
"build:test": "NODE_ENV=test node build/build.js",
"build:test": "cross-env NODE_ENV=test node build/build.js",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, good spot

"coverage": "cross-env NODE_ENV=coverage nyc --reporter=lcov --reporter=text npm run test:unit",
"docs": "cd docs && gitbook install && gitbook serve",
"docs:deploy": "build/update-docs.sh",
Expand Down
4 changes: 4 additions & 0 deletions src/wrappers/error-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ export default class ErrorWrapper implements BaseWrapper {
return false
}

visible (): void {
throwError(`find did not return ${this.selector}, cannot call visible() on empty Wrapper`)
}

hasAttribute (): void {
throwError(`find did not return ${this.selector}, cannot call hasAttribute() on empty Wrapper`)
}
Expand Down
6 changes: 6 additions & 0 deletions src/wrappers/wrapper-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ export default class WrapperArray implements BaseWrapper {
return this.length > 0 && this.wrappers.every(wrapper => wrapper.exists())
}

visible (): boolean {
this.throwErrorIfWrappersIsEmpty('visible')

return this.length > 0 && this.wrappers.every(wrapper => wrapper.visible())
}

emitted (): void {
this.throwErrorIfWrappersIsEmpty('emitted')

Expand Down
20 changes: 20 additions & 0 deletions src/wrappers/wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,26 @@ export default class Wrapper implements BaseWrapper {
return true
}

/**
* Utility to check wrapper is visible. Returns false if a parent element has display: none or visibility: hidden style.
*/
visible (): boolean {
let element = this.element

if (!element) {
return false
}

while (element) {
if (element.style && (element.style.visibility === 'hidden' || element.style.display === 'none')) {
return false
}
element = element.parentElement
}

return true
}

/**
* Checks if wrapper has an attribute with matching value
*/
Expand Down
19 changes: 19 additions & 0 deletions test/resources/components/component-with-v-show.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<template>
<div v-show="rootReady">
<div v-show="!ready" class="not-ready">not-ready</div>

<div v-show="ready" class="parent ready">
<div class="child ready">ready</div>
</div>
</div>
</template>

<script>
export default {
name: 'component-with-show',
data: () => ({
ready: false,
rootReady: true
})
}
</script>
137 changes: 137 additions & 0 deletions test/unit/specs/mount/Wrapper/visible.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import { compileToFunctions } from 'vue-template-compiler'
import { mount } from '~vue-test-utils'
import ComponentWithVShow from '~resources/components/component-with-v-show.vue'
import ComponentWithVIf from '~resources/components/component-with-v-if.vue'

describe('visible', () => {
it('returns true if element has no inline style', () => {
const compiled = compileToFunctions('<div><div><span class="visible"></span></div></div>')
const wrapper = mount(compiled)
const element = wrapper.find('.visible')
expect(element.visible()).to.equal(true)
})

it('returns false if element has inline style display: none', () => {
const compiled = compileToFunctions('<div><div><span style="display: none;" class="visible"></span></div></div>')
const wrapper = mount(compiled)
const element = wrapper.find('.visible')
expect(element.visible()).to.equal(false)
})

it('returns false if element has inline style visibility: hidden', () => {
const compiled = compileToFunctions('<div><div><span style="visibility: hidden;" class="visible"></span></div></div>')
const wrapper = mount(compiled)
const element = wrapper.find('.visible')
expect(element.visible()).to.equal(false)
})

it('returns true if element has v-show true', () => {
const wrapper = mount(ComponentWithVShow)
wrapper.vm.$set(wrapper.vm, 'ready', true)
wrapper.update()

const notReadyElement = wrapper.find('.not-ready')
expect(notReadyElement.visible()).to.equal(false)

const readyElement = wrapper.find('.parent.ready')
expect(readyElement.visible()).to.equal(true)
})

it('returns false if element has v-show true', () => {
const wrapper = mount(ComponentWithVShow)
wrapper.vm.$set(wrapper.vm, 'ready', true)
wrapper.update()

const notReadyElement = wrapper.find('.not-ready')
expect(notReadyElement.visible()).to.equal(false)

const readyElement = wrapper.find('.parent.ready')
expect(readyElement.visible()).to.equal(true)
})

it('returns true if parent element has v-show true', () => {
const wrapper = mount(ComponentWithVShow)
wrapper.vm.$set(wrapper.vm, 'ready', true)
wrapper.update()

const notReadyElement = wrapper.find('.not-ready')
expect(notReadyElement.visible()).to.equal(false)

const readyChildElement = wrapper.find('.child.ready')
expect(readyChildElement.visible()).to.equal(true)
})

it('returns false if parent element has v-show false', () => {
const wrapper = mount(ComponentWithVShow)
wrapper.vm.$set(wrapper.vm, 'ready', true)
wrapper.update()

const notReadyElement = wrapper.find('.not-ready')
expect(notReadyElement.visible()).to.equal(false)

const readyChildElement = wrapper.find('.child.ready')
expect(readyChildElement.visible()).to.equal(true)
})

it('returns false if root element has v-show false and parent has v-show true', () => {
const wrapper = mount(ComponentWithVShow)
wrapper.vm.$set(wrapper.vm, 'ready', true)
wrapper.vm.$set(wrapper.vm, 'rootReady', false)
wrapper.update()

const notReadyElement = wrapper.find('.not-ready')
expect(notReadyElement.visible()).to.equal(false)

const readyChildElement = wrapper.find('.child.ready')
expect(readyChildElement.visible()).to.equal(false)
})

it('returns false if root element has v-show true and parent has v-show false', () => {
const wrapper = mount(ComponentWithVShow)
wrapper.vm.$set(wrapper.vm, 'ready', false)
wrapper.vm.$set(wrapper.vm, 'rootReady', true)
wrapper.update()

const notReadyElement = wrapper.find('.not-ready')
expect(notReadyElement.visible()).to.equal(true)

const readyChildElement = wrapper.find('.child.ready')
expect(readyChildElement.visible()).to.equal(false)
})

it('returns true if all elements are visible', () => {
const wrapper = mount(ComponentWithVShow)
wrapper.vm.$set(wrapper.vm, 'ready', true)
wrapper.vm.$set(wrapper.vm, 'rootReady', true)
wrapper.update()

const readyChildElement = wrapper.find('.ready')
expect(readyChildElement.visible()).to.equal(true)
})

it('returns false if one element is not visible', () => {
const wrapper = mount(ComponentWithVShow)
wrapper.vm.$set(wrapper.vm, 'ready', true)
wrapper.vm.$set(wrapper.vm, 'rootReady', true)
wrapper.update()

const readyChildElement = wrapper.find('.ready, .not-ready')
expect(readyChildElement.visible()).to.equal(false)
})

it('fails if one element is absent', () => {
const wrapper = mount(ComponentWithVIf)
wrapper.vm.$set(wrapper.vm, 'ready', false)
wrapper.update()

const fn = () => wrapper.find('.child.ready').visible()
expect(fn).to.throw()
})

it('returns true if one element is present', () => {
const wrapper = mount(ComponentWithVIf)
wrapper.vm.$set(wrapper.vm, 'ready', true)
wrapper.update()
expect(wrapper.find('.child.ready').visible()).to.equal(true)
})
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice tests 👌

1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type RefSelector = {
interface BaseWrapper {
contains (selector: Selector): boolean
exists (): boolean
visible (): boolean

attributes(): { [name: string]: string } | void
classes(): Array<string> | void
Expand Down