1- import { render } from '@testing-library/react' ;
1+ import { render , screen , waitFor } from '@testing-library/react' ;
2+ import '@testing-library/jest-dom' ;
3+ import userEvent from '@testing-library/user-event' ;
24import DataViewTreeFilter , { DataViewTreeFilterProps } from './DataViewTreeFilter' ;
35import DataViewToolbar from '../DataViewToolbar' ;
46import { TreeViewDataItem } from '@patternfly/react-core' ;
57
68describe ( 'DataViewTreeFilter component' , ( ) => {
79 const treeItems : TreeViewDataItem [ ] = [
810 {
9- name : 'Development Workspace' ,
10- id : 'workspace-dev' ,
11- checkProps : { 'aria-label' : 'dev-workspace-check' , checked : false }
11+ name : 'Linux' ,
12+ id : 'os-linux' ,
13+ checkProps : { 'aria-label' : 'linux-check' , checked : false } ,
14+ children : [
15+ {
16+ name : 'Ubuntu 22.04' ,
17+ id : 'os-ubuntu' ,
18+ checkProps : { checked : false }
19+ } ,
20+ {
21+ name : 'RHEL 9' ,
22+ id : 'os-rhel' ,
23+ checkProps : { checked : false }
24+ } ,
25+ {
26+ name : 'Debian 12' ,
27+ id : 'os-debian' ,
28+ checkProps : { checked : false }
29+ } ,
30+ {
31+ name : 'CentOS 8' ,
32+ id : 'os-centos' ,
33+ checkProps : { checked : false }
34+ } ,
35+ {
36+ name : 'Fedora 38' ,
37+ id : 'os-fedora' ,
38+ checkProps : { checked : false }
39+ }
40+ ] ,
41+ defaultExpanded : true
1242 } ,
1343 {
14- name : 'Production Workspace' ,
15- id : 'workspace-prod' ,
16- checkProps : { 'aria-label' : 'prod-workspace-check' , checked : false }
44+ name : 'Windows' ,
45+ id : 'os-windows' ,
46+ checkProps : { 'aria-label' : 'windows-check' , checked : false } ,
47+ children : [
48+ {
49+ name : 'Windows Server 2022' ,
50+ id : 'os-windows-2022' ,
51+ checkProps : { checked : false }
52+ }
53+ ]
1754 } ,
1855 {
19- name : 'Operating Systems ' ,
20- id : 'os-parent ' ,
21- checkProps : { 'aria-label' : 'os -check' , checked : false } ,
56+ name : 'macOS ' ,
57+ id : 'os-macos ' ,
58+ checkProps : { 'aria-label' : 'macos -check' , checked : false } ,
2259 children : [
2360 {
24- name : 'Linux ' ,
25- id : 'os-linux ' ,
61+ name : 'macOS Ventura ' ,
62+ id : 'os-macos-ventura ' ,
2663 checkProps : { checked : false }
2764 } ,
2865 {
29- name : 'Windows ' ,
30- id : 'os-windows ' ,
66+ name : 'macOS Sonoma ' ,
67+ id : 'os-macos-sonoma ' ,
3168 checkProps : { checked : false }
3269 }
3370 ]
@@ -40,11 +77,146 @@ describe('DataViewTreeFilter component', () => {
4077 value : [ 'Linux' ] ,
4178 items : treeItems
4279 } ;
80+ beforeEach ( ( ) => {
81+ jest . clearAllMocks ( ) ;
82+ } ) ;
4383
4484 it ( 'should render correctly' , ( ) => {
4585 const { container } = render (
4686 < DataViewToolbar filters = { < DataViewTreeFilter { ...defaultProps } /> } />
4787 ) ;
4888 expect ( container ) . toMatchSnapshot ( ) ;
4989 } ) ;
90+ describe ( 'defaultExpanded' , ( ) => {
91+ it ( 'should have expanded items by default' , async ( ) => {
92+ render (
93+ < DataViewToolbar
94+ filters = {
95+ < DataViewTreeFilter
96+ filterId = "os"
97+ title = "Operating System"
98+ items = { treeItems }
99+ defaultExpanded = { true }
100+ />
101+ }
102+ />
103+ ) ;
104+
105+ const openMenu = screen . getByRole ( 'button' , { name : / o p e r a t i n g s y s t e m / i } ) ;
106+ await userEvent . click ( openMenu ) ;
107+ await waitFor ( ( ) => {
108+ const node = screen . getByText ( 'Ubuntu 22.04' ) ;
109+ expect ( node ) . toHaveClass ( 'pf-v6-c-tree-view__node-text' ) ;
110+ expect ( node ) . toBeInTheDocument ( ) ;
111+ } ) ;
112+ } ) ;
113+ } ) ;
114+ describe ( 'onChange callback' , ( ) => {
115+ it ( 'onChange should be called on toggle of node' , async ( ) => {
116+ const mockOnChange = jest . fn ( ) ;
117+ render (
118+ < DataViewToolbar
119+ filters = {
120+ < DataViewTreeFilter
121+ filterId = "os"
122+ title = "Operating System"
123+ items = { treeItems }
124+ defaultExpanded = { true }
125+ onChange = { mockOnChange }
126+ />
127+ }
128+ />
129+ ) ;
130+
131+ const openMenu = screen . getByRole ( 'button' , { name : / o p e r a t i n g s y s t e m / i } ) ;
132+ await userEvent . click ( openMenu ) ;
133+
134+ await waitFor ( ( ) => {
135+ const node = screen . getByText ( 'Ubuntu 22.04' ) ;
136+ expect ( node ) . toBeInTheDocument ( ) ;
137+ } ) ;
138+
139+ const node = screen . getByText ( 'Ubuntu 22.04' ) ;
140+ await userEvent . click ( node ) ;
141+
142+ await waitFor ( ( ) => {
143+ expect ( mockOnChange ) . toHaveBeenCalled ( ) ;
144+ } ) ;
145+ } ) ;
146+ } ) ;
147+ describe ( 'onSelect callback' , ( ) => {
148+ it ( 'onSelect should return list of selected items when item is selected' , async ( ) => {
149+ const mockOnSelect = jest . fn ( ) ;
150+ render (
151+ < DataViewToolbar
152+ filters = {
153+ < DataViewTreeFilter
154+ filterId = "os"
155+ title = "Operating System"
156+ items = { treeItems }
157+ defaultExpanded = { true }
158+ onSelect = { mockOnSelect }
159+ />
160+ }
161+ />
162+ ) ;
163+
164+ const openMenu = screen . getByRole ( 'button' , { name : / o p e r a t i n g s y s t e m / i } ) ;
165+ await userEvent . click ( openMenu ) ;
166+
167+ await waitFor ( ( ) => {
168+ const node = screen . getByText ( 'Ubuntu 22.04' ) ;
169+ expect ( node ) . toBeInTheDocument ( ) ;
170+ } ) ;
171+
172+ const node = screen . getByText ( 'Ubuntu 22.04' ) ;
173+ await userEvent . click ( node ) ;
174+
175+ await waitFor ( ( ) => {
176+ expect ( mockOnSelect ) . toHaveBeenCalled ( ) ;
177+ expect ( mockOnSelect ) . toHaveBeenCalledWith (
178+ expect . arrayContaining ( [
179+ expect . objectContaining ( {
180+ name : 'Ubuntu 22.04' ,
181+ id : 'os-ubuntu'
182+ } )
183+ ] )
184+ ) ;
185+ } ) ;
186+ } ) ;
187+ } ) ;
188+
189+ describe ( 'rendering all items' , ( ) => {
190+ it ( 'all tree items should be rendered' , async ( ) => {
191+ render (
192+ < DataViewToolbar
193+ filters = {
194+ < DataViewTreeFilter
195+ filterId = "os"
196+ title = "Operating System"
197+ items = { treeItems }
198+ defaultExpanded = { true }
199+ />
200+ }
201+ />
202+ ) ;
203+
204+ const openMenu = screen . getByRole ( 'button' , { name : / o p e r a t i n g s y s t e m / i } ) ;
205+ await userEvent . click ( openMenu ) ;
206+
207+ await waitFor ( ( ) => {
208+ expect ( screen . getByText ( 'Linux' ) ) . toBeInTheDocument ( ) ;
209+ expect ( screen . getByText ( 'Windows' ) ) . toBeInTheDocument ( ) ;
210+ expect ( screen . getByText ( 'macOS' ) ) . toBeInTheDocument ( ) ;
211+ expect ( screen . getByText ( 'Ubuntu 22.04' ) ) . toBeInTheDocument ( ) ;
212+ expect ( screen . getByText ( 'RHEL 9' ) ) . toBeInTheDocument ( ) ;
213+ expect ( screen . getByText ( 'Debian 12' ) ) . toBeInTheDocument ( ) ;
214+ expect ( screen . getByText ( 'CentOS 8' ) ) . toBeInTheDocument ( ) ;
215+ expect ( screen . getByText ( 'Fedora 38' ) ) . toBeInTheDocument ( ) ;
216+ expect ( screen . getByText ( 'Windows Server 2022' ) ) . toBeInTheDocument ( ) ;
217+ expect ( screen . getByText ( 'macOS Ventura' ) ) . toBeInTheDocument ( ) ;
218+ expect ( screen . getByText ( 'macOS Sonoma' ) ) . toBeInTheDocument ( ) ;
219+ } ) ;
220+ } ) ;
221+ } ) ;
50222} ) ;
0 commit comments