import { Container } from 'pixi.js'; import { describe, expect, it } from 'vitest'; import { TextPool } from '../../src/ui/textPool'; describe('a pool of texts', () => { it('makes a text for each wanted string, in their order', () => { const layer = new Container(); const shown = new TextPool(layer).show(['0', '1', '2']); expect(shown.map((t) => t.text)).toEqual(['0', '1', '2']); expect(layer.children).toHaveLength(3); }); it('keeps a text on the string it shows, so a pan sets only the new one', () => { const pool = new TextPool(new Container()); const [, one, two] = pool.show(['0', '1', '2']); const before = pool.changes; const shown = pool.show(['1', '2', '3']); expect(shown[0]).toBe(one); expect(shown[1]).toBe(two); expect(shown.map((t) => t.text)).toEqual(['1', '2', '3']); expect(pool.changes - before).toBe(1); }); it('hides the texts not wanted and makes no more than it needs', () => { const layer = new Container(); const pool = new TextPool(layer); pool.show(['a', 'b', 'c']); const [kept] = pool.show(['b']); expect(kept!.text).toBe('b'); expect(layer.children.filter((t) => t.visible)).toEqual([kept]); pool.show(['x', 'y', 'z']); expect(layer.children).toHaveLength(3); expect(layer.children.every((t) => t.visible)).toBe(true); }); it('gives a string wanted twice two texts', () => { const pool = new TextPool(new Container()); pool.show(['…', '…']); const shown = pool.show(['…', '…']); expect(new Set(shown).size).toBe(2); expect(pool.changes).toBe(2); }); });