11import { Text } from "../../src/renderer/shapes/text" ;
2+ import { measureText } from "../../src/renderer/utils/text" ;
23const OS_PLATFORM = process . platform ;
34
45function expectValueBySystem ( value : any , macValue : any , winValue : any ) {
@@ -11,6 +12,46 @@ function expectValueBySystem(value: any, macValue: any, winValue: any) {
1112 }
1213}
1314
15+ function expectWrappedTextWithinWidth (
16+ sourceText : string ,
17+ wrappedText : string ,
18+ width : number
19+ ) {
20+ const measureConfigs = { text : "" , x : 0 , y : 0 , width } ;
21+ expect ( sourceText . startsWith ( wrappedText ) ) . toBe ( true ) ;
22+ expect ( measureText ( wrappedText , measureConfigs ) ) . toBeLessThanOrEqual ( width ) ;
23+
24+ if ( wrappedText . length < sourceText . length ) {
25+ expect (
26+ measureText ( sourceText . slice ( 0 , wrappedText . length + 1 ) , measureConfigs )
27+ ) . toBeGreaterThan ( width ) ;
28+ }
29+ }
30+
31+ function expectEllipsizedTextWithinWidth (
32+ sourceText : string ,
33+ ellipsizedText : string ,
34+ width : number
35+ ) {
36+ const measureConfigs = { text : "" , x : 0 , y : 0 , width } ;
37+ expect ( ellipsizedText . endsWith ( "..." ) ) . toBe ( true ) ;
38+
39+ const visibleText = ellipsizedText . slice ( 0 , - 3 ) ;
40+ expect ( sourceText . startsWith ( visibleText ) ) . toBe ( true ) ;
41+ expect ( measureText ( ellipsizedText , measureConfigs ) ) . toBeLessThanOrEqual (
42+ width
43+ ) ;
44+
45+ if ( visibleText . length < sourceText . length ) {
46+ expect (
47+ measureText (
48+ `${ sourceText . slice ( 0 , visibleText . length + 1 ) } ...` ,
49+ measureConfigs
50+ )
51+ ) . toBeGreaterThan ( width ) ;
52+ }
53+ }
54+
1455describe ( "src/shapes/text.ts" , ( ) => {
1556 it ( "should work without width & height" , ( ) => {
1657 const text = new Text ( {
@@ -161,29 +202,29 @@ describe("src/shapes/text.ts", () => {
161202 } ) ;
162203
163204 expect ( text . getDrawText ( ) ) . toEqual ( [ "a" , "b" , "c" , "d" , "e" ] ) ;
164- expect ( text . getBBox ( ) . top ) . toBe ( - 45 ) ;
165- expect ( text . getBBox ( ) . height ) . toBe ( 90 ) ;
205+ const bbox = text . getBBox ( ) ;
206+ expect ( bbox . height ) . toBe ( text . getDrawText ( ) . length * text . getLineHeight ( ) ) ;
207+ expect ( bbox . top ) . toBe ( - bbox . height / 2 ) ;
166208 } ) ;
167209
168210 it ( "bugfix: 文本阶段长度计算" , ( ) => {
211+ const sourceText = "支付用户渗透率支付用户渗透率" ;
212+ const width = 122 ;
169213 const text = new Text ( {
170- text : "支付用户渗透率支付用户渗透率" ,
214+ text : sourceText ,
171215 x : 0 ,
172216 y : 0 ,
173- width : 122 ,
217+ width,
174218 } ) ;
175- if ( OS_PLATFORM === "linux" ) {
176- expect ( text . getDrawText ( ) ) . toEqual ( [ "支付用户渗透率支" ] ) ;
177- } else {
178- expect ( text . getDrawText ( ) ) . toEqual ( [ "支付用户渗透率支付用" ] ) ;
179- }
219+
220+ const drawText = text . getDrawText ( ) ;
221+ expect ( drawText ) . toHaveLength ( 1 ) ;
222+ expectWrappedTextWithinWidth ( sourceText , drawText [ 0 ] , width ) ;
180223
181224 text . set ( "textOverflow" , "ellipsis" ) ;
182- if ( OS_PLATFORM === "linux" ) {
183- expect ( text . getDrawText ( ) ) . toEqual ( [ "支付用户渗透率..." ] ) ;
184- } else {
185- expect ( text . getDrawText ( ) ) . toEqual ( [ "支付用户渗透率支付..." ] ) ;
186- }
225+ const ellipsizedText = text . getDrawText ( ) ;
226+ expect ( ellipsizedText ) . toHaveLength ( 1 ) ;
227+ expectEllipsizedTextWithinWidth ( sourceText , ellipsizedText [ 0 ] , width ) ;
187228 } ) ;
188229
189230 it ( "bugfix: 连续换行情况" , ( ) => {
0 commit comments