@@ -3,7 +3,11 @@ import { describe, it } from 'node:test';
33
44import { 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
812const 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