@@ -1548,3 +1548,117 @@ describe('GraphComponent snapAddedNodeIds before resetToPrevious', () => {
15481548 expect ( parsed . ty ) . toBeCloseTo ( 120 , 5 ) ;
15491549 } ) ) ;
15501550} ) ;
1551+
1552+ @Component ( {
1553+ selector : 'test-graph-container-resize-host' ,
1554+ template : `
1555+ <div class="graph-container" [style.width.px]="containerWidth" [style.height.px]="containerHeight">
1556+ <ngx-graph [nodes]="nodes" [links]="links" [layout]="syncLayout" [animate]="false"></ngx-graph>
1557+ </div>
1558+ ` ,
1559+ imports : [ GraphComponent ]
1560+ } )
1561+ class TestGraphContainerResizeHostComponent {
1562+ syncLayout = new TestSyncLayout ( ) ;
1563+ containerWidth = 600 ;
1564+ containerHeight = 400 ;
1565+ nodes : Node [ ] = [
1566+ { id : 'n1' , label : 'A' } ,
1567+ { id : 'n2' , label : 'B' }
1568+ ] ;
1569+ links : Edge [ ] = [ { id : 'e1' , source : 'n1' , target : 'n2' } ] ;
1570+ }
1571+
1572+ @Component ( {
1573+ selector : 'test-graph-fixed-view-host' ,
1574+ template : `<ngx-graph [nodes]="nodes" [links]="links" [layout]="syncLayout" [view]="[800, 600]"></ngx-graph>` ,
1575+ imports : [ GraphComponent ]
1576+ } )
1577+ class TestGraphFixedViewHostComponent {
1578+ syncLayout = new TestSyncLayout ( ) ;
1579+ nodes : Node [ ] = [ { id : 'n1' , label : 'A' } ] ;
1580+ links : Edge [ ] = [ ] ;
1581+ }
1582+
1583+ describe ( 'GraphComponent container resize' , ( ) => {
1584+ let resizeObserverCallback : ResizeObserverCallback | undefined ;
1585+ let OriginalResizeObserver : typeof ResizeObserver ;
1586+
1587+ beforeEach ( async ( ) => {
1588+ OriginalResizeObserver = window . ResizeObserver ;
1589+ resizeObserverCallback = undefined ;
1590+ class ResizeObserverMock {
1591+ constructor ( callback : ResizeObserverCallback ) {
1592+ resizeObserverCallback = callback ;
1593+ }
1594+ observe ( ) : void { }
1595+ disconnect ( ) : void { }
1596+ unobserve ( ) : void { }
1597+ }
1598+ ( window as any ) . ResizeObserver = ResizeObserverMock ;
1599+
1600+ await TestBed . configureTestingModule ( {
1601+ imports : [ TestGraphContainerResizeHostComponent , TestGraphFixedViewHostComponent ] ,
1602+ providers : [ LayoutService ]
1603+ } ) . compileComponents ( ) ;
1604+ } ) ;
1605+
1606+ afterEach ( ( ) => {
1607+ window . ResizeObserver = OriginalResizeObserver ;
1608+ } ) ;
1609+
1610+ it ( 'refreshViewportDimensions updates width and height without re-running layout' , fakeAsync ( ( ) => {
1611+ const fixture = TestBed . createComponent ( TestGraphContainerResizeHostComponent ) ;
1612+ fixture . detectChanges ( ) ;
1613+ flush ( ) ;
1614+ tick ( 16 ) ;
1615+ fixture . detectChanges ( ) ;
1616+ flush ( ) ;
1617+
1618+ const graph = fixture . debugElement . query ( By . directive ( GraphComponent ) ) . componentInstance as GraphComponent ;
1619+ const createGraphSpy = spyOn ( graph , 'createGraph' ) . and . callThrough ( ) ;
1620+ spyOn ( graph , 'getContainerDims' ) . and . returnValue ( { width : 960 , height : 500 } ) ;
1621+
1622+ graph . refreshViewportDimensions ( ) ;
1623+
1624+ expect ( graph . width ) . toBe ( 960 ) ;
1625+ expect ( graph . height ) . toBe ( 500 ) ;
1626+ expect ( createGraphSpy ) . not . toHaveBeenCalled ( ) ;
1627+ } ) ) ;
1628+
1629+ it ( 'observes the parent container and refreshes viewport dimensions on resize' , fakeAsync ( ( ) => {
1630+ const fixture = TestBed . createComponent ( TestGraphContainerResizeHostComponent ) ;
1631+ fixture . detectChanges ( ) ;
1632+ flush ( ) ;
1633+ tick ( 16 ) ;
1634+ fixture . detectChanges ( ) ;
1635+ flush ( ) ;
1636+
1637+ expect ( resizeObserverCallback ) . toBeDefined ( ) ;
1638+
1639+ const graph = fixture . debugElement . query ( By . directive ( GraphComponent ) ) . componentInstance as GraphComponent ;
1640+ const createGraphSpy = spyOn ( graph , 'createGraph' ) . and . callThrough ( ) ;
1641+ spyOn ( graph , 'getContainerDims' ) . and . returnValue ( { width : 840 , height : 400 } ) ;
1642+
1643+ resizeObserverCallback ! ( [ ] , { } as ResizeObserver ) ;
1644+ tick ( 16 ) ;
1645+ fixture . detectChanges ( ) ;
1646+
1647+ expect ( graph . width ) . toBe ( 840 ) ;
1648+ expect ( createGraphSpy ) . not . toHaveBeenCalled ( ) ;
1649+ } ) ) ;
1650+
1651+ it ( 'refreshViewportDimensions is a no-op when [view] supplies explicit dimensions' , fakeAsync ( ( ) => {
1652+ const fixture = TestBed . createComponent ( TestGraphFixedViewHostComponent ) ;
1653+ fixture . detectChanges ( ) ;
1654+ flush ( ) ;
1655+ tick ( 16 ) ;
1656+ fixture . detectChanges ( ) ;
1657+ flush ( ) ;
1658+
1659+ const graph = fixture . debugElement . query ( By . directive ( GraphComponent ) ) . componentInstance as GraphComponent ;
1660+ graph . refreshViewportDimensions ( ) ;
1661+ expect ( graph . width ) . toBe ( 800 ) ;
1662+ expect ( graph . height ) . toBe ( 600 ) ;
1663+ } ) ) ;
1664+ } ) ;
0 commit comments