Skip to content
Closed
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
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed';
import { AfterViewInit, Component, signal, TemplateRef, viewChild } from '@angular/core';
import {
AfterViewInit,
Component,
inputBinding,
outputBinding,
signal,
TemplateRef,
viewChild,
WritableSignal
} from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
Comment on lines +2 to 12

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

In Angular 18.2+, inputBinding and outputBinding are exported from @angular/core/testing, not @angular/core. Importing them from @angular/core will cause a compilation error. Please move these imports to @angular/core/testing.

import {
  AfterViewInit,
  Component,
  signal,
  TemplateRef,
  viewChild,
  WritableSignal
} from '@angular/core';
import { ComponentFixture, TestBed, inputBinding, outputBinding } from '@angular/core/testing';

import { userEvent } from '@vitest/browser/context';

import {
InnerSortEvent,
SortableTableColumnInternal,
TableColumnInternal
} from '../../types/internal.types';
import { SortPropDir } from '../../types/public.types';
import { toInternalColumn } from '../../utils/column-helper';
import { DataTableHeaderCellComponent } from './header-cell.component';
import { HeaderCellHarness } from './testing/header-cell.harnes';
Expand Down Expand Up @@ -160,3 +171,66 @@
});
});
});

describe('DataTableHeaderCellComponent - custom sort icons', () => {
let fixture: ComponentFixture<DataTableHeaderCellComponent>;
let sorts: WritableSignal<SortPropDir[]>;
const column = signal({
name: 'test',
prop: 'test',
sortable: true,
resizeable: false,
width: signal(20)
});
const sortAscendingIcon = signal('icon up');
const sortDescendingIcon = signal('icon down');

beforeEach(async () => {
sorts = signal<SortPropDir[]>([]);
fixture = TestBed.createComponent(DataTableHeaderCellComponent, {
bindings: [
inputBinding('sortType', () => 'single'),
inputBinding('ariaHeaderCheckboxMessage', () => 'Select All'),
inputBinding('sortAscendingIcon', sortAscendingIcon),
inputBinding('sortDescendingIcon', sortDescendingIcon),
inputBinding('column', column),
inputBinding('sorts', sorts),
inputBinding('showResizeHandle', () => false),
outputBinding('sort', (event: InnerSortEvent) => {
sorts.set([{ prop: event.column.prop!, dir: event.newValue! }]);
})
]
});
await fixture.whenStable();
});
Comment on lines +203 to +205

it('should apply custom sortAscendingIcon class when toggling to ascending sort', async () => {
const label = fixture.nativeElement.querySelector('.datatable-header-cell-label');
await userEvent.click(label);

Check failure on line 209 in projects/ngx-datatable/src/lib/components/header/header-cell.component.spec.ts

View workflow job for this annotation

GitHub Actions / build

`userEvent` is experimental
await fixture.whenStable();

const sortBtn = fixture.nativeElement.querySelector('.sort-btn');

expect(sortBtn).toHaveClass('sort-btn');
expect(sortBtn).toHaveClass('sort-asc');
expect(sortBtn).toHaveClass('icon');
expect(sortBtn).toHaveClass('up');
expect(sortBtn).not.toHaveClass('datatable-icon-up');
});

it('should apply custom sortDescendingIcon class when toggling to descending sort', async () => {
const label = fixture.nativeElement.querySelector('.datatable-header-cell-label');
await userEvent.click(label);

Check failure on line 223 in projects/ngx-datatable/src/lib/components/header/header-cell.component.spec.ts

View workflow job for this annotation

GitHub Actions / build

`userEvent` is experimental
await fixture.whenStable();

await userEvent.click(label);

Check failure on line 226 in projects/ngx-datatable/src/lib/components/header/header-cell.component.spec.ts

View workflow job for this annotation

GitHub Actions / build

`userEvent` is experimental
await fixture.whenStable();

const sortButton = fixture.nativeElement.querySelector('.sort-btn');
expect(sortButton).toHaveClass('sort-btn');
expect(sortButton).toHaveClass('sort-desc');
expect(sortButton).toHaveClass('icon');
expect(sortButton).toHaveClass('down');
expect(sortButton).not.toHaveClass('datatable-icon-down');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ export class DataTableHeaderCellComponent implements OnInit, OnDestroy {

protected readonly isCheckboxable = computed(() => this.column().headerCheckboxable);

protected readonly sortClass = computed<string[] | undefined>(() => {
return this.calcSortClass(this.sortDir());
protected readonly sortClass = computed<string | undefined>(() => {
return this.calcSortClass(this.sortDir())?.join(' ');

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please move the join inside calcSortClass and please either inline the function then or at least convert to an arrow function without {}

});
protected readonly sortDir = computed<SortDirection | undefined>(() => {
return this.calcSortDir(this.sorts());
Expand Down
2 changes: 1 addition & 1 deletion projects/ngx-datatable/tsconfig.spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "../../out-tsc/spec",
"types": ["vitest/globals"]
"types": ["vitest/globals", "vitest/browser"]
},
"include": ["src/**/*.spec.ts", "src/**/*.d.ts"]
}
Loading