import { expect, fixture, html } from '@open-wc/testing' import '../src/renderers/ox-grist-renderer-sparkline.js' import { OxGristRendererSparkline, sparklineRuns } from '../src/renderers/ox-grist-renderer-sparkline.js' /* A sparkline in a cell: a gap where a month has no value, the months to come dashed, the last value a dot. */ describe('sparkline renderer', () => { it('★ splits at a missing month and puts the highest value at the top', () => { const runs = sparklineRuns([1, 3, null, 2]) expect(runs.actual.length).to.equal(2) expect(runs.actual[0].length).to.equal(2) expect(runs.actual[0][1][1]).to.equal(2, 'the highest value is at the top') expect(runs.actual[1].length).to.equal(1) expect(runs.last![0]).to.equal(98, 'the last value is at the right end') }) it('draws the months to come from the last value, apart', () => { const runs = sparklineRuns({ values: [10, 20], expected: [20, 30] }) expect(runs.expected.length).to.equal(1) expect(runs.expected[0].length).to.equal(3, 'joined to the last value') expect(sparklineRuns({ values: [10, null], expected: [30] }).expected[0].length).to.equal(1, 'not joined across a gap') }) it('a flat line is in the middle; nothing draws nothing', () => { expect(sparklineRuns([5, 5, 5]).actual[0].every(([, y]) => y === 12)).to.be.true expect(sparklineRuns([]).actual).to.deep.equal([]) expect(sparklineRuns(null).last).to.equal(null) }) it('renders its lines and the tooltip the column gives', async () => { const element = new OxGristRendererSparkline() element.value = { values: [1, 2, null, 4], expected: [5] } element.column = { type: 'sparkline', name: 'trend', record: { options: { title: () => '1, 2, -, 4 → 5' } } } as any const shown = await fixture(html`${element}`) const svgElement = shown.shadowRoot!.querySelector('svg')! expect(svgElement.querySelectorAll('path:not(.expected)').length).to.equal(1) expect(svgElement.querySelectorAll('path.expected').length).to.equal(1) expect(svgElement.querySelector('title')!.textContent).to.equal('1, 2, -, 4 → 5') }) })