-
Notifications
You must be signed in to change notification settings - Fork 665
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
||
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) | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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", | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice tests 👌 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done