diff --git a/packages/common/src/core/__tests__/slickGrid.spec.ts b/packages/common/src/core/__tests__/slickGrid.spec.ts index 6c58a9c50..be3674a24 100644 --- a/packages/common/src/core/__tests__/slickGrid.spec.ts +++ b/packages/common/src/core/__tests__/slickGrid.spec.ts @@ -19,8 +19,8 @@ describe('SlickGrid core file', () => { it('should be able to instantiate SlickGrid without DataView', () => { const columns = [{ id: 'firstName', field: 'firstName', name: 'First Name' }] as Column[]; - const options = { enableCellNavigation: true } as GridOption; - grid = new SlickGrid('#myGrid', [], columns, options, undefined, true); + const options = { enableCellNavigation: true, devMode: { ownerNodeIndex: 0 } } as GridOption; + grid = new SlickGrid('#myGrid', [], columns, options); grid.init(); expect(grid).toBeTruthy(); @@ -29,9 +29,9 @@ describe('SlickGrid core file', () => { it('should be able to instantiate SlickGrid with a DataView', () => { const columns = [{ id: 'firstName', field: 'firstName', name: 'First Name' }] as Column[]; - const options = { enableCellNavigation: true } as GridOption; + const options = { enableCellNavigation: true, devMode: { ownerNodeIndex: 0 } } as GridOption; const dv = new SlickDataView({}); - grid = new SlickGrid('#myGrid', dv, columns, options, undefined, true); + grid = new SlickGrid('#myGrid', dv, columns, options); grid.init(); expect(grid).toBeTruthy(); diff --git a/packages/common/src/core/slickGrid.ts b/packages/common/src/core/slickGrid.ts index cac2d148a..de7992744 100644 --- a/packages/common/src/core/slickGrid.ts +++ b/packages/common/src/core/slickGrid.ts @@ -176,7 +176,6 @@ export class SlickGrid = Column, O e protected canvas: HTMLCanvasElement | null = null; protected canvas_context: CanvasRenderingContext2D | null = null; - protected _devMode = false; // settings protected _options!: O; @@ -454,7 +453,7 @@ export class SlickGrid = Column, O e * @param {Array} columns - An array of column definitions. * @param {Object} [options] - Grid this._options. **/ - constructor(protected container: HTMLElement | string, protected data: CustomDataView | TData[], protected columns: C[], protected options: Partial, protected externalPubSub?: BasePubSub, protected devMode = false) { + constructor(protected container: HTMLElement | string, protected data: CustomDataView | TData[], protected columns: C[], protected options: Partial, protected externalPubSub?: BasePubSub) { this.onActiveCellChanged = new SlickEvent('onActiveCellChanged', externalPubSub); this.onActiveCellPositionChanged = new SlickEvent('onActiveCellPositionChanged', externalPubSub); this.onAddNewRow = new SlickEvent('onAddNewRow', externalPubSub); @@ -2417,9 +2416,14 @@ export class SlickGrid = Column, O e let i: number; if (!this.stylesheet) { const sheets: any = (this._options.shadowRoot || document).styleSheets; + + if (typeof this.options.devMode?.ownerNodeIndex === 'number' && this.options.devMode.ownerNodeIndex >= 0) { + sheets[this.options.devMode.ownerNodeIndex].ownerNode = this._style; + } + for (i = 0; i < sheets.length; i++) { const sheet = sheets[i]; - if ((sheet.ownerNode || sheet.owningElement) === this._style || this.devMode) { + if ((sheet.ownerNode || sheet.owningElement) === this._style) { this.stylesheet = sheet; break; } @@ -3745,7 +3749,7 @@ export class SlickGrid = Column, O e } getViewportWidth() { - this.viewportW = parseFloat(getInnerSize(this._container, 'width') as unknown as string); + this.viewportW = parseFloat(getInnerSize(this._container, 'width') as unknown as string) || this.options.devMode?.containerClientWidth || 0; return this.viewportW; } diff --git a/packages/common/src/interfaces/gridOption.interface.ts b/packages/common/src/interfaces/gridOption.interface.ts index f3998537c..9b700c3fb 100644 --- a/packages/common/src/interfaces/gridOption.interface.ts +++ b/packages/common/src/interfaces/gridOption.interface.ts @@ -259,6 +259,9 @@ export interface GridOption { /** Default cell Formatter that will be used by the grid */ defaultFormatter?: Formatter; + /** Escape hatch geared towards testing Slickgrid in jsdom based environments to circumvent the lack of stylesheet.ownerNode and clientWidth calculations */ + devMode?: false & { ownerNodeIndex?: number; containerClientWidth?: number; }; + /** Do we have paging enabled? */ doPaging?: boolean;