Skip to content

Commit 537808f

Browse files
committed
test(jsx-ast): add test to increase coverage of handling overloads
1 parent 48a8e0c commit 537808f

3 files changed

Lines changed: 72 additions & 2 deletions

File tree

packages/react/src/html/ui/components/OverloadTabs/index.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable react-x/no-array-index-key */
12
import Tabs from '@node-core/ui-components/Common/Tabs';
23
import * as TabsPrimitive from '@radix-ui/react-tabs';
34

packages/react/src/html/ui/components/OverloadTabs/index.module.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
margin-top: calc(var(--spacing, 0.25rem) * 2);
1515
}
1616

17-
.panel[data-state="inactive"] {
17+
.panel[data-state='inactive'] {
1818
opacity: 0;
1919
visibility: hidden;
2020
pointer-events: none;

packages/react/src/jsx-ast/utils/__tests__/buildContent.test.mjs

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import { describe, it } from 'node:test';
33

44
import { setConfig } from '@doc-kit/core/utils/configuration/index.mjs';
55

6-
import { transformHeadingNode, gatherChangeEntries } from '../buildContent.mjs';
6+
import {
7+
transformHeadingNode,
8+
gatherChangeEntries,
9+
groupOverloadsIntoTabs,
10+
} from '../buildContent.mjs';
711

812
const heading = {
913
type: 'heading',
@@ -142,3 +146,68 @@ describe('gatherChangeEntries', () => {
142146
assert.equal(result[1].label, 'Added new feature.');
143147
});
144148
});
149+
150+
describe('groupOverloadsIntoTabs', () => {
151+
it('groups consecutive overloads into a single OverloadTabs component', () => {
152+
const originalEntries = [
153+
{ heading: { data: { name: 'funcA', isOverload: false } } },
154+
{ heading: { depth: 3, data: { name: 'funcB', isOverload: false } } },
155+
{ heading: { depth: 3, data: { name: 'funcB', isOverload: true } } },
156+
{ heading: { depth: 3, data: { name: 'funcB', isOverload: true } } },
157+
{ heading: { data: { name: 'funcC', isOverload: false } } },
158+
];
159+
160+
const makeNode = (className, bodyText) => ({
161+
type: 'element',
162+
tagName: 'div',
163+
properties: { className },
164+
children: [
165+
{ type: 'element', tagName: 'h3', depth: 3 }, // The heading to be stripped
166+
{ type: 'text', value: bodyText },
167+
],
168+
});
169+
170+
const processedChildren = [
171+
makeNode('entry-a', 'body a'),
172+
makeNode('entry-b1', 'body b1'),
173+
makeNode('entry-b2', 'body b2'),
174+
makeNode('entry-b3', 'body b3'),
175+
makeNode('entry-c', 'body c'),
176+
];
177+
178+
const result = groupOverloadsIntoTabs(processedChildren, originalEntries);
179+
180+
// 0: funcA, 1: funcB-heading, 2: Overloads-heading, 3: OverloadTabs(funcB), 4: funcC
181+
assert.equal(result.length, 5);
182+
183+
// First element is untouched
184+
assert.equal(result[0].properties.className, 'entry-a');
185+
186+
// Second element is the extracted heading
187+
assert.equal(result[1].tagName, 'h3');
188+
189+
// Third element is the "Overloads" heading
190+
assert.equal(result[2].children[0].value, 'Overloads');
191+
192+
// Fourth element is the OverloadTabs component
193+
const tabsComponent = result[3];
194+
assert.equal(tabsComponent.name, 'OverloadTabs');
195+
assert.equal(tabsComponent.children.length, 3); // 3 tab panels
196+
197+
// Check that the h3 was removed from the overloads and they are wrapped in overload-panel
198+
const panel1 = tabsComponent.children[0];
199+
const classAttr1 = panel1.attributes.find(a => a.name === 'className');
200+
assert.equal(classAttr1.value, 'overload-panel');
201+
assert.equal(panel1.children[0].type, 'text');
202+
assert.equal(panel1.children[0].value, 'body b1');
203+
204+
const panel2 = tabsComponent.children[1];
205+
const classAttr2 = panel2.attributes.find(a => a.name === 'className');
206+
assert.equal(classAttr2.value, 'overload-panel');
207+
assert.equal(panel2.children[0].type, 'text');
208+
assert.equal(panel2.children[0].value, 'body b2');
209+
210+
// Fifth element is untouched
211+
assert.equal(result[4].properties.className, 'entry-c');
212+
});
213+
});

0 commit comments

Comments
 (0)