@@ -5,7 +5,6 @@ import unexpectedDOM from 'unexpected-dom';
55import sinon from 'sinon' ;
66import { jsdom } from 'jsdom' ;
77import requestAnimationFrame , { cancel as cancelAnimationFrame } from 'raf' ;
8- import Canvas from 'canvas' ;
98import React , { Component } from 'react' ;
109import { render , unmountComponentAtNode } from 'react-dom' ;
1110import { renderToString } from 'react-dom/server' ;
@@ -36,6 +35,9 @@ const expect = unexpected.clone()
3635 } )
3736;
3837
38+ const characterWidth = 6 ; // px
39+ const measureWidth = text => text . length * characterWidth ;
40+
3941describe ( '<Truncate />' , ( ) => {
4042 it ( 'should be a React component' , ( ) => {
4143 expect ( Truncate , 'to be a' , Component . constructor ) ;
@@ -72,7 +74,6 @@ describe('<Truncate />', () => {
7274 before ( ( ) => {
7375 global . document = jsdom ( ) ;
7476 global . window = global . document . defaultView ;
75- global . window . Canvas = Canvas ;
7677 global . window . requestAnimationFrame = requestAnimationFrame ;
7778 global . window . cancelAnimationFrame = cancelAnimationFrame ;
7879 Object . defineProperty ( global . window . HTMLElement . prototype , 'innerText' , {
@@ -110,7 +111,11 @@ describe('<Truncate />', () => {
110111 }
111112 } ) ;
112113
113- describe ( 'with a box of 85px mocked out' , ( ) => {
114+ // Mock out a box that's 16 characters wide
115+ const numCharacters = 16 ;
116+ const width = numCharacters * characterWidth ;
117+
118+ describe ( `with a box of ${ width } px mocked out` , ( ) => {
114119 const renderIntoBox = component => renderIntoDocument (
115120 < div >
116121 { component }
@@ -119,23 +124,27 @@ describe('<Truncate />', () => {
119124
120125 before ( ( ) => {
121126 sinon . stub ( global . window . HTMLDivElement . prototype ,
122- 'getBoundingClientRect' , ( ) => ( { width : 85 } )
127+ 'getBoundingClientRect' , ( ) => ( { width } )
123128 ) ;
124129
125- // Approximate .offsetWidth with context.measureText
126130 sinon . stub ( Truncate . prototype ,
127- 'ellipsisWidth' , node => {
128- const canvas = document . createElement ( 'canvas' ) ;
129- const context = canvas . getContext ( '2d' ) ;
131+ 'measureWidth' , text => {
132+ return measureWidth ( text ) ;
133+ }
134+ ) ;
130135
131- return context . measureText ( node . textContent ) . width ;
136+ // Approximate .offsetWidth
137+ sinon . stub ( Truncate . prototype ,
138+ 'ellipsisWidth' , node => {
139+ return measureWidth ( node . textContent ) ;
132140 }
133141 ) ;
134142 } ) ;
135143
136144 after ( ( ) => {
137145 global . window . HTMLDivElement . prototype . getBoundingClientRect . restore ( ) ;
138146
147+ Truncate . prototype . measureWidth . restore ( ) ;
139148 Truncate . prototype . ellipsisWidth . restore ( ) ;
140149 } ) ;
141150
@@ -225,7 +234,7 @@ describe('<Truncate />', () => {
225234 ) ;
226235
227236 expect ( component , 'to display text' , `
228- Thereisasuperl …
237+ Thereisasuperlo …
229238 ` ) ;
230239 } ) ;
231240
@@ -240,7 +249,7 @@ describe('<Truncate />', () => {
240249
241250 expect ( component , 'to display text' , `
242251 I'm curious what
243- the … read more
252+ the n … read more
244253 ` ) ;
245254 } ) ;
246255
@@ -250,30 +259,42 @@ describe('<Truncate />', () => {
250259 const component = render (
251260 < div >
252261 < Truncate lines = { 1 } >
253- Some old content
262+ Some old content here
254263 </ Truncate >
255264 </ div > ,
256265 container
257266 ) ;
258267
259268 expect ( component , 'to display text' , `
260- Some old cont …
269+ Some old conten …
261270 ` ) ;
262271
263272 render (
264273 < div >
265274 < Truncate lines = { 1 } >
266- Some new content
275+ Some new content here
267276 </ Truncate >
268277 </ div > ,
269278 container
270279 ) ;
271280
272281 expect ( component , 'to display text' , `
273- Some new con …
282+ Some new conten …
274283 ` ) ;
275284 } ) ;
276285
286+ it ( 'should render without an error when the last line is exactly as wide as the container' , ( ) => {
287+ const render = ( ) => renderIntoDocument (
288+ < div >
289+ < Truncate lines = { 2 } >
290+ { new Array ( numCharacters ) . fill ( 'a' ) . join ( ) }
291+ </ Truncate >
292+ </ div >
293+ ) ;
294+
295+ expect ( render , 'not to throw' ) ;
296+ } ) ;
297+
277298 describe ( 'onTruncate' , ( ) => {
278299 describe ( 'with Truncate.prototype.onTruncate mocked out' , ( ) => {
279300 before ( ( ) => {
@@ -298,8 +319,9 @@ describe('<Truncate />', () => {
298319
299320 renderIntoBox (
300321 < Truncate onTruncate = { handleTruncate } >
301- This is some text
302- that got truncated
322+ Some text over
323+ here that got
324+ truncated
303325 </ Truncate >
304326 ) ;
305327
@@ -312,8 +334,8 @@ describe('<Truncate />', () => {
312334
313335 renderIntoBox (
314336 < Truncate lines = { false } onTruncate = { handleTruncate } >
315- This is some text
316- that did not get
337+ Some text over
338+ here that is not
317339 truncated
318340 </ Truncate >
319341 ) ;
@@ -326,8 +348,8 @@ describe('<Truncate />', () => {
326348
327349 renderIntoBox (
328350 < Truncate lines = { 3 } onTruncate = { handleTruncate } >
329- This is some text
330- that did not get
351+ Some text over
352+ here that is not
331353 truncated
332354 </ Truncate >
333355 ) ;
@@ -357,43 +379,6 @@ describe('<Truncate />', () => {
357379 } ) ;
358380 } ) ;
359381
360- it ( 'should render without an error when the last line is exactly as wide as the container' , ( ) => {
361- const text = 'Foo bar - end of text' ;
362-
363- const canvas = document . createElement ( 'canvas' ) ;
364- const context = canvas . getContext ( '2d' ) ;
365- const measureText = context . measureText . bind ( context ) ;
366-
367- sinon . stub ( global . window . HTMLDivElement . prototype ,
368- 'getBoundingClientRect' , ( ) => ( {
369- width : measureText ( text ) . width
370- } )
371- ) ;
372-
373- // Approximate .offsetWidth with context.measureText
374- sinon . stub ( Truncate . prototype ,
375- 'ellipsisWidth' , node => {
376- return measureText ( node . textContent ) . width ;
377- }
378- ) ;
379-
380- try {
381- const render = ( ) => renderIntoDocument (
382- < div >
383- < Truncate lines = { 2 } >
384- Foo bar - end of text
385- </ Truncate >
386- </ div >
387- ) ;
388-
389- expect ( render , 'not to throw' ) ;
390- } finally {
391- global . window . HTMLDivElement . prototype . getBoundingClientRect . restore ( ) ;
392-
393- Truncate . prototype . ellipsisWidth . restore ( ) ;
394- }
395- } ) ;
396-
397382 it ( 'should recalculate when resizing the window' , ( ) => {
398383 const calcTargetWidth = sinon . spy ( Truncate . prototype , 'calcTargetWidth' ) ;
399384
0 commit comments