|
1 | 1 | import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed'; |
2 | | -import { AfterViewInit, Component, signal, TemplateRef, viewChild } from '@angular/core'; |
| 2 | +import { |
| 3 | + AfterViewInit, |
| 4 | + Component, |
| 5 | + inputBinding, |
| 6 | + outputBinding, |
| 7 | + signal, |
| 8 | + TemplateRef, |
| 9 | + viewChild, |
| 10 | + WritableSignal |
| 11 | +} from '@angular/core'; |
3 | 12 | import { ComponentFixture, TestBed } from '@angular/core/testing'; |
| 13 | +import { userEvent } from '@vitest/browser/context'; |
4 | 14 |
|
5 | 15 | import { |
6 | 16 | InnerSortEvent, |
7 | 17 | SortableTableColumnInternal, |
8 | 18 | TableColumnInternal |
9 | 19 | } from '../../types/internal.types'; |
| 20 | +import { SortPropDir } from '../../types/public.types'; |
10 | 21 | import { toInternalColumn } from '../../utils/column-helper'; |
11 | 22 | import { DataTableHeaderCellComponent } from './header-cell.component'; |
12 | 23 | import { HeaderCellHarness } from './testing/header-cell.harnes'; |
@@ -160,3 +171,69 @@ describe('DataTableHeaderCellComponent with template', () => { |
160 | 171 | }); |
161 | 172 | }); |
162 | 173 | }); |
| 174 | + |
| 175 | +describe('DataTableHeaderCellComponent - custom sort icons', () => { |
| 176 | + let fixture: ComponentFixture<DataTableHeaderCellComponent>; |
| 177 | + let sorts: WritableSignal<SortPropDir[]>; |
| 178 | + const column = signal({ |
| 179 | + name: 'test', |
| 180 | + prop: 'test', |
| 181 | + sortable: true, |
| 182 | + resizeable: false, |
| 183 | + width: signal(20) |
| 184 | + }); |
| 185 | + const sortAscendingIcon = signal('icon up'); |
| 186 | + const sortDescendingIcon = signal('icon down'); |
| 187 | + |
| 188 | + beforeEach(async () => { |
| 189 | + sorts = signal<SortPropDir[]>([]); |
| 190 | + fixture = TestBed.createComponent(DataTableHeaderCellComponent, { |
| 191 | + bindings: [ |
| 192 | + inputBinding('sortType', () => 'single'), |
| 193 | + inputBinding('ariaHeaderCheckboxMessage', () => 'Select All'), |
| 194 | + inputBinding('sortAscendingIcon', sortAscendingIcon), |
| 195 | + inputBinding('sortDescendingIcon', sortDescendingIcon), |
| 196 | + inputBinding('column', column), |
| 197 | + inputBinding('sorts', sorts), |
| 198 | + inputBinding('showResizeHandle', () => false), |
| 199 | + outputBinding('sort', (event: InnerSortEvent) => { |
| 200 | + sorts.set([{ prop: event.column.prop!, dir: event.newValue! }]); |
| 201 | + }) |
| 202 | + ] |
| 203 | + }); |
| 204 | + await fixture.whenStable(); |
| 205 | + }); |
| 206 | + |
| 207 | + it('should apply custom sortAscendingIcon class when toggling to ascending sort', async () => { |
| 208 | + const label = fixture.nativeElement.querySelector('.datatable-header-cell-label'); |
| 209 | + // eslint-disable-next-line @angular-eslint/no-experimental |
| 210 | + await userEvent.click(label); |
| 211 | + await fixture.whenStable(); |
| 212 | + |
| 213 | + const sortBtn = fixture.nativeElement.querySelector('.sort-btn'); |
| 214 | + |
| 215 | + expect(sortBtn).toHaveClass('sort-btn'); |
| 216 | + expect(sortBtn).toHaveClass('sort-asc'); |
| 217 | + expect(sortBtn).toHaveClass('icon'); |
| 218 | + expect(sortBtn).toHaveClass('up'); |
| 219 | + expect(sortBtn).not.toHaveClass('datatable-icon-up'); |
| 220 | + }); |
| 221 | + |
| 222 | + it('should apply custom sortDescendingIcon class when toggling to descending sort', async () => { |
| 223 | + const label = fixture.nativeElement.querySelector('.datatable-header-cell-label'); |
| 224 | + // eslint-disable-next-line @angular-eslint/no-experimental |
| 225 | + await userEvent.click(label); |
| 226 | + await fixture.whenStable(); |
| 227 | + |
| 228 | + // eslint-disable-next-line @angular-eslint/no-experimental |
| 229 | + await userEvent.click(label); |
| 230 | + await fixture.whenStable(); |
| 231 | + |
| 232 | + const sortButton = fixture.nativeElement.querySelector('.sort-btn'); |
| 233 | + expect(sortButton).toHaveClass('sort-btn'); |
| 234 | + expect(sortButton).toHaveClass('sort-desc'); |
| 235 | + expect(sortButton).toHaveClass('icon'); |
| 236 | + expect(sortButton).toHaveClass('down'); |
| 237 | + expect(sortButton).not.toHaveClass('datatable-icon-down'); |
| 238 | + }); |
| 239 | +}); |
0 commit comments