Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: introduce devMode to support nodejs based unit testing #1251

Merged
merged 1 commit into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/common/src/core/__tests__/slickGrid.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any, Column>('#myGrid', [], columns, options, undefined, true);
const options = { enableCellNavigation: true, devMode: { ownerNodeIndex: 0 } } as GridOption;
grid = new SlickGrid<any, Column>('#myGrid', [], columns, options);
grid.init();

expect(grid).toBeTruthy();
Expand All @@ -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<any, Column>('#myGrid', dv, columns, options, undefined, true);
grid = new SlickGrid<any, Column>('#myGrid', dv, columns, options);
grid.init();

expect(grid).toBeTruthy();
Expand Down
12 changes: 8 additions & 4 deletions packages/common/src/core/slickGrid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ export class SlickGrid<TData = any, C extends Column<TData> = Column<TData>, O e

protected canvas: HTMLCanvasElement | null = null;
protected canvas_context: CanvasRenderingContext2D | null = null;
protected _devMode = false;

// settings
protected _options!: O;
Expand Down Expand Up @@ -454,7 +453,7 @@ export class SlickGrid<TData = any, C extends Column<TData> = Column<TData>, O e
* @param {Array<C>} columns - An array of column definitions.
* @param {Object} [options] - Grid this._options.
**/
constructor(protected container: HTMLElement | string, protected data: CustomDataView<TData> | TData[], protected columns: C[], protected options: Partial<O>, protected externalPubSub?: BasePubSub, protected devMode = false) {
constructor(protected container: HTMLElement | string, protected data: CustomDataView<TData> | TData[], protected columns: C[], protected options: Partial<O>, protected externalPubSub?: BasePubSub) {
this.onActiveCellChanged = new SlickEvent<OnActiveCellChangedEventArgs>('onActiveCellChanged', externalPubSub);
this.onActiveCellPositionChanged = new SlickEvent<SlickGridEventData>('onActiveCellPositionChanged', externalPubSub);
this.onAddNewRow = new SlickEvent<OnAddNewRowEventArgs>('onAddNewRow', externalPubSub);
Expand Down Expand Up @@ -2417,9 +2416,14 @@ export class SlickGrid<TData = any, C extends Column<TData> = Column<TData>, 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;
}
Expand Down Expand Up @@ -3745,7 +3749,7 @@ export class SlickGrid<TData = any, C extends Column<TData> = Column<TData>, 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;
}

Expand Down
3 changes: 3 additions & 0 deletions packages/common/src/interfaces/gridOption.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,9 @@ export interface GridOption<C extends Column = Column> {
/** 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;

Expand Down