|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#27347 — an inline `object-metric` tile colours its trend badge by |
| 5 | + * its declared `polarity`. |
| 6 | + * |
| 7 | + * The dataset-bound tile has coloured its delta by the measure's `polarity` |
| 8 | + * since objectstack#26602, but a page-level `object-metric` node aggregates a |
| 9 | + * field, not a dataset measure, so it coloured by direction alone: an average |
| 10 | + * turnaround that rose 20% was drawn green. The node now declares `polarity` |
| 11 | + * itself, in the measure's vocabulary, and `MetricWidget` colours the badge it |
| 12 | + * draws (`derivedTrend ?? trend`) by the one shared rule, `deltaToneClass`: |
| 13 | + * - `lower`: a rise is red, a fall is green; |
| 14 | + * - `neutral`: no good/bad colour, the muted text colour; |
| 15 | + * - `higher`: a rise is green, a fall is red; |
| 16 | + * - undeclared: exactly today's class string, with no guess from the field's |
| 17 | + * name — the fixture aggregates a field literally named `turnaround_days`. |
| 18 | + * |
| 19 | + * The arrow is the number's direction under every polarity. The `compareTo` |
| 20 | + * rows mount the REGISTERED block through `SchemaRenderer` (its |
| 21 | + * `ElementDataSourceGate` shell forwards every authored key but `objectName` |
| 22 | + * and `filter` untouched), so the pin covers the path an author reaches. The |
| 23 | + * static `trend` badge is the same badge, so one top-level key covers it too. |
| 24 | + */ |
| 25 | + |
| 26 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 27 | +import { render, screen, cleanup } from '@testing-library/react'; |
| 28 | +import { SchemaRenderer, SchemaRendererProvider } from '@object-ui/react'; |
| 29 | +// Registers `object-metric` at MODULE scope, never inside a hook |
| 30 | +// (object-ui/no-dynamic-import-in-test-hook). |
| 31 | +import '../index'; |
| 32 | + |
| 33 | +afterEach(cleanup); |
| 34 | + |
| 35 | +const GOOD = /text-emerald-600/; |
| 36 | +const BAD = /text-rose-600/; |
| 37 | +const MUTED = /text-muted-foreground/; |
| 38 | + |
| 39 | +type Polarity = 'higher' | 'lower' | 'neutral' | undefined; |
| 40 | + |
| 41 | +const quarterStart = (d: Date) => new Date(d.getFullYear(), Math.floor(d.getMonth() / 3) * 3, 1); |
| 42 | +const iso = (d: Date) => |
| 43 | + `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`; |
| 44 | +const CURRENT_FROM = iso(quarterStart(new Date())); |
| 45 | + |
| 46 | +/** The current window answers `current`; every shifted window answers `previous`. */ |
| 47 | +const comparingAdapter = (current: number, previous: number) => ({ |
| 48 | + aggregate: vi.fn(async (_object: string, q: { filter?: { submitted_on?: { $gte?: string } } }) => [ |
| 49 | + { turnaround_days_avg: String(q?.filter?.submitted_on?.$gte) === CURRENT_FROM ? current : previous }, |
| 50 | + ]), |
| 51 | +}); |
| 52 | + |
| 53 | +const mount = (schema: Record<string, unknown>, adapter?: unknown) => |
| 54 | + render( |
| 55 | + <SchemaRendererProvider dataSource={adapter as never}> |
| 56 | + <SchemaRenderer |
| 57 | + schema={{ type: 'object-metric', objectName: 'approval', label: 'Avg. turnaround', ...schema } as never} |
| 58 | + /> |
| 59 | + </SchemaRendererProvider>, |
| 60 | + ); |
| 61 | + |
| 62 | +/** The `compareTo`-derived badge of an `object-metric` node declaring `polarity`. */ |
| 63 | +async function derivedBadge(current: number, previous: number, polarity: Polarity) { |
| 64 | + mount( |
| 65 | + { |
| 66 | + aggregate: { field: 'turnaround_days', function: 'avg' }, |
| 67 | + filter: { submitted_on: { $gte: '{current_quarter_start}', $lte: '{current_quarter_end}' } }, |
| 68 | + compareTo: { kind: 'previousPeriod' }, |
| 69 | + ...(polarity ? { polarity } : {}), |
| 70 | + }, |
| 71 | + comparingAdapter(current, previous), |
| 72 | + ); |
| 73 | + return screen.findByText('20%'); |
| 74 | +} |
| 75 | + |
| 76 | +const arrow = (badge: HTMLElement) => badge.querySelector('svg')?.getAttribute('class') ?? ''; |
| 77 | + |
| 78 | +describe('object-metric — trend badge colour by declared `polarity` (objectstack#27347)', () => { |
| 79 | + it('lower: a rise is drawn bad, with the up arrow kept', async () => { |
| 80 | + const badge = await derivedBadge(120, 100, 'lower'); |
| 81 | + expect(badge.className).toMatch(BAD); |
| 82 | + expect(badge.className).not.toMatch(GOOD); |
| 83 | + expect(arrow(badge)).toMatch(/arrow-up/); |
| 84 | + }); |
| 85 | + |
| 86 | + it('lower: a fall is drawn good, with the down arrow kept', async () => { |
| 87 | + const badge = await derivedBadge(80, 100, 'lower'); |
| 88 | + expect(badge.className).toMatch(GOOD); |
| 89 | + expect(badge.className).not.toMatch(BAD); |
| 90 | + expect(arrow(badge)).toMatch(/arrow-down/); |
| 91 | + }); |
| 92 | + |
| 93 | + it('higher: a rise is drawn good, as before', async () => { |
| 94 | + const badge = await derivedBadge(120, 100, 'higher'); |
| 95 | + expect(badge.className).toMatch(GOOD); |
| 96 | + expect(badge.className).not.toMatch(BAD); |
| 97 | + }); |
| 98 | + |
| 99 | + it('neutral: neither a rise nor a fall takes a good/bad colour', async () => { |
| 100 | + for (const [current, direction] of [[120, /arrow-up/], [80, /arrow-down/]] as const) { |
| 101 | + const badge = await derivedBadge(current, 100, 'neutral'); |
| 102 | + expect(badge.className).not.toMatch(GOOD); |
| 103 | + expect(badge.className).not.toMatch(BAD); |
| 104 | + expect(badge.className).toMatch(MUTED); |
| 105 | + expect(arrow(badge)).toMatch(direction); |
| 106 | + cleanup(); |
| 107 | + } |
| 108 | + }); |
| 109 | + |
| 110 | + it('CONTROL — undeclared draws today\'s class string byte for byte, never guessed from the field name', async () => { |
| 111 | + const rise = await derivedBadge(120, 100, undefined); |
| 112 | + expect(rise.className).toBe('flex items-center shrink-0 font-medium text-emerald-600 dark:text-emerald-400'); |
| 113 | + cleanup(); |
| 114 | + const fall = await derivedBadge(80, 100, undefined); |
| 115 | + expect(fall.className).toBe('flex items-center shrink-0 font-medium text-rose-600 dark:text-rose-400'); |
| 116 | + }); |
| 117 | + |
| 118 | + it('colours the static `trend` badge by the same key', () => { |
| 119 | + mount({ fallbackValue: 4.2, trend: { value: 12, direction: 'up' }, polarity: 'lower' }); |
| 120 | + const badge = screen.getByText('12%'); |
| 121 | + expect(badge.className).toMatch(BAD); |
| 122 | + expect(arrow(badge)).toMatch(/arrow-up/); |
| 123 | + }); |
| 124 | + |
| 125 | + it('CONTROL — a static badge with no `direction` takes no tone under any polarity', () => { |
| 126 | + mount({ fallbackValue: 4.2, trend: { value: 12 }, polarity: 'lower' }); |
| 127 | + const badge = screen.getByText('12%'); |
| 128 | + expect(badge.className).toBe('flex items-center shrink-0 font-medium'); |
| 129 | + }); |
| 130 | +}); |
0 commit comments