11import React from 'react' ;
2- import { mount } from './enzyme ' ;
2+ import { fireEvent , render } from '@testing-library/react ' ;
33import Cascader from '../src' ;
4+ import { clickOption , expectOpen } from './util' ;
45
56describe ( 'Cascader.FieldNames' , ( ) => {
67 const options = [
@@ -34,26 +35,31 @@ describe('Cascader.FieldNames', () => {
3435
3536 it ( 'customize' , ( ) => {
3637 const onChange = jest . fn ( ) ;
37- const wrapper = mount (
38+ const { container } = render (
3839 < Cascader options = { options } fieldNames = { fieldNames } onChange = { onChange } /> ,
3940 ) ;
4041
4142 // Open
42- wrapper . find ( '.rc-cascader' ) . first ( ) . simulate ( 'mousedown' ) ;
43- expect ( wrapper . isOpen ( ) ) . toBeTruthy ( ) ;
43+ const cascader = container . querySelector ( '.rc-cascader' ) ;
44+ fireEvent . mouseDown ( cascader ! ) ;
45+ expectOpen ( container ) ;
4446
4547 // Check values
46- expect ( wrapper . find ( '.rc-cascader-menu' ) ) . toHaveLength ( 1 ) ;
47- expect ( wrapper . find ( '.rc-cascader-menu' ) . at ( 0 ) . find ( '.rc-cascader-menu-item' ) ) . toHaveLength ( 2 ) ;
48+ const menus = container . querySelectorAll ( '.rc-cascader-menu' ) ;
49+ expect ( menus ) . toHaveLength ( 1 ) ;
50+ const menuItems = menus [ 0 ] . querySelectorAll ( '.rc-cascader-menu-item' ) ;
51+ expect ( menuItems ) . toHaveLength ( 2 ) ;
4852
4953 // Click Bamboo
50- wrapper . clickOption ( 0 , 1 ) ;
51- expect ( wrapper . find ( '.rc-cascader-menu' ) ) . toHaveLength ( 2 ) ;
52- expect ( wrapper . find ( '.rc-cascader-menu' ) . at ( 1 ) . find ( '.rc-cascader-menu-item' ) ) . toHaveLength ( 1 ) ;
54+ clickOption ( container , 0 , 1 ) ;
55+ const updatedMenus = container . querySelectorAll ( '.rc-cascader-menu' ) ;
56+ expect ( updatedMenus ) . toHaveLength ( 2 ) ;
57+ const updatedMenuItems = updatedMenus [ 1 ] . querySelectorAll ( '.rc-cascader-menu-item' ) ;
58+ expect ( updatedMenuItems ) . toHaveLength ( 1 ) ;
5359
5460 // Click Little & Toy
55- wrapper . clickOption ( 1 , 0 ) ;
56- wrapper . clickOption ( 2 , 0 ) ;
61+ clickOption ( container , 1 , 0 ) ;
62+ clickOption ( container , 2 , 0 ) ;
5763
5864 expect ( onChange ) . toHaveBeenCalledWith (
5965 [ 'bamboo' , 'little' , 'toy' ] ,
@@ -66,7 +72,7 @@ describe('Cascader.FieldNames', () => {
6672 } ) ;
6773
6874 it ( 'defaultValue' , ( ) => {
69- const wrapper = mount (
75+ const { container } = render (
7076 < Cascader
7177 options = { options }
7278 fieldNames = { fieldNames }
@@ -76,17 +82,20 @@ describe('Cascader.FieldNames', () => {
7682 /> ,
7783 ) ;
7884
79- expect ( wrapper . find ( '.rc-cascader-content-value' ) . text ( ) ) . toEqual ( 'Bamboo / Little / Toy' ) ;
85+ const contentValue = container . querySelector ( '.rc-cascader-content-value' ) ;
86+ expect ( contentValue ?. textContent ) . toEqual ( 'Bamboo / Little / Toy' ) ;
8087
81- expect ( wrapper . find ( '.rc-cascader-menu' ) ) . toHaveLength ( 3 ) ;
82- expect ( wrapper . find ( '.rc-cascader-menu-item-active' ) ) . toHaveLength ( 3 ) ;
83- expect ( wrapper . find ( '.rc-cascader-menu-item-active' ) . at ( 0 ) . text ( ) ) . toEqual ( 'Bamboo' ) ;
84- expect ( wrapper . find ( '.rc-cascader-menu-item-active' ) . at ( 1 ) . text ( ) ) . toEqual ( 'Little' ) ;
85- expect ( wrapper . find ( '.rc-cascader-menu-item-active' ) . at ( 2 ) . text ( ) ) . toEqual ( 'Toy' ) ;
88+ const menus = container . querySelectorAll ( '.rc-cascader-menu' ) ;
89+ expect ( menus ) . toHaveLength ( 3 ) ;
90+ const activeItems = container . querySelectorAll ( '.rc-cascader-menu-item-active' ) ;
91+ expect ( activeItems ) . toHaveLength ( 3 ) ;
92+ expect ( activeItems [ 0 ] . textContent ) . toEqual ( 'Bamboo' ) ;
93+ expect ( activeItems [ 1 ] . textContent ) . toEqual ( 'Little' ) ;
94+ expect ( activeItems [ 2 ] . textContent ) . toEqual ( 'Toy' ) ;
8695 } ) ;
8796
8897 it ( 'displayRender' , ( ) => {
89- const wrapper = mount (
98+ const { container } = render (
9099 < Cascader
91100 options = { options }
92101 fieldNames = { fieldNames }
@@ -97,13 +106,12 @@ describe('Cascader.FieldNames', () => {
97106 /> ,
98107 ) ;
99108
100- expect ( wrapper . find ( '.rc-cascader-content-value' ) . text ( ) ) . toEqual (
101- 'Bamboo->Little->Toy & bamboo>>little>>toy' ,
102- ) ;
109+ const contentValue = container . querySelector ( '.rc-cascader-content-value' ) ;
110+ expect ( contentValue ?. textContent ) . toEqual ( 'Bamboo->Little->Toy & bamboo>>little>>toy' ) ;
103111 } ) ;
104112
105113 it ( 'same title & value should show correct title' , ( ) => {
106- const wrapper = mount (
114+ const { container } = render (
107115 < Cascader
108116 options = { [ { name : 'bamboo' , children : [ { name : 'little' } ] } ] }
109117 open
@@ -115,11 +123,12 @@ describe('Cascader.FieldNames', () => {
115123 /> ,
116124 ) ;
117125
118- expect ( wrapper . find ( '.rc-cascader-menu-item' ) . last ( ) . text ( ) ) . toEqual ( 'little' ) ;
126+ const menuItems = container . querySelectorAll ( '.rc-cascader-menu-item' ) ;
127+ expect ( menuItems [ menuItems . length - 1 ] . textContent ) . toEqual ( 'little' ) ;
119128 } ) ;
120129
121130 it ( 'empty should correct when label same as value' , ( ) => {
122- const wrapper = mount (
131+ const { container } = render (
123132 < Cascader
124133 options = { [ ] }
125134 open
@@ -131,12 +140,13 @@ describe('Cascader.FieldNames', () => {
131140 /> ,
132141 ) ;
133142
134- expect ( wrapper . find ( '.rc-cascader-menu-item' ) . last ( ) . text ( ) ) . toEqual ( 'Not Found' ) ;
143+ const menuItems = container . querySelectorAll ( '.rc-cascader-menu-item' ) ;
144+ expect ( menuItems [ menuItems . length - 1 ] . textContent ) . toEqual ( 'Not Found' ) ;
135145 } ) ;
136146
137147 it ( '`null` is a value in fieldNames options should throw a warning' , ( ) => {
138148 const errorSpy = jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => null ) ;
139- mount (
149+ render (
140150 < Cascader
141151 fieldNames = { fieldNames }
142152 options = { [
0 commit comments