From 918efa4b2076e014813ec868aa4cc40349ba271b Mon Sep 17 00:00:00 2001 From: Vildan Softic Date: Fri, 8 Dec 2023 00:35:54 +0100 Subject: [PATCH] feat: introduce devMode to support nodejs based unit testing (#946) * feat: introduce devMode to support nodejs based unit testing --- src/models/gridOption.interface.ts | 3 +++ src/slick.grid.ts | 7 ++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/models/gridOption.interface.ts b/src/models/gridOption.interface.ts index 998c9c6a..9047619d 100644 --- a/src/models/gridOption.interface.ts +++ b/src/models/gridOption.interface.ts @@ -108,6 +108,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; diff --git a/src/slick.grid.ts b/src/slick.grid.ts index a6ea69b6..7718980d 100644 --- a/src/slick.grid.ts +++ b/src/slick.grid.ts @@ -2467,6 +2467,11 @@ 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++) { if ((sheets[i].ownerNode || sheets[i].owningElement) === this._style) { this.stylesheet = sheets[i]; @@ -4285,7 +4290,7 @@ export class SlickGrid = Column, O e } getViewportWidth() { - this.viewportW = parseFloat(Utils.innerSize(this._container, 'width') as unknown as string); + this.viewportW = parseFloat(Utils.innerSize(this._container, 'width') as unknown as string) || this.options.devMode?.containerClientWidth || 0; return this.viewportW; }