11import { BidiModule } from '@angular/cdk/bidi' ;
22import { DataSource } from '@angular/cdk/collections' ;
33import { ESCAPE } from '@angular/cdk/keycodes' ;
4- import { ChangeDetectionStrategy , Component , Directive , ElementRef , ViewChild } from '@angular/core' ;
4+ import {
5+ ChangeDetectionStrategy ,
6+ Component ,
7+ Directive ,
8+ ElementRef ,
9+ Injectable ,
10+ ViewChild ,
11+ } from '@angular/core' ;
512import { ComponentFixture , TestBed , fakeAsync , flush } from '@angular/core/testing' ;
613import { MatTableModule } from '@angular/material/table' ;
7- import { BehaviorSubject } from 'rxjs' ;
14+ import { BehaviorSubject , Observable , ReplaySubject } from 'rxjs' ;
815import { dispatchKeyboardEvent } from '../../cdk/testing/private' ;
916
10- import { ColumnSize } from '@angular/cdk-experimental/column-resize' ;
17+ import { ColumnSize , ColumnSizeStore } from '@angular/cdk-experimental/column-resize' ;
1118import { AbstractMatColumnResize } from './column-resize-directives/common' ;
1219import {
1320 MatColumnResize ,
@@ -52,7 +59,7 @@ function getTableTemplate(defaultEnabled: boolean) {
5259 }
5360 </style>
5461 <div #table [dir]="direction">
55- <table ${ directives . table } mat-table [dataSource]="dataSource"
62+ <table ${ directives . table } mat-table [dataSource]="dataSource" id="theTable"
5663 style="table-layout: fixed;">
5764 <!-- Position Column -->
5865 <ng-container matColumnDef="position" sticky>
@@ -109,7 +116,7 @@ function getFlexTemplate(defaultEnabled: boolean) {
109116 }
110117 </style>
111118 <div #table [dir]="direction">
112- <mat-table ${ directives . table } [dataSource]="dataSource">
119+ <mat-table ${ directives . table } [dataSource]="dataSource" id="theTable" >
113120 <!-- Position Column -->
114121 <ng-container matColumnDef="position" sticky>
115122 <mat-header-cell *matHeaderCellDef
@@ -628,6 +635,63 @@ describe('Material Popover Edit', () => {
628635 } ) ) ;
629636 } ) ;
630637 }
638+
639+ describe ( 'ColumnSizeStore (persistance)' , ( ) => {
640+ let component : BaseTestComponent ;
641+ let fixture : ComponentFixture < BaseTestComponent > ;
642+ let columnSizeStore : FakeColumnSizeStore ;
643+
644+ beforeEach ( fakeAsync ( ( ) => {
645+ jasmine . addMatchers ( approximateMatcher ) ;
646+
647+ TestBed . configureTestingModule ( {
648+ imports : [ BidiModule , MatTableModule , MatColumnResizeModule ] ,
649+ providers : [
650+ FakeColumnSizeStore ,
651+ { provide : ColumnSizeStore , useExisting : FakeColumnSizeStore } ,
652+ ] ,
653+ declarations : [ MatResizeOnPushTest ] ,
654+ } ) ;
655+ fixture = TestBed . createComponent ( MatResizeOnPushTest ) ;
656+ component = fixture . componentInstance ;
657+ columnSizeStore = TestBed . inject ( FakeColumnSizeStore ) ;
658+ fixture . detectChanges ( ) ;
659+ flush ( ) ;
660+ } ) ) ;
661+
662+ it ( 'applies the persisted size' , fakeAsync ( ( ) => {
663+ ( expect ( component . getColumnWidth ( 1 ) ) . not as any ) . isApproximately ( 300 ) ;
664+
665+ columnSizeStore . emitSize ( 'theTable' , 'name' , 300 ) ;
666+
667+ flush ( ) ;
668+
669+ ( expect ( component . getColumnWidth ( 1 ) ) as any ) . isApproximately ( 300 ) ;
670+ } ) ) ;
671+
672+ it ( 'persists the user-triggered size update' , fakeAsync ( ( ) => {
673+ const initialColumnWidth = component . getColumnWidth ( 1 ) ;
674+
675+ component . triggerHoverState ( ) ;
676+ fixture . detectChanges ( ) ;
677+
678+ component . resizeColumnWithMouse ( 1 , 5 ) ;
679+ fixture . detectChanges ( ) ;
680+ flush ( ) ;
681+
682+ component . completeResizeWithMouseInProgress ( 1 ) ;
683+ flush ( ) ;
684+
685+ component . endHoverState ( ) ;
686+ fixture . detectChanges ( ) ;
687+
688+ expect ( columnSizeStore . setSizeCalls . length ) . toBe ( 1 ) ;
689+ const { tableId, columnId, sizePx} = columnSizeStore . setSizeCalls [ 0 ] ;
690+ expect ( tableId ) . toBe ( 'theTable' ) ;
691+ expect ( columnId ) . toBe ( 'name' ) ;
692+ ( expect ( sizePx ) as any ) . isApproximately ( initialColumnWidth + 5 ) ;
693+ } ) ) ;
694+ } ) ;
631695} ) ;
632696
633697function createElementData ( ) {
@@ -639,3 +703,38 @@ function createElementData() {
639703 { position : 5 , name : 'Boron' , weight : 10.811 , symbol : 'B' } ,
640704 ] ;
641705}
706+
707+ @Injectable ( )
708+ class FakeColumnSizeStore extends ColumnSizeStore {
709+ readonly emitStore = new Map < string , ReplaySubject < number > > ( ) ;
710+ readonly setSizeCalls : { tableId : string ; columnId : string ; sizePx : number } [ ] = [ ] ;
711+
712+ /** Returns an observable that will emit values from emitSize(). */
713+ override getSize ( tableId : string , columnId : string ) : Observable < number > | null {
714+ return this . _getOrAdd ( tableId , columnId ) ;
715+ }
716+
717+ /**
718+ * Adds an entry to setSizeCalls.
719+ * Note: Does not affect values returned from getSize.
720+ */
721+ override setSize ( tableId : string , columnId : string , sizePx : number ) : void {
722+ this . setSizeCalls . push ( { tableId, columnId, sizePx} ) ;
723+ }
724+
725+ /** Call this in test code to simulate persisted column sizes. */
726+ emitSize ( tableId : string , columnId : string , sizePx : number ) {
727+ const stored = this . _getOrAdd ( tableId , columnId ) ;
728+ stored . next ( sizePx ) ;
729+ }
730+
731+ private _getOrAdd ( tableId : string , columnId : string ) : ReplaySubject < number > {
732+ const key = `tableId----columnId` ;
733+ let stored = this . emitStore . get ( key ) ;
734+ if ( ! stored ) {
735+ stored = new ReplaySubject < number > ( 1 ) ;
736+ this . emitStore . set ( key , stored ) ;
737+ }
738+ return stored ;
739+ }
740+ }
0 commit comments