|
| 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 | +}); |
0 commit comments