]> git.proxmox.com Git - mirror_xterm.js.git/blame - src/utils/DomElementObjectPool.test.ts
Merge pull request #669 from Tyriar/pull_getCoords_into_module
[mirror_xterm.js.git] / src / utils / DomElementObjectPool.test.ts
CommitLineData
33585038
DI
1import { assert } from 'chai';
2import { DomElementObjectPool } from './DomElementObjectPool';
3
4class MockDocument {
5 private _attr: {[key: string]: string} = {};
6 constructor() {}
7 public getAttribute(key: string): string { return this._attr[key]; };
8 public setAttribute(key: string, value: string): void { this._attr[key] = value; }
9}
10
11describe('DomElementObjectPool', () => {
12 let pool: DomElementObjectPool;
13
14 beforeEach(() => {
15 pool = new DomElementObjectPool('span');
16 (<any>global).document = {
17 createElement: () => new MockDocument()
18 };
19 });
20
21 it('should acquire distinct elements', () => {
22 const element1 = pool.acquire();
23 const element2 = pool.acquire();
24 assert.notEqual(element1, element2);
25 });
26
27 it('should acquire released elements', () => {
28 const element = pool.acquire();
29 pool.release(element);
30 assert.equal(pool.acquire(), element);
31 });
32
33 it('should handle a series of acquisitions and releases', () => {
34 const element1 = pool.acquire();
35 const element2 = pool.acquire();
36 pool.release(element1);
37 assert.equal(pool.acquire(), element1);
38 pool.release(element1);
39 pool.release(element2);
40 assert.equal(pool.acquire(), element2);
41 assert.equal(pool.acquire(), element1);
42 });
43
44 it('should throw when releasing an element that was not acquired', () => {
45 assert.throws(() => pool.release(document.createElement('span')));
46 });
47});