|
| 1 | +import { mount } from '@vue/test-utils'; |
| 2 | +import { Text } from '@tdesign/components'; |
| 3 | +import type { TdTextProps } from '@tdesign/components'; |
| 4 | +import { nextTick } from 'vue'; |
| 5 | + |
| 6 | +describe('Typography Text', () => { |
| 7 | + const longTextString = `TDesign was founded with the principles of open-source collaboration from the beginning. The collaboration scheme discussion, component design, and API design, including source code, are fully open within the company, garnering widespread attention from internal developers and designers. TDesign follows an equal, open, and strict policy, regardless of the participants' roles.`; |
| 8 | + const shortText = 'TDesign was founded with the principles of open-source collaboration from the beginning.'; |
| 9 | + const ellipsisText = new RegExp('...'); |
| 10 | + |
| 11 | + describe('props', () => { |
| 12 | + it(':code[Boolean]', () => { |
| 13 | + const wrapper = mount(() => <Text code>{shortText}</Text>); |
| 14 | + |
| 15 | + expect(wrapper.find('code').element.innerHTML).toMatch(new RegExp(shortText)); |
| 16 | + }); |
| 17 | + |
| 18 | + it(':content[String/Function/Slot]', () => { |
| 19 | + const defaultWrapperSlot = mount(() => <Text>{shortText}</Text>); |
| 20 | + const propWrapperString = mount(() => <Text content={shortText}></Text>); |
| 21 | + const propWrapperFunction = mount(() => <Text content={() => shortText}></Text>); |
| 22 | + const propWrapperSlot = mount(() => <Text v-slots={{ content: () => <>{shortText}</> }}></Text>); |
| 23 | + |
| 24 | + expect(defaultWrapperSlot.find('.t-typography').element.innerHTML).toMatch(new RegExp(shortText)); |
| 25 | + expect(propWrapperString.find('.t-typography').element.innerHTML).toMatch(new RegExp(shortText)); |
| 26 | + expect(propWrapperFunction.find('.t-typography').element.innerHTML).toMatch(new RegExp(shortText)); |
| 27 | + expect(propWrapperSlot.find('.t-typography').element.innerHTML).toMatch(new RegExp(shortText)); |
| 28 | + }); |
| 29 | + |
| 30 | + it(':copyable[object]', async () => { |
| 31 | + const handleCopy = vi.fn(); |
| 32 | + const renderCopySlot = () => 'test'; |
| 33 | + const wrapper = mount(() => ( |
| 34 | + <Text |
| 35 | + id="test" |
| 36 | + copyable={{ |
| 37 | + onCopy: handleCopy, |
| 38 | + suffix: renderCopySlot, |
| 39 | + tooltipProps: { trigger: 'click', attach: '#test' }, |
| 40 | + }} |
| 41 | + > |
| 42 | + {shortText} |
| 43 | + </Text> |
| 44 | + )); |
| 45 | + |
| 46 | + expect(wrapper.find('.t-button').element.innerHTML).toMatch(new RegExp('test')); |
| 47 | + |
| 48 | + wrapper.find('.t-button').trigger('click'); |
| 49 | + |
| 50 | + expect(handleCopy).toHaveBeenCalled(); |
| 51 | + }); |
| 52 | + |
| 53 | + it(':delete[Boolean]', () => { |
| 54 | + const wrapper = mount(() => <Text delete>{longTextString}</Text>); |
| 55 | + |
| 56 | + expect(wrapper.find('del').exists()).eq(true); |
| 57 | + }); |
| 58 | + |
| 59 | + it(':disabled[Boolean]', () => { |
| 60 | + const wrapper = mount(() => <Text disabled>{longTextString}</Text>); |
| 61 | + |
| 62 | + expect(wrapper.find('.t-typography').classes('t-typography--disabled')).eq(true); |
| 63 | + }); |
| 64 | + |
| 65 | + it(':ellipsis[Boolean]', () => { |
| 66 | + const wrapper = mount(() => <Text ellipsis>{longTextString}</Text>); |
| 67 | + |
| 68 | + expect(wrapper.find('.t-typography').element.innerHTML).toMatch(ellipsisText); |
| 69 | + }); |
| 70 | + |
| 71 | + it(':ellipsis[object]', async () => { |
| 72 | + const onExpand = vi.fn(); |
| 73 | + const wrapper = mount(() => ( |
| 74 | + <Text |
| 75 | + ellipsis={{ |
| 76 | + row: 1, |
| 77 | + onExpand, |
| 78 | + expandable: true, |
| 79 | + collapsible: true, |
| 80 | + tooltipProps: { content: 'tooltip content' }, |
| 81 | + }} |
| 82 | + > |
| 83 | + {longTextString} |
| 84 | + </Text> |
| 85 | + )); |
| 86 | + |
| 87 | + await nextTick(); |
| 88 | + |
| 89 | + const expandSymbol = wrapper.find('.t-typography-ellipsis-symbol'); |
| 90 | + expect(expandSymbol.exists()).toBe(true); |
| 91 | + |
| 92 | + await expandSymbol.trigger('click'); |
| 93 | + expect(onExpand).toHaveBeenCalledWith(true); |
| 94 | + |
| 95 | + const collapseSymbol = wrapper.find('.t-typography-ellipsis-symbol'); |
| 96 | + expect(collapseSymbol.exists()).toBe(true); |
| 97 | + |
| 98 | + const collapsibleWrapper = mount(() => ( |
| 99 | + <Text |
| 100 | + ellipsis={{ |
| 101 | + row: 2, |
| 102 | + expandable: true, |
| 103 | + collapsible: false, |
| 104 | + onExpand, |
| 105 | + }} |
| 106 | + > |
| 107 | + {longTextString} |
| 108 | + </Text> |
| 109 | + )); |
| 110 | + |
| 111 | + await nextTick(); |
| 112 | + |
| 113 | + const expandBtn = collapsibleWrapper.find('.t-typography-ellipsis-symbol'); |
| 114 | + await expandBtn.trigger('click'); |
| 115 | + |
| 116 | + expect(collapsibleWrapper.find('.t-typography-ellipsis-symbol').exists()).toBe(false); |
| 117 | + }); |
| 118 | + |
| 119 | + it(':ellipsis[object(slot)]', async () => { |
| 120 | + const wrapper = mount(() => ( |
| 121 | + <Text |
| 122 | + ellipsis={{ |
| 123 | + expandable: true, |
| 124 | + suffix: () => '...更多', |
| 125 | + }} |
| 126 | + > |
| 127 | + {longTextString} |
| 128 | + </Text> |
| 129 | + )); |
| 130 | + |
| 131 | + const expandSymbol = wrapper.find('.t-typography-ellipsis-symbol'); |
| 132 | + expect(expandSymbol.exists()).toBe(true); |
| 133 | + // TODO: 组件的suffix存在问题,function会被渲染成string,计划后面再开pr fix |
| 134 | + expect(expandSymbol.text()).toBe("() => '...更多'"); |
| 135 | + }); |
| 136 | + |
| 137 | + it(':italic[Boolean]', () => { |
| 138 | + const wrapper = mount(() => <Text italic>{longTextString}</Text>); |
| 139 | + |
| 140 | + expect(wrapper.find('i').exists()).eq(true); |
| 141 | + }); |
| 142 | + |
| 143 | + it(':keyboard[Boolean]', () => { |
| 144 | + const wrapper = mount(() => <Text keyboard>{longTextString}</Text>); |
| 145 | + |
| 146 | + expect(wrapper.find('kbd').exists()).eq(true); |
| 147 | + }); |
| 148 | + |
| 149 | + it(':mark[String/Boolean]', () => { |
| 150 | + const defaultWrapper = mount(() => <Text mark>{longTextString}</Text>); |
| 151 | + const colorWrapper = mount(() => <Text mark="#07c160">{longTextString}</Text>); |
| 152 | + |
| 153 | + expect(defaultWrapper.find('mark').exists()).eq(true); |
| 154 | + expect(colorWrapper.find('mark').element.style.backgroundColor).eq('rgb(7, 193, 96)'); |
| 155 | + }); |
| 156 | + |
| 157 | + it(':strong[Boolean]', () => { |
| 158 | + const wrapper = mount(() => <Text strong>{longTextString}</Text>); |
| 159 | + |
| 160 | + expect(wrapper.find('strong').exists()).eq(true); |
| 161 | + }); |
| 162 | + |
| 163 | + const themeList: TdTextProps['theme'][] = ['primary', 'secondary', 'success', 'warning', 'error']; |
| 164 | + const classPrefix = 't-typography--'; |
| 165 | + |
| 166 | + themeList.forEach((item) => { |
| 167 | + it(`:theme[String]-${item}`, () => { |
| 168 | + const wrapper = mount(() => <Text theme={item}>{longTextString}</Text>); |
| 169 | + expect(wrapper.find('.t-typography').classes(`${classPrefix}${item}`)).eq(true); |
| 170 | + }); |
| 171 | + }); |
| 172 | + |
| 173 | + it(':underline[Boolean]', () => { |
| 174 | + const wrapper = mount(() => <Text underline>{longTextString}</Text>); |
| 175 | + |
| 176 | + expect(wrapper.find('u').exists()).eq(true); |
| 177 | + }); |
| 178 | + }); |
| 179 | +}); |
0 commit comments