Skip to content

Commit

Permalink
chore: add cy test
Browse files Browse the repository at this point in the history
  • Loading branch information
yuwu9145 committed Sep 22, 2024
1 parent bc9ce26 commit d61e495
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 5 deletions.
11 changes: 7 additions & 4 deletions packages/vuetify/src/labs/VTreeview/VTreeview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,13 @@ export const VTreeview = genericComponent<new <T>(
if (!search.value) return null
const getPath = vListRef.value?.getPath
if (!getPath) return null
return new Set(filteredItems.value.flatMap(item => [
...getPath(props.returnObject ? toRaw(item.raw) : item.props.value),
...getChildren(props.returnObject ? toRaw(item.raw) : item.props.value)]
))
return new Set(filteredItems.value.flatMap(item => {
const itemVal = props.returnObject ? item.raw : item.props.value
return [
...getPath(itemVal),
...getChildren(itemVal),
]
}))
})

function getChildren (id: unknown) {
Expand Down
103 changes: 102 additions & 1 deletion packages/vuetify/src/labs/VTreeview/__tests__/VTreeview.spec.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

// Components
import { VTreeview } from '../VTreeview'
import { VTextField } from '@/components/VTextField'

// Utilities
import { ref } from 'vue'
import { ref, shallowRef } from 'vue'

function compareItemObject (a: any, b: any) {
return a.id - b.id
Expand Down Expand Up @@ -849,6 +850,106 @@ describe('VTreeview', () => {
})
})
})
describe('search', () => {
// https://github.com/vuetifyjs/vuetify/issues/20488
it('should filter items based on the search text and return the correct result', () => {
const items = ref([
{
id: 1,
title: 'Vuetify Human Resources',
children: [
{
id: 2,
title: 'Core team',
children: [
{
id: 201,
title: 'John',
},
{
id: 202,
title: 'Kael',
},
{
id: 203,
title: 'Nekosaur',
},
{
id: 204,
title: 'Jacek',
},
{
id: 205,
title: 'Andrew',
},
],
},
{
id: 3,
title: 'Administrators',
children: [
{
id: 301,
title: 'Mike',
},
{
id: 302,
title: 'Hunt',
},
],
},
{
id: 4,
title: 'Contributors',
children: [
{
id: 401,
title: 'Phlow',
},
{
id: 402,
title: 'Brandon',
},
{
id: 403,
title: 'Sean',
},
],
},
],
},
])
const search = shallowRef('')

function filterFn (value: string, search: string) {
return value.toLowerCase().includes(search.toLowerCase())
}
cy.mount(() => (
<>
<VTextField v-model={ search.value } />
<VTreeview
customFilter={ filterFn }
search={ search.value }
items={ items.value }
item-title="title"
item-value="id"
open-all
return-object
/>
</>
))
.get('.v-text-field input')
.type('j')
.get('.v-treeview-item').eq(0).should('be.visible') // { id: 1, title: 'Vuetify Human Resources' }
.get('.v-treeview-item').eq(1).should('be.visible') // { id: 2, title: 'Core team' }
.get('.v-treeview-item').eq(2).should('be.visible') // { id: 201, title: 'John' }
.get('.v-treeview-item').eq(3).should('not.be.visible')
.get('.v-treeview-item').eq(4).should('not.be.visible')
.get('.v-treeview-item').eq(5).should('be.visible') // { id: 204, title: 'Jacek' }
.get('.v-treeview-item').eq(9).should('not.be.visible')
.get('.v-treeview-item').eq(13).should('not.be.visible')
})
})
})

it('should have all items visible when open-all is applied', () => {
Expand Down

0 comments on commit d61e495

Please sign in to comment.