Skip to content

Commit

Permalink
fix(bits): table component unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pilat-martin committed Sep 4, 2024
1 parent 83334c7 commit 86b7291
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 40 deletions.
34 changes: 10 additions & 24 deletions packages/bits/src/lib/table/table-row/table-row.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ export class TableHeaderRowComponent

public ngAfterViewInit(): void {
if (
this.tableStateHandlerService.selectable &&
this.tableStateHandlerService.selectable ||
this.tableStateHandlerService.selectionMode ===
TableSelectionMode.Multi
) {
Expand Down Expand Up @@ -402,7 +402,7 @@ export class TableHeaderRowComponent
[checked]="isRowSelected()"
(valueChange)="rowSelected()"
(click)="stopPropagation($event)"
#rowSelectionRadio
#rowSelectionElement
>
</nui-radio>
<nui-checkbox
Expand All @@ -411,7 +411,7 @@ export class TableHeaderRowComponent
[checked]="isRowSelected()"
(valueChange)="rowSelected()"
(click)="stopPropagation($event)"
#rowSelectionCheckbox
#rowSelectionElement
>
</nui-checkbox>
</td>
Expand Down Expand Up @@ -459,11 +459,8 @@ export class TableRowComponent extends CdkRow implements OnInit, OnDestroy {
return this.density.toLowerCase() === "tiny";
}

@ViewChild("rowSelectionCheckbox", { read: ElementRef, static: false })
private rowSelectionCheckbox: ElementRef;

@ViewChild("rowSelectionRadio", { read: ElementRef, static: false })
private rowSelectionRadio: ElementRef;
@ViewChild("rowSelectionElement", { read: ElementRef, static: false })
private rowSelectionElement: ElementRef;

private onDestroy$ = new Subject<void>();

Expand Down Expand Up @@ -495,6 +492,7 @@ export class TableRowComponent extends CdkRow implements OnInit, OnDestroy {
this.selectable = selectable;
this.changeDetectorRef.markForCheck();
});

this.tableStateHandlerService.selectionModeChanged
.pipe(takeUntil(this.onDestroy$))
.subscribe((mode: TableSelectionMode) => {
Expand Down Expand Up @@ -530,22 +528,10 @@ export class TableRowComponent extends CdkRow implements OnInit, OnDestroy {
return;
}

// TODO refactor a bit
let rowSelector: Element | null = null;
if (this.selectionMode === TableSelectionMode.Multi) {
rowSelector = closestTableRow.querySelector("nui-checkbox");

if (rowSelector === this.rowSelectionCheckbox.nativeElement) {
this.rowSelected();
return;
}
}
if (this.selectionMode === TableSelectionMode.Radio) {
rowSelector = closestTableRow.querySelector("nui-radio");

if (rowSelector === this.rowSelectionRadio.nativeElement) {
this.rowSelected();
}
let rowSelector: Element | null = closestTableRow.querySelector(".nui-table__table-cell__checkbox");
if (rowSelector === this.rowSelectionElement?.nativeElement) {
this.rowSelected();
return;
}
}

Expand Down
30 changes: 15 additions & 15 deletions packages/bits/src/lib/table/table.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -551,32 +551,32 @@ describe("components >", () => {
});

it("should emit selection from table on row click if clickable rows enabled", () => {
spyOn(component, "checkboxClicked");
spyOn(component, "rowSelected");
spyOnProperty(
tableStateHandlerService,
"selectable",
"selectionEnabled",
"get"
).and.returnValue(true);
fixture.componentInstance.clickableRow = true;

const checkbox = {};
component.rowSelectionCheckbox = { nativeElement: checkbox };
const selectionElement = {};
component.rowSelectionElement = { nativeElement: selectionElement };
const eventTarget = constructEventTarget(
{ querySelector: () => checkbox },
{ querySelector: () => selectionElement },
null
);

fixture.componentInstance.rowClickHandler(
<HTMLElement>eventTarget
);
expect(component.checkboxClicked).toHaveBeenCalled();
expect(component.rowSelected).toHaveBeenCalled();
});

it("should not emit selection from table on row click if clickable rows enabled but an ignored element was clicked", () => {
spyOn(component, "checkboxClicked");
spyOn(component, "rowSelected");
spyOnProperty(
tableStateHandlerService,
"selectable",
"selectionEnabled",
"get"
).and.returnValue(true);
fixture.componentInstance.clickableRow = true;
Expand All @@ -589,14 +589,14 @@ describe("components >", () => {
fixture.componentInstance.rowClickHandler(
<HTMLElement>eventTarget
);
expect(component.checkboxClicked).not.toHaveBeenCalled();
expect(component.rowSelected).not.toHaveBeenCalled();
});

it("should not emit selection from an unselectable table", () => {
spyOn(component, "checkboxClicked");
spyOn(component, "rowSelected");
spyOnProperty(
tableStateHandlerService,
"selectable",
"selectionEnabled",
"get"
).and.returnValue(false);
fixture.componentInstance.clickableRow = true;
Expand All @@ -606,14 +606,14 @@ describe("components >", () => {
fixture.componentInstance.rowClickHandler(
<HTMLElement>eventTarget
);
expect(component.checkboxClicked).not.toHaveBeenCalled();
expect(component.rowSelected).not.toHaveBeenCalled();
});

it("should not emit selection from a selectable table when clickable rows are not enabled", () => {
spyOn(component, "checkboxClicked");
spyOn(component, "rowSelected");
spyOnProperty(
tableStateHandlerService,
"selectable",
"selectionEnabled",
"get"
).and.returnValue(true);
fixture.componentInstance.clickableRow = false;
Expand All @@ -623,7 +623,7 @@ describe("components >", () => {
fixture.componentInstance.rowClickHandler(
<HTMLElement>eventTarget
);
expect(component.checkboxClicked).not.toHaveBeenCalled();
expect(component.rowSelected).not.toHaveBeenCalled();
});
});
describe("columns types >", () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/bits/src/lib/table/table.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export class TableComponent<T>
@Input() sortable = false;
@Input() resizable = false;
/**
* @deprecated Use selectionMode instead.
* @deprecated Use selectionConfig instead.
*/
@Input() selectable = false;
@Input() selectionConfig: TableSelectionConfig = {
Expand Down

0 comments on commit 86b7291

Please sign in to comment.