|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * DecisionProgressIndicator — one pin per `decision_progress` shape |
| 6 | + * (objectui#12033, the A3 primitive of objectui#2763). |
| 7 | + * |
| 8 | + * The approval-service contract declares three behaviors for the tally: |
| 9 | + * `unanimous` (approvals of every approver), `quorum` (approvals of the M |
| 10 | + * threshold) and `per_group` (satisfied groups, plus per-group detail). Each |
| 11 | + * gets its own pin below, plus the continuous-bar branch a long tally takes. |
| 12 | + * |
| 13 | + * What is asserted is the indicator's structure — tick count, filled ticks, |
| 14 | + * the progressbar's value range, the group ticks — and WHICH pack row each |
| 15 | + * label is read from. The expected strings are produced by the same i18next |
| 16 | + * instance the render uses, asked for the key, so a pin names the row and its |
| 17 | + * interpolation arguments rather than restating English prose. |
| 18 | + */ |
| 19 | + |
| 20 | +import * as React from 'react'; |
| 21 | +import { describe, it, expect, afterEach } from 'vitest'; |
| 22 | +import { render, screen, cleanup } from '@testing-library/react'; |
| 23 | +import { createI18n, I18nProvider } from '@object-ui/i18n'; |
| 24 | +import type { ApprovalDecisionProgress } from '../../hooks/useRecordApprovals'; |
| 25 | +import { DecisionProgressIndicator } from './DecisionProgressIndicator'; |
| 26 | + |
| 27 | +const i18n = createI18n({ defaultLanguage: 'en', detectBrowserLanguage: false }); |
| 28 | + |
| 29 | +/** The pack row a label must come from, rendered by the instance under test. */ |
| 30 | +function row(key: string, args?: Record<string, unknown>): string { |
| 31 | + return String(i18n.t(`approvalsInbox.${key}`, args)); |
| 32 | +} |
| 33 | + |
| 34 | +async function renderIndicator(progress: ApprovalDecisionProgress, eligibleApprovers?: number) { |
| 35 | + render( |
| 36 | + <I18nProvider instance={i18n}> |
| 37 | + <DecisionProgressIndicator progress={progress} eligibleApprovers={eligibleApprovers} /> |
| 38 | + </I18nProvider>, |
| 39 | + ); |
| 40 | + return screen.findByRole('progressbar'); |
| 41 | +} |
| 42 | + |
| 43 | +/** The bar's own children: one per tick, or the single continuous track. */ |
| 44 | +function ticks(bar: HTMLElement): Element[] { |
| 45 | + return Array.from(bar.children); |
| 46 | +} |
| 47 | + |
| 48 | +function filled(bar: HTMLElement): Element[] { |
| 49 | + return ticks(bar).filter((el) => (el.getAttribute('class') ?? '').split(/\s+/).includes('bg-emerald-500')); |
| 50 | +} |
| 51 | + |
| 52 | +afterEach(() => { |
| 53 | + cleanup(); |
| 54 | +}); |
| 55 | + |
| 56 | +describe('DecisionProgressIndicator — the decision_progress shapes (objectui#12033)', () => { |
| 57 | + it('unanimous: one tick per approver, filled up to the approvals recorded', async () => { |
| 58 | + const bar = await renderIndicator({ behavior: 'unanimous', got: 1, need: 3 }, 2); |
| 59 | + |
| 60 | + expect(screen.getByText(row('progressApprovals', { got: 1, need: 3 }))).toBeTruthy(); |
| 61 | + expect(screen.getByText(row('progressEligible', { count: 2 }))).toBeTruthy(); |
| 62 | + expect(bar.getAttribute('aria-label')).toBe(row('progressBar')); |
| 63 | + expect(bar.getAttribute('aria-valuemin')).toBe('0'); |
| 64 | + expect(bar.getAttribute('aria-valuemax')).toBe('3'); |
| 65 | + expect(bar.getAttribute('aria-valuenow')).toBe('1'); |
| 66 | + expect(ticks(bar)).toHaveLength(3); |
| 67 | + expect(filled(bar)).toHaveLength(1); |
| 68 | + // Group ticks belong to `per_group` alone. |
| 69 | + expect(bar.nextElementSibling).toBeNull(); |
| 70 | + }); |
| 71 | + |
| 72 | + it('quorum (M-of-N): the ticks count the M threshold, the eligible N rides beside it', async () => { |
| 73 | + const bar = await renderIndicator({ behavior: 'quorum', got: 1, need: 2 }, 5); |
| 74 | + |
| 75 | + expect(screen.getByText(row('progressApprovals', { got: 1, need: 2 }))).toBeTruthy(); |
| 76 | + expect(screen.getByText(row('progressEligible', { count: 5 }))).toBeTruthy(); |
| 77 | + expect(bar.getAttribute('aria-valuemax')).toBe('2'); |
| 78 | + expect(bar.getAttribute('aria-valuenow')).toBe('1'); |
| 79 | + // M ticks, not N: the bar is the threshold the engine finalizes on. |
| 80 | + expect(ticks(bar)).toHaveLength(2); |
| 81 | + expect(filled(bar)).toHaveLength(1); |
| 82 | + expect(bar.nextElementSibling).toBeNull(); |
| 83 | + }); |
| 84 | + |
| 85 | + it('quorum with no eligible count shows the tally alone', async () => { |
| 86 | + await renderIndicator({ behavior: 'quorum', got: 0, need: 2 }); |
| 87 | + |
| 88 | + expect(screen.getByText(row('progressApprovals', { got: 0, need: 2 }))).toBeTruthy(); |
| 89 | + expect(screen.queryByText(row('progressEligible', { count: 0 }))).toBeNull(); |
| 90 | + }); |
| 91 | + |
| 92 | + it('per_group (countersign): a tick per group, the satisfied ones filled, a badge per group, no eligible count', async () => { |
| 93 | + const bar = await renderIndicator( |
| 94 | + { |
| 95 | + behavior: 'per_group', |
| 96 | + got: 1, |
| 97 | + need: 2, |
| 98 | + groups: [ |
| 99 | + { group: 'quality', got: 1, need: 1, satisfied: true }, |
| 100 | + { group: 'production', got: 0, need: 2, satisfied: false }, |
| 101 | + ], |
| 102 | + }, |
| 103 | + 3, |
| 104 | + ); |
| 105 | + |
| 106 | + expect(screen.getByText(row('progressGroups', { got: 1, need: 2 }))).toBeTruthy(); |
| 107 | + // The approver count describes approvals, not groups — never shown here. |
| 108 | + expect(screen.queryByText(row('progressEligible', { count: 3 }))).toBeNull(); |
| 109 | + expect(ticks(bar)).toHaveLength(2); |
| 110 | + expect(filled(bar)).toHaveLength(1); |
| 111 | + |
| 112 | + const quality = screen.getByText('quality 1/1'); |
| 113 | + const production = screen.getByText('production 0/2'); |
| 114 | + expect(quality.getAttribute('title')).toBe('1/1'); |
| 115 | + expect(production.getAttribute('title')).toBe('0/2'); |
| 116 | + // Satisfied groups carry the check, open ones the hollow circle. |
| 117 | + expect(quality.querySelector('svg')?.getAttribute('class')).toMatch(/\blucide-check\b/); |
| 118 | + expect(production.querySelector('svg')?.getAttribute('class')).toMatch(/\blucide-circle\b/); |
| 119 | + // Both badges sit in the one group row that follows the bar. |
| 120 | + expect(quality.parentElement).toBe(bar.nextElementSibling); |
| 121 | + expect(production.parentElement).toBe(bar.nextElementSibling); |
| 122 | + }); |
| 123 | + |
| 124 | + it('a tally past a dozen decisions is one continuous fill, not hairline ticks', async () => { |
| 125 | + const bar = await renderIndicator({ behavior: 'unanimous', got: 5, need: 20 }); |
| 126 | + |
| 127 | + expect(bar.getAttribute('aria-valuemax')).toBe('20'); |
| 128 | + expect(ticks(bar)).toHaveLength(1); |
| 129 | + const fill = ticks(bar)[0].firstElementChild as HTMLElement; |
| 130 | + expect(fill.style.width).toBe('25%'); |
| 131 | + }); |
| 132 | + |
| 133 | + it('caps the progressbar value at the requirement', async () => { |
| 134 | + const bar = await renderIndicator({ behavior: 'quorum', got: 3, need: 2 }); |
| 135 | + |
| 136 | + expect(bar.getAttribute('aria-valuenow')).toBe('2'); |
| 137 | + expect(filled(bar)).toHaveLength(2); |
| 138 | + }); |
| 139 | +}); |
0 commit comments