]>
Commit | Line | Data |
---|---|---|
202b54af DI |
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', () => { | |
0ea2e9cf | 10 | let dom: jsdom.JSDOM; |
202b54af DI |
11 | let window: Window; |
12 | let document: Document; | |
202b54af DI |
13 | let container: HTMLElement; |
14 | let charMeasure: ICharMeasure; | |
15 | ||
0ea2e9cf PK |
16 | beforeEach(() => { |
17 | dom = new jsdom.JSDOM(''); | |
18 | window = dom.window; | |
19 | document = window.document; | |
20 | container = document.createElement('div'); | |
21 | document.body.appendChild(container); | |
22 | charMeasure = new CharMeasure(document, container); | |
202b54af DI |
23 | }); |
24 | ||
25 | describe('measure', () => { | |
26 | it('should set _measureElement on first call', () => { | |
27 | charMeasure.measure(); | |
28 | assert.isDefined((<any>charMeasure)._measureElement, 'CharMeasure.measure should have created _measureElement'); | |
29 | }); | |
30 | ||
31 | it('should be performed async on first call', done => { | |
32 | assert.equal(charMeasure.width, null); | |
33 | charMeasure.measure(); | |
34 | // Mock getBoundingClientRect since jsdom doesn't have a layout engine | |
35 | (<any>charMeasure)._measureElement.getBoundingClientRect = () => { | |
36 | return { width: 1, height: 1 }; | |
37 | }; | |
38 | assert.equal(charMeasure.width, null); | |
39 | setTimeout(() => { | |
40 | assert.equal(charMeasure.width, 1); | |
41 | done(); | |
42 | }, 0); | |
43 | }); | |
44 | ||
45 | it('should be performed sync on successive calls', done => { | |
46 | charMeasure.measure(); | |
47 | // Mock getBoundingClientRect since jsdom doesn't have a layout engine | |
48 | (<any>charMeasure)._measureElement.getBoundingClientRect = () => { | |
49 | return { width: 1, height: 1 }; | |
50 | }; | |
51 | setTimeout(() => { | |
52 | const firstWidth = charMeasure.width; | |
53 | // Mock getBoundingClientRect since jsdom doesn't have a layout engine | |
54 | (<any>charMeasure)._measureElement.getBoundingClientRect = () => { | |
55 | return { width: 2, height: 2 }; | |
56 | }; | |
57 | charMeasure.measure(); | |
58 | assert.equal(charMeasure.width, firstWidth * 2); | |
59 | done(); | |
60 | }, 0); | |
61 | }); | |
62 | ||
63 | it('should NOT do a measure when the parent is hidden', done => { | |
64 | charMeasure.measure(); | |
65 | setTimeout(() => { | |
66 | const firstWidth = charMeasure.width; | |
67 | container.style.display = 'none'; | |
68 | container.style.fontSize = '2em'; | |
69 | charMeasure.measure(); | |
70 | assert.equal(charMeasure.width, firstWidth); | |
71 | done(); | |
72 | }, 0); | |
73 | }); | |
74 | }); | |
75 | }); |