|
| 1 | +import React from 'react'; |
| 2 | +import { describe, it, expect, render, fireEvent, vi } from '@test/utils'; |
| 3 | +import { HomeIcon, AppIcon, ChatIcon, UserIcon } from 'tdesign-icons-react'; |
| 4 | +import { TabBar, TabBarItem } from '..'; |
| 5 | + |
| 6 | +const prefix = 't'; |
| 7 | +const prefixClass = `${prefix}-tab-bar-item`; |
| 8 | +const list = [ |
| 9 | + { value: 1, label: '首页', icon: <HomeIcon /> }, |
| 10 | + { value: 2, label: '应用', icon: <AppIcon /> }, |
| 11 | + { value: 3, label: '聊天', icon: <ChatIcon /> }, |
| 12 | + { |
| 13 | + value: 4, |
| 14 | + label: '我的', |
| 15 | + icon: <UserIcon />, |
| 16 | + subTabBar: [ |
| 17 | + { |
| 18 | + value: '4_1', |
| 19 | + label: '基本信息', |
| 20 | + }, |
| 21 | + { |
| 22 | + value: '4_2', |
| 23 | + label: '个人主页', |
| 24 | + }, |
| 25 | + { |
| 26 | + value: '4_3', |
| 27 | + label: '设置', |
| 28 | + }, |
| 29 | + ], |
| 30 | + }, |
| 31 | +]; |
| 32 | + |
| 33 | +describe('TabBarItem', () => { |
| 34 | + describe('props', () => { |
| 35 | + it(': className', () => { |
| 36 | + const { container } = render( |
| 37 | + <TabBar> |
| 38 | + {list.map((item) => ( |
| 39 | + <TabBarItem key={item.value} {...item} className="custom-class"> |
| 40 | + {item.label} |
| 41 | + </TabBarItem> |
| 42 | + ))} |
| 43 | + </TabBar>, |
| 44 | + ); |
| 45 | + |
| 46 | + expect(container.querySelectorAll('.custom-class')).toHaveLength(list.length); |
| 47 | + }); |
| 48 | + |
| 49 | + it(': style', () => { |
| 50 | + const { container } = render( |
| 51 | + <TabBar> |
| 52 | + {list.map((item) => ( |
| 53 | + <TabBarItem key={item.value} {...item} style={{ color: '#0052d9' }}> |
| 54 | + {item.label} |
| 55 | + </TabBarItem> |
| 56 | + ))} |
| 57 | + </TabBar>, |
| 58 | + ); |
| 59 | + const tabBarItems = container.querySelectorAll(`.${prefixClass}`); |
| 60 | + expect(tabBarItems).toHaveLength(list.length); |
| 61 | + expect(tabBarItems[0]).toHaveStyle({ color: '#0052d9' }); |
| 62 | + }); |
| 63 | + |
| 64 | + it(': badgeProps', () => { |
| 65 | + const { container, rerender } = render( |
| 66 | + <TabBar> |
| 67 | + {list.map((item) => ( |
| 68 | + <TabBarItem key={item.value} {...item} badgeProps={{ dot: true }}> |
| 69 | + {item.label} |
| 70 | + </TabBarItem> |
| 71 | + ))} |
| 72 | + </TabBar>, |
| 73 | + ); |
| 74 | + expect(container.querySelectorAll(`.${prefix}-badge--dot`)).toHaveLength(list.length); |
| 75 | + |
| 76 | + rerender( |
| 77 | + <TabBar> |
| 78 | + {list.map((item) => ( |
| 79 | + <TabBarItem key={item.value} {...item} badgeProps={{ count: 16 }}> |
| 80 | + {item.label} |
| 81 | + </TabBarItem> |
| 82 | + ))} |
| 83 | + </TabBar>, |
| 84 | + ); |
| 85 | + expect(container.querySelectorAll(`.${prefix}-badge__content`)).toHaveLength(list.length); |
| 86 | + }); |
| 87 | + |
| 88 | + it(': badgeProps', () => { |
| 89 | + const { container, rerender } = render( |
| 90 | + <TabBar> |
| 91 | + {list.map((item) => ( |
| 92 | + <TabBarItem key={item.value} {...item} badgeProps={{ dot: true }}> |
| 93 | + {item.label} |
| 94 | + </TabBarItem> |
| 95 | + ))} |
| 96 | + </TabBar>, |
| 97 | + ); |
| 98 | + expect(container.querySelectorAll(`.${prefix}-badge--dot`)).toHaveLength(list.length); |
| 99 | + |
| 100 | + rerender( |
| 101 | + <TabBar> |
| 102 | + {list.map((item) => ( |
| 103 | + <TabBarItem key={item.value} {...item} badgeProps={{ count: 16 }}> |
| 104 | + {item.label} |
| 105 | + </TabBarItem> |
| 106 | + ))} |
| 107 | + </TabBar>, |
| 108 | + ); |
| 109 | + expect(container.querySelectorAll(`.${prefix}-badge__content`)).toHaveLength(list.length); |
| 110 | + }); |
| 111 | + |
| 112 | + it(': icon', () => { |
| 113 | + const { container } = render( |
| 114 | + <TabBar> |
| 115 | + {list.map((item) => ( |
| 116 | + <TabBarItem key={item.value} {...item}> |
| 117 | + {item.label} |
| 118 | + </TabBarItem> |
| 119 | + ))} |
| 120 | + </TabBar>, |
| 121 | + ); |
| 122 | + |
| 123 | + expect(container.querySelector(`.${prefixClass}__icon`)).toBeTruthy(); |
| 124 | + expect(container.querySelectorAll(`.${prefixClass}__icon`)).toHaveLength(list.length); |
| 125 | + }); |
| 126 | + |
| 127 | + // tabBarItem 无value属性时,通过一个递增的 defaultIndex 生成一个临时的当前组件唯一值 |
| 128 | + it(': value', async () => { |
| 129 | + const { container } = render( |
| 130 | + <TabBar value={1}> |
| 131 | + {list.map((item) => ( |
| 132 | + <TabBarItem key={item.value}>{item.label}</TabBarItem> |
| 133 | + ))} |
| 134 | + </TabBar>, |
| 135 | + ); |
| 136 | + expect(container.querySelectorAll(`.${prefixClass}`)).toHaveLength(list.length); |
| 137 | + const activeItem = container.querySelector(`.${prefixClass}__content--checked`); |
| 138 | + expect(activeItem).toBeTruthy(); |
| 139 | + expect(activeItem).toHaveTextContent(list[1].label); |
| 140 | + }); |
| 141 | + |
| 142 | + it(': subTabBar', async () => { |
| 143 | + const onChange = vi.fn(); |
| 144 | + const { container, getByText } = render( |
| 145 | + <TabBar onChange={onChange}> |
| 146 | + {list.map((item) => ( |
| 147 | + <TabBarItem key={item.value} {...item}> |
| 148 | + {item.label} |
| 149 | + </TabBarItem> |
| 150 | + ))} |
| 151 | + </TabBar>, |
| 152 | + ); |
| 153 | + |
| 154 | + const subTabBarItem = getByText('我的'); |
| 155 | + fireEvent.click(subTabBarItem); |
| 156 | + expect(container.querySelector(`.${prefixClass}__spread`)).toBeTruthy(); |
| 157 | + expect(onChange).toHaveBeenCalled(); |
| 158 | + // expect(onChange).toHaveBeenCalledWith([4]); |
| 159 | + |
| 160 | + const subTabBarMenu = container.querySelectorAll(`.${prefixClass}__spread-item`); |
| 161 | + fireEvent.click(subTabBarMenu[0]); |
| 162 | + expect(onChange).toHaveBeenCalled(); |
| 163 | + // expect(onChange).toHaveBeenCalledWith([4, '4_1']); |
| 164 | + // setTimeout(() => { |
| 165 | + // expect(container.querySelector(`.${prefixClass}__spread`)).toBeFalsy(); |
| 166 | + // }); |
| 167 | + }); |
| 168 | + }); |
| 169 | + |
| 170 | + describe('event', () => { |
| 171 | + it(': tabBar click', async () => { |
| 172 | + const { container, getByText } = render( |
| 173 | + <TabBar> |
| 174 | + {list.map((item) => ( |
| 175 | + <TabBarItem key={item.value} {...item}> |
| 176 | + {item.label} |
| 177 | + </TabBarItem> |
| 178 | + ))} |
| 179 | + </TabBar>, |
| 180 | + ); |
| 181 | + |
| 182 | + const subTabBarItem = getByText('我的'); |
| 183 | + fireEvent.click(subTabBarItem); |
| 184 | + expect(container.querySelector(`.${prefixClass}__spread`)).toBeTruthy(); |
| 185 | + fireEvent.click(subTabBarItem); |
| 186 | + expect(container.querySelector(`.${prefixClass}__spread`)).toBeTruthy(); |
| 187 | + }); |
| 188 | + }); |
| 189 | +}); |
0 commit comments