Skip to content
This repository was archived by the owner on Apr 11, 2018. It is now read-only.

Commit 202b54a

Browse files
committed
Convert CharMeasure phantom test to jsdom
1 parent 26ebc3d commit 202b54a

File tree

4 files changed

+82
-68
lines changed

4 files changed

+82
-68
lines changed

src/utils/CharMeasure.phantom.ts

Lines changed: 0 additions & 65 deletions
This file was deleted.

src/utils/CharMeasure.test.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* @license MIT
3+
*/
4+
import jsdom = require('jsdom');
5+
import { assert } from 'chai';
6+
import { ICharMeasure, ITerminal } from '../Interfaces';
7+
import { CharMeasure } from './CharMeasure';
8+
9+
describe('CharMeasure', () => {
10+
let window: Window;
11+
let document: Document;
12+
13+
let container: HTMLElement;
14+
let charMeasure: ICharMeasure;
15+
16+
beforeEach(done => {
17+
jsdom.env('', (err, w) => {
18+
window = w;
19+
document = window.document;
20+
container = document.createElement('div');
21+
document.body.appendChild(container);
22+
charMeasure = new CharMeasure(document, container);
23+
done();
24+
});
25+
});
26+
27+
describe('measure', () => {
28+
it('should set _measureElement on first call', () => {
29+
charMeasure.measure();
30+
assert.isDefined((<any>charMeasure)._measureElement, 'CharMeasure.measure should have created _measureElement');
31+
});
32+
33+
it('should be performed async on first call', done => {
34+
assert.equal(charMeasure.width, null);
35+
charMeasure.measure();
36+
// Mock getBoundingClientRect since jsdom doesn't have a layout engine
37+
(<any>charMeasure)._measureElement.getBoundingClientRect = () => {
38+
return { width: 1, height: 1 };
39+
};
40+
assert.equal(charMeasure.width, null);
41+
setTimeout(() => {
42+
assert.equal(charMeasure.width, 1);
43+
done();
44+
}, 0);
45+
});
46+
47+
it('should be performed sync on successive calls', done => {
48+
charMeasure.measure();
49+
// Mock getBoundingClientRect since jsdom doesn't have a layout engine
50+
(<any>charMeasure)._measureElement.getBoundingClientRect = () => {
51+
return { width: 1, height: 1 };
52+
};
53+
setTimeout(() => {
54+
const firstWidth = charMeasure.width;
55+
// Mock getBoundingClientRect since jsdom doesn't have a layout engine
56+
(<any>charMeasure)._measureElement.getBoundingClientRect = () => {
57+
return { width: 2, height: 2 };
58+
};
59+
charMeasure.measure();
60+
assert.equal(charMeasure.width, firstWidth * 2);
61+
done();
62+
}, 0);
63+
});
64+
65+
it('should NOT do a measure when the parent is hidden', done => {
66+
charMeasure.measure();
67+
setTimeout(() => {
68+
const firstWidth = charMeasure.width;
69+
container.style.display = 'none';
70+
container.style.fontSize = '2em';
71+
charMeasure.measure();
72+
assert.equal(charMeasure.width, firstWidth);
73+
done();
74+
}, 0);
75+
});
76+
});
77+
});

src/utils/CharMeasure.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ import { EventEmitter } from '../EventEmitter.js';
99
* Utility class that measures the size of a character.
1010
*/
1111
export class CharMeasure extends EventEmitter {
12+
private _document: Document;
1213
private _parentElement: HTMLElement;
1314
private _measureElement: HTMLElement;
1415
private _width: number;
1516
private _height: number;
1617

17-
constructor(parentElement: HTMLElement) {
18+
constructor(document: Document, parentElement: HTMLElement) {
1819
super();
20+
this._document = document;
1921
this._parentElement = parentElement;
2022
}
2123

@@ -29,7 +31,7 @@ export class CharMeasure extends EventEmitter {
2931

3032
public measure(): void {
3133
if (!this._measureElement) {
32-
this._measureElement = document.createElement('span');
34+
this._measureElement = this._document.createElement('span');
3335
this._measureElement.style.position = 'absolute';
3436
this._measureElement.style.top = '0';
3537
this._measureElement.style.left = '-9999em';

src/xterm.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ Terminal.prototype.open = function(parent) {
647647
}
648648
this.parent.appendChild(this.element);
649649

650-
this.charMeasure = new CharMeasure(this.helperContainer);
650+
this.charMeasure = new CharMeasure(document, this.helperContainer);
651651
this.charMeasure.on('charsizechanged', function () {
652652
self.updateCharSizeCSS();
653653
});

0 commit comments

Comments
 (0)