-
Notifications
You must be signed in to change notification settings - Fork 266
Add isVisible and stub transition/transition-group by default #212
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
Changes from all commits
9f343b0
42d4b61
5f375c5
3b4c136
912f4be
f1144a8
eeb74c3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/*! | ||
* isElementVisible | ||
* Adapted from https://github.com/testing-library/jest-dom | ||
* Licensed under the MIT License. | ||
*/ | ||
|
||
function isStyleVisible<T extends Element>(element: T) { | ||
if (!(element instanceof HTMLElement) && !(element instanceof SVGElement)) { | ||
return false | ||
} | ||
|
||
const { display, visibility, opacity } = element.style | ||
|
||
return ( | ||
display !== 'none' && | ||
visibility !== 'hidden' && | ||
visibility !== 'collapse' && | ||
opacity !== '0' | ||
) | ||
} | ||
|
||
function isAttributeVisible<T extends Element>(element: T) { | ||
return ( | ||
!element.hasAttribute('hidden') && | ||
(element.nodeName === 'DETAILS' ? element.hasAttribute('open') : true) | ||
) | ||
} | ||
|
||
export function isElementVisible<T extends Element>(element: T) { | ||
return ( | ||
element.nodeName !== '#comment' && | ||
isStyleVisible(element) && | ||
isAttributeVisible(element) && | ||
(!element.parentElement || isElementVisible(element.parentElement)) | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import { mount } from '../src' | ||
|
||
describe('isVisible', () => { | ||
const Comp = { | ||
template: `<div><span v-show="show" /></div>`, | ||
props: { | ||
show: { | ||
type: Boolean | ||
} | ||
} | ||
} | ||
|
||
it('returns false when element hidden via v-show', () => { | ||
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. suggestion: WDYT of adding one more pair of tests 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. Sure, good idea. 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. done! |
||
const wrapper = mount(Comp, { | ||
props: { | ||
show: false | ||
} | ||
}) | ||
|
||
expect(wrapper.find('span').isVisible()).toBe(false) | ||
}) | ||
|
||
it('returns true when element is visible via v-show', () => { | ||
const wrapper = mount(Comp, { | ||
props: { | ||
show: true | ||
} | ||
}) | ||
|
||
expect(wrapper.find('span').isVisible()).toBe(true) | ||
}) | ||
|
||
it('returns false when element parent is invisible via v-show', () => { | ||
const Comp = { | ||
template: `<div v-show="false"><span /></div>` | ||
} | ||
const wrapper = mount(Comp) | ||
|
||
expect(wrapper.find('span').isVisible()).toBe(false) | ||
}) | ||
|
||
it('element becomes hidden reactively', async () => { | ||
const Comp = { | ||
template: `<button @click="show = false" /><span v-show="show" />`, | ||
data() { | ||
return { | ||
show: true | ||
} | ||
} | ||
} | ||
const wrapper = mount(Comp) | ||
|
||
expect(wrapper.find('span').isVisible()).toBe(true) | ||
await wrapper.find('button').trigger('click') | ||
expect(wrapper.find('span').isVisible()).toBe(false) | ||
}) | ||
|
||
it('handles transitions', async () => { | ||
const Comp = { | ||
template: ` | ||
<button @click="show = false" /> | ||
<transition name="fade"> | ||
<span class="item" v-show="show"> | ||
Content | ||
</span> | ||
</transition> | ||
`, | ||
data() { | ||
return { | ||
show: true | ||
} | ||
} | ||
} | ||
const wrapper = mount(Comp, {}) | ||
|
||
expect(wrapper.find('span').isVisible()).toBe(true) | ||
await wrapper.find('button').trigger('click') | ||
expect(wrapper.find('span').isVisible()).toBe(false) | ||
}) | ||
|
||
it('handles transition-group', async () => { | ||
const Comp = { | ||
template: ` | ||
<div id="list-demo"> | ||
<button @click="add" id="add">Add</button> | ||
<button @click="remove" id="remove">Remove</button> | ||
<transition-group name="list" tag="p"> | ||
<span v-for="item in items" :key="item" class="list-item"> | ||
Item: {{ item }} | ||
</span> | ||
</transition-group> | ||
</div> | ||
`, | ||
methods: { | ||
add() { | ||
this.items.push(2) | ||
}, | ||
remove() { | ||
this.items.splice(1) // back to [1] | ||
} | ||
}, | ||
data() { | ||
return { | ||
items: [1] | ||
} | ||
} | ||
} | ||
const wrapper = mount(Comp) | ||
|
||
expect(wrapper.html()).toContain('Item: 1') | ||
await wrapper.find('#add').trigger('click') | ||
expect(wrapper.html()).toContain('Item: 1') | ||
expect(wrapper.html()).toContain('Item: 2') | ||
await wrapper.find('#remove').trigger('click') | ||
expect(wrapper.html()).toContain('Item: 1') | ||
expect(wrapper.html()).not.toContain('Item: 2') | ||
}) | ||
}) |
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.
I guess this was battle-tested in VTU 1, but just in case here is how jQuery tests the visibility https://github.com/jquery/jquery/blob/d0ce00cdfa680f1f0c38460bc51ea14079ae8b07/src/css/hiddenVisibleSelectors.js (we implement the same logic in an Angular testing library).
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.
I just coped this from another library (dom-testing-library, or something? same as V1).
There is even more robust visible checks out there; since the primary use case is
v-show
, which does this viadisplay: none
, I think this should cover the main use cases. We could make it more robust if someone has a problem. Surprisingly how had a true visibility selector can be to implement...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.
https://github.com/testing-library/jest-dom/blob/master/src/to-be-visible.js#L25