|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * framework#3588 — a funnel's shape asserts a sequence. |
| 11 | + * |
| 12 | + * The renderer used to sort funnel segments by value descending, |
| 13 | + * unconditionally — so a sales pipeline rendered as a tidy narrowing shape |
| 14 | + * regardless of the stages' real order, and any server-side ordering was |
| 15 | + * silently overridden. With a declared `categoryOrder` the pipeline order wins; |
| 16 | + * without one the value-descending default is preserved. |
| 17 | + */ |
| 18 | + |
| 19 | +import React from 'react'; |
| 20 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 21 | +import { render, cleanup } from '@testing-library/react'; |
| 22 | + |
| 23 | +// Recharts' ResponsiveContainer measures its parent via ResizeObserver, which |
| 24 | +// reports 0×0 under the headless DOM — so the funnel never paints. Replace it |
| 25 | +// with a fixed-size passthrough so segments actually render. |
| 26 | +vi.mock('recharts', async () => { |
| 27 | + const actual = await vi.importActual<any>('recharts'); |
| 28 | + return { |
| 29 | + ...actual, |
| 30 | + ResponsiveContainer: ({ children }: any) => |
| 31 | + React.cloneElement(children, { width: 320, height: 320 }), |
| 32 | + }; |
| 33 | +}); |
| 34 | + |
| 35 | +import AdvancedChartImpl from './AdvancedChartImpl'; |
| 36 | + |
| 37 | +afterEach(cleanup); |
| 38 | + |
| 39 | +/** |
| 40 | + * Rows as analytics returns them: alphabetical, and deliberately NOT |
| 41 | + * monotonically narrowing — Proposal (200) is the largest, so a value sort and |
| 42 | + * the declared pipeline order produce visibly different funnels. |
| 43 | + */ |
| 44 | +const PIPELINE = [ |
| 45 | + { stage: 'Needs Analysis', total_amount: 120 }, |
| 46 | + { stage: 'Negotiation', total_amount: 90 }, |
| 47 | + { stage: 'Proposal', total_amount: 200 }, |
| 48 | + { stage: 'Qualification', total_amount: 150 }, |
| 49 | +]; |
| 50 | + |
| 51 | +/** As `buildCategoryOrder` emits it: value and label adjacent, in declared order. */ |
| 52 | +const DECLARED = [ |
| 53 | + 'qualification', 'Qualification', |
| 54 | + 'needs_analysis', 'Needs Analysis', |
| 55 | + 'proposal', 'Proposal', |
| 56 | + 'negotiation', 'Negotiation', |
| 57 | +]; |
| 58 | + |
| 59 | +/** |
| 60 | + * The segment labels in the order Recharts drew them. `<Funnel>` renders one |
| 61 | + * `<text>` label per trapezoid in source order, which is exactly the ordering |
| 62 | + * under test. |
| 63 | + */ |
| 64 | +function drawnOrder(container: HTMLElement): string[] { |
| 65 | + return Array.from(container.querySelectorAll('text')) |
| 66 | + .map((t) => t.textContent?.trim() ?? '') |
| 67 | + .filter(Boolean); |
| 68 | +} |
| 69 | + |
| 70 | +function renderFunnel(extra: Record<string, unknown>) { |
| 71 | + return render( |
| 72 | + <AdvancedChartImpl |
| 73 | + chartType="funnel" |
| 74 | + data={PIPELINE} |
| 75 | + xAxisKey="stage" |
| 76 | + series={[{ dataKey: 'total_amount' }]} |
| 77 | + config={{ total_amount: { label: 'Amount' } }} |
| 78 | + isAnimationActive={false} |
| 79 | + {...extra} |
| 80 | + />, |
| 81 | + ); |
| 82 | +} |
| 83 | + |
| 84 | +describe('funnel stage order (#3588)', () => { |
| 85 | + it('draws every stage (guards the assertions below against an empty render)', () => { |
| 86 | + const { container } = renderFunnel({ categoryOrder: DECLARED }); |
| 87 | + expect(container.querySelectorAll('.recharts-funnel-trapezoid')).toHaveLength(4); |
| 88 | + expect(drawnOrder(container)).toHaveLength(4); |
| 89 | + }); |
| 90 | + |
| 91 | + it('draws in the DECLARED pipeline order, not by value', () => { |
| 92 | + const { container } = renderFunnel({ categoryOrder: DECLARED }); |
| 93 | + expect(drawnOrder(container)).toEqual([ |
| 94 | + 'Qualification', 'Needs Analysis', 'Proposal', 'Negotiation', |
| 95 | + ]); |
| 96 | + }); |
| 97 | + |
| 98 | + it('keeps the value-descending default when no order is declared', () => { |
| 99 | + const { container } = renderFunnel({}); |
| 100 | + expect(drawnOrder(container)).toEqual([ |
| 101 | + 'Proposal', 'Qualification', 'Needs Analysis', 'Negotiation', |
| 102 | + ]); |
| 103 | + }); |
| 104 | + |
| 105 | + it('keeps a category missing from the declared order, after the declared ones', () => { |
| 106 | + const { container } = render( |
| 107 | + <AdvancedChartImpl |
| 108 | + chartType="funnel" |
| 109 | + data={[...PIPELINE, { stage: 'Closed Won', total_amount: 10 }]} |
| 110 | + xAxisKey="stage" |
| 111 | + series={[{ dataKey: 'total_amount' }]} |
| 112 | + config={{ total_amount: { label: 'Amount' } }} |
| 113 | + isAnimationActive={false} |
| 114 | + categoryOrder={DECLARED} |
| 115 | + />, |
| 116 | + ); |
| 117 | + // Never dropped — an unlisted stage still renders, and lands last. |
| 118 | + expect(drawnOrder(container)).toEqual([ |
| 119 | + 'Qualification', 'Needs Analysis', 'Proposal', 'Negotiation', 'Closed Won', |
| 120 | + ]); |
| 121 | + }); |
| 122 | + |
| 123 | + it('orders by the stored VALUE too, for rows the server left unlabeled', () => { |
| 124 | + const { container } = render( |
| 125 | + <AdvancedChartImpl |
| 126 | + chartType="funnel" |
| 127 | + data={[ |
| 128 | + { stage: 'negotiation', total_amount: 90 }, |
| 129 | + { stage: 'qualification', total_amount: 150 }, |
| 130 | + { stage: 'proposal', total_amount: 200 }, |
| 131 | + ]} |
| 132 | + xAxisKey="stage" |
| 133 | + series={[{ dataKey: 'total_amount' }]} |
| 134 | + config={{ total_amount: { label: 'Amount' } }} |
| 135 | + isAnimationActive={false} |
| 136 | + categoryOrder={DECLARED} |
| 137 | + />, |
| 138 | + ); |
| 139 | + expect(drawnOrder(container)).toEqual(['qualification', 'proposal', 'negotiation']); |
| 140 | + }); |
| 141 | +}); |
0 commit comments