Skip to content

Commit 7a7149c

Browse files
pablosicherteric-adstage
authored andcommitted
Approximate text width by multiplying amount of characters
...instead of relying on Canvas.measureText for that. This means that the calculation is no longer dependent on how the OS renders fonts and therefore makes the test suite simpler, and portable across OS environments.
1 parent 92c90eb commit 7a7149c

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

test/Truncate.js

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import unexpectedDOM from 'unexpected-dom';
55
import sinon from 'sinon';
66
import { jsdom } from 'jsdom';
77
import requestAnimationFrame, { cancel as cancelAnimationFrame } from 'raf';
8-
import Canvas from 'canvas';
98
import React, { Component } from 'react';
109
import { render, unmountComponentAtNode } from 'react-dom';
1110
import { 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+
3941
describe('<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 14 characters wide
115+
const numCharacters = 14;
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,16 +124,13 @@ 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
130+
// Approximate .offsetWidth
126131
sinon.stub(Truncate.prototype,
127132
'ellipsisWidth', node => {
128-
const canvas = document.createElement('canvas');
129-
const context = canvas.getContext('2d');
130-
131-
return context.measureText(node.textContent).width;
133+
return measureWidth(node.textContent);
132134
}
133135
);
134136
});
@@ -151,7 +153,7 @@ describe('<Truncate />', () => {
151153

152154
expect(component, 'to display text', `
153155
This text should
154-
stop after here…
156+
stop after here
155157
`);
156158
});
157159

@@ -225,7 +227,7 @@ describe('<Truncate />', () => {
225227
);
226228

227229
expect(component, 'to display text', `
228-
Thereisasuperl
230+
Thereisasuperlo
229231
`);
230232
});
231233

@@ -240,7 +242,7 @@ describe('<Truncate />', () => {
240242

241243
expect(component, 'to display text', `
242244
I'm curious what
243-
the … read more
245+
the… read more
244246
`);
245247
});
246248

@@ -257,7 +259,7 @@ describe('<Truncate />', () => {
257259
);
258260

259261
expect(component, 'to display text', `
260-
Some old cont
262+
Some old conte
261263
`);
262264

263265
render(
@@ -270,7 +272,7 @@ describe('<Truncate />', () => {
270272
);
271273

272274
expect(component, 'to display text', `
273-
Some new con
275+
Some new cont
274276
`);
275277
});
276278

@@ -360,20 +362,16 @@ describe('<Truncate />', () => {
360362
it('should render without an error when the last line is exactly as wide as the container', () => {
361363
const text = 'Foo bar - end of text';
362364

363-
const canvas = document.createElement('canvas');
364-
const context = canvas.getContext('2d');
365-
const measureText = context.measureText.bind(context);
366-
367365
sinon.stub(global.window.HTMLDivElement.prototype,
368366
'getBoundingClientRect', () => ({
369-
width: measureText(text).width
367+
width: measureWidth(text)
370368
})
371369
);
372370

373-
// Approximate .offsetWidth with context.measureText
371+
// Approximate .offsetWidth
374372
sinon.stub(Truncate.prototype,
375373
'ellipsisWidth', node => {
376-
return measureText(node.textContent).width;
374+
return measureWidth(text);
377375
}
378376
);
379377

0 commit comments

Comments
 (0)