|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { h, renderToString } from '@textui/core'; |
| 3 | +import { Panel } from '../src/layout/index.js'; |
| 4 | +import { CATALOG } from '../src/index.js'; |
| 5 | + |
| 6 | +/* |
| 7 | + * A panel can carry a short label beside its heading - a count, a state, the |
| 8 | + * shortcut that opens it - and it goes on the top rule, hard against the right. |
| 9 | + * |
| 10 | + * `meta` was documented as exactly that and did something else: on a bordered |
| 11 | + * panel it went into the *bottom* rule, and only on a borderless one did it sit |
| 12 | + * beside the title. One prop, two places, depending on another prop. |
| 13 | + */ |
| 14 | +describe('a panel with a label beside its title', () => { |
| 15 | + const draw = (props: Record<string, unknown>, width: number): string[] => |
| 16 | + renderToString(h(Panel, props, h('text', { content: 'body' })), { width, components: CATALOG }) |
| 17 | + .split('\n').map((line) => line.trimEnd()).filter((line) => line !== ''); |
| 18 | + |
| 19 | + // Two widths, because the whole question is what happens when the two |
| 20 | + // labels want more room than the rule has. |
| 21 | + for (const width of [40, 24]) { |
| 22 | + it(`puts it on the top rule at ${width} columns`, () => { |
| 23 | + const [top] = draw({ title: 'Controls', rightTitle: '3 items' }, width); |
| 24 | + expect(top).toContain('Controls'); |
| 25 | + expect(top).toContain('3 items'); |
| 26 | + // Hard against the right: the last cell is the corner, and the label is |
| 27 | + // the thing before it. |
| 28 | + expect(top?.trimEnd().endsWith('3 items ┐')).toBe(true); |
| 29 | + }); |
| 30 | + } |
| 31 | + |
| 32 | + it('truncates the title rather than the label', () => { |
| 33 | + // The right label takes its width first. It is the short one - a count or |
| 34 | + // a state - and a half-written count says nothing, where a truncated |
| 35 | + // heading still reads. |
| 36 | + const [top] = draw({ title: 'A very long panel heading indeed', rightTitle: '12' }, 24); |
| 37 | + expect(top).toContain('12 ┐'); |
| 38 | + expect(top).toContain('…'); |
| 39 | + expect(top).not.toContain('heading indeed'); |
| 40 | + }); |
| 41 | + |
| 42 | + it('never lets the two labels land on the same cells', () => { |
| 43 | + // The failure this is guarding is one drawn over the other, which reads as |
| 44 | + // a corrupted frame rather than as a layout mistake. |
| 45 | + for (const width of [40, 24, 16]) { |
| 46 | + const [top = ''] = draw({ title: 'Wide enough heading', rightTitle: 'state' }, width); |
| 47 | + expect(top.length).toBe(width); |
| 48 | + // One '…' at most: two would mean both were truncated into each other. |
| 49 | + expect((top.match(/…/g) ?? []).length).toBeLessThanOrEqual(1); |
| 50 | + } |
| 51 | + }); |
| 52 | + |
| 53 | + it('stands alone, with no title beside it', () => { |
| 54 | + const [top = ''] = draw({ rightTitle: 'ctrl+p' }, 30); |
| 55 | + expect(top).toContain('ctrl+p ┐'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('is a different place from meta, which is the bottom rule', () => { |
| 59 | + const rows = draw({ title: 'Both', rightTitle: 'live', meta: 'f1 help' }, 34); |
| 60 | + expect(rows[0]).toContain('live'); |
| 61 | + expect(rows[0]).not.toContain('f1 help'); |
| 62 | + expect(rows[rows.length - 1]).toContain('f1 help'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('shares the heading row when there is no border to write on', () => { |
| 66 | + const rows = draw({ title: 'Airy', rightTitle: '7', border: 'none' }, 30); |
| 67 | + expect(rows[0]).toContain('Airy'); |
| 68 | + expect(rows[0]?.trimEnd().endsWith('7')).toBe(true); |
| 69 | + }); |
| 70 | +}); |
0 commit comments