forked from swimlane/ngx-datatable
-
Notifications
You must be signed in to change notification settings - Fork 28
refactor(header-cell): add unit tests #338
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
Open
chintankavathia
wants to merge
1
commit into
siemens:main
Choose a base branch
from
chintankavathia:refactor/header-cell
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
156 changes: 120 additions & 36 deletions
156
projects/ngx-datatable/src/lib/components/header/header-cell.component.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,153 @@ | ||
import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed'; | ||
import { AfterViewInit, Component, TemplateRef, viewChild } from '@angular/core'; | ||
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; | ||
|
||
import { TableColumnInternal } from '../../types/internal.types'; | ||
import { | ||
InnerSortEvent, | ||
SortableTableColumnInternal, | ||
TableColumnInternal | ||
} from '../../types/internal.types'; | ||
import { toInternalColumn } from '../../utils/column-helper'; | ||
import { DataTableHeaderCellComponent } from './header-cell.component'; | ||
import { HeaderCellHarness } from './testing/header-cell.harnes'; | ||
|
||
describe('DataTableHeaderCellComponent', () => { | ||
let fixture: ComponentFixture<DataTableHeaderCellComponent>; | ||
let component: DataTableHeaderCellComponent; | ||
let harness: HeaderCellHarness; | ||
|
||
beforeEach(waitForAsync(() => { | ||
beforeEach(waitForAsync(async () => { | ||
fixture = TestBed.createComponent(DataTableHeaderCellComponent); | ||
component = fixture.componentInstance; | ||
})); | ||
|
||
it('should emit new width on resize', () => { | ||
fixture.componentRef.setInput('column', { | ||
name: 'test', | ||
resizeable: true | ||
prop: 'test', | ||
resizeable: true, | ||
sortable: true | ||
}); | ||
fixture.componentRef.setInput('sortType', 'single'); | ||
fixture.componentRef.setInput('headerHeight', 50); | ||
fixture.componentRef.setInput('ariaHeaderCheckboxMessage', 'Select all rows'); | ||
fixture.componentInstance.sort.subscribe(sort => { | ||
fixture.componentRef.setInput('sorts', [ | ||
{ | ||
prop: sort.column.name, | ||
dir: sort.newValue | ||
} | ||
]); | ||
}); | ||
harness = await TestbedHarnessEnvironment.harnessForFixture(fixture, HeaderCellHarness); | ||
})); | ||
|
||
it('should emit new width on resize', async () => { | ||
spyOn(component.resizing, 'emit'); | ||
fixture.detectChanges(); | ||
const initialWidth = fixture.nativeElement.clientWidth; | ||
const event = new MouseEvent('mousedown'); | ||
fixture.nativeElement.querySelector('.resize-handle').dispatchEvent(event); | ||
const mouseMoveEvent = new MouseEvent('mousemove', { screenX: 100 }); | ||
document.dispatchEvent(mouseMoveEvent); | ||
const mouseUpEvent = new MouseEvent('mouseup'); | ||
document.dispatchEvent(mouseUpEvent); | ||
const initialWidth = await harness.cellWidth(); | ||
await harness.resizeCell(); | ||
const newWidth = 100 + initialWidth; | ||
expect(component.resizing.emit).toHaveBeenCalledWith({ | ||
width: newWidth, | ||
column: { name: 'test', resizeable: true } as TableColumnInternal<any> | ||
column: { | ||
name: 'test', | ||
prop: 'test', | ||
resizeable: true, | ||
sortable: true | ||
} as TableColumnInternal<any> | ||
}); | ||
}); | ||
|
||
it('should emit sort event', () => { | ||
fixture.componentRef.setInput('column', { | ||
prop: 'test', | ||
sortable: true | ||
}); | ||
it('should emit sort event', async () => { | ||
spyOn(component.sort, 'emit'); | ||
fixture.detectChanges(); | ||
const event = new MouseEvent('click'); | ||
fixture.nativeElement.querySelector('.datatable-header-cell-label').dispatchEvent(event); | ||
await harness.applySort(); | ||
expect(component.sort.emit).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should not render resize handle when showResizeHandle is false (last column)', () => { | ||
fixture.componentRef.setInput('column', { | ||
name: 'test', | ||
resizeable: true | ||
}); | ||
it('should not render resize handle when showResizeHandle is false (last column)', async () => { | ||
fixture.componentRef.setInput('showResizeHandle', false); | ||
fixture.detectChanges(); | ||
const resizeHandle = fixture.nativeElement.querySelector('.resize-handle'); | ||
expect(resizeHandle).toBeNull(); | ||
expect(await harness.hasResizeHandle()).toBe(false); | ||
}); | ||
|
||
it('should render resize handle when showResizeHandle is true', () => { | ||
it('should render resize handle when showResizeHandle is true', async () => { | ||
fixture.componentRef.setInput('showResizeHandle', true); | ||
expect(await harness.hasResizeHandle()).toBe(true); | ||
}); | ||
|
||
it('should emit select when checkbox is clicked', async () => { | ||
fixture.componentRef.setInput('column', { | ||
name: 'test', | ||
resizeable: true | ||
headerCheckboxable: true | ||
}); | ||
fixture.componentRef.setInput('showResizeHandle', true); | ||
spyOn(component.select, 'emit'); | ||
await harness.selectAllRows(); | ||
expect(component.select.emit).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should toggle sort direction on sort button click', async () => { | ||
await harness.applySort(); | ||
expect(await harness.getSortDirection()).toBe('asc'); | ||
await harness.applySort(); | ||
expect(await harness.getSortDirection()).toBe('desc'); | ||
}); | ||
|
||
it('should sort on enter key press', async () => { | ||
spyOn(component.sort, 'emit'); | ||
await harness.applySort(true); | ||
expect(component.sort.emit).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
@Component({ | ||
imports: [DataTableHeaderCellComponent], | ||
template: `<datatable-header-cell | ||
sortType="single" | ||
headerHeight="50" | ||
[column]="column" | ||
(sort)="sort($event)" | ||
/> | ||
<ng-template #headerCellTemplate let-sort="sortFn" let-column="column"> | ||
<span class="custom-header">Custom Header for {{ column.name }}</span> | ||
<button class="custom-sort-button" type="button" (click)="sort($event)" | ||
>Custom sort button</button | ||
> | ||
</ng-template> ` | ||
}) | ||
class TestHeaderCellComponent implements AfterViewInit { | ||
column: TableColumnInternal<any> = toInternalColumn([ | ||
{ | ||
name: 'test', | ||
sortable: true | ||
} | ||
])[0]; | ||
|
||
readonly headerCellTemplate = viewChild('headerCellTemplate', { read: TemplateRef<any> }); | ||
|
||
sort(event: InnerSortEvent) {} | ||
|
||
ngAfterViewInit() { | ||
this.column = { ...this.column, headerTemplate: this.headerCellTemplate() }; | ||
} | ||
} | ||
|
||
describe('DataTableHeaderCellComponent with template', () => { | ||
let fixture: ComponentFixture<TestHeaderCellComponent>; | ||
let harness: HeaderCellHarness; | ||
|
||
beforeEach(waitForAsync(async () => { | ||
fixture = TestBed.createComponent(TestHeaderCellComponent); | ||
harness = await TestbedHarnessEnvironment.harnessForFixture(fixture, HeaderCellHarness); | ||
})); | ||
|
||
it('should render custom header template', async () => { | ||
fixture.detectChanges(); | ||
const resizeHandle = fixture.nativeElement.querySelector('.resize-handle'); | ||
expect(resizeHandle).not.toBeNull(); | ||
expect(await harness.getHeaderCellText()).toContain('Custom Header for test'); | ||
}); | ||
|
||
it('should call sort function on custom button click', async () => { | ||
spyOn(fixture.componentInstance, 'sort'); | ||
await harness.clickCustomSortButton(); | ||
expect(fixture.componentInstance.sort).toHaveBeenCalledWith({ | ||
column: fixture.componentInstance.column as SortableTableColumnInternal<any>, | ||
prevValue: undefined, | ||
newValue: 'asc' | ||
}); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
projects/ngx-datatable/src/lib/components/header/testing/header-cell.harnes.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,75 @@ | ||||||||||||||
import { ComponentHarness } from '@angular/cdk/testing'; | ||||||||||||||
|
||||||||||||||
export class HeaderCellHarness extends ComponentHarness { | ||||||||||||||
static readonly hostSelector = 'datatable-header-cell'; | ||||||||||||||
|
||||||||||||||
private cellLabel = this.locatorForOptional('.datatable-header-cell-label'); | ||||||||||||||
private cellWrapper = this.locatorFor('.datatable-header-cell-template-wrap'); | ||||||||||||||
private cellResizeHandle = this.locatorForOptional('.resize-handle'); | ||||||||||||||
private cellCheckbox = this.locatorForOptional('.datatable-checkbox'); | ||||||||||||||
private sortBtn = this.locatorForOptional('.sort-btn'); | ||||||||||||||
private customSortButton = this.locatorForOptional('.custom-sort-button'); | ||||||||||||||
|
||||||||||||||
async getHeaderCellText(): Promise<string> { | ||||||||||||||
const label = await this.cellLabel(); | ||||||||||||||
const wrapperText = await (await this.cellWrapper()).text(); | ||||||||||||||
return label?.text() ?? wrapperText; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
async hasResizeHandle(): Promise<boolean> { | ||||||||||||||
const resizeHandle = await this.cellResizeHandle(); | ||||||||||||||
return !!resizeHandle; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
async resizeCell(): Promise<void> { | ||||||||||||||
const resizeHandle = await this.cellResizeHandle(); | ||||||||||||||
if (resizeHandle) { | ||||||||||||||
await resizeHandle.dispatchEvent('mousedown', { clientX: 0, screenX: 0 }); | ||||||||||||||
const mouseMove = new MouseEvent('mousemove', { clientX: 100, screenX: 100 }); | ||||||||||||||
document.dispatchEvent(mouseMove); | ||||||||||||||
const mouseUp = new MouseEvent('mouseup'); | ||||||||||||||
document.dispatchEvent(mouseUp); | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
async selectAllRows(): Promise<void> { | ||||||||||||||
const checkbox = await this.cellCheckbox(); | ||||||||||||||
if (checkbox && !(await checkbox.getProperty('checked'))) { | ||||||||||||||
await checkbox.click(); | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
async applySort(withKeyboard = false): Promise<void> { | ||||||||||||||
const sortButton = await this.sortBtn(); | ||||||||||||||
if (sortButton && !withKeyboard) { | ||||||||||||||
await sortButton.click(); | ||||||||||||||
} else { | ||||||||||||||
(await this.host()).dispatchEvent('keydown', { | ||||||||||||||
key: 'Enter' | ||||||||||||||
}); | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
async getSortDirection(): Promise<string | undefined> { | ||||||||||||||
const sortButton = await this.sortBtn(); | ||||||||||||||
if (sortButton) { | ||||||||||||||
const isAscending = await sortButton.hasClass('sort-asc'); | ||||||||||||||
const isDescending = await sortButton.hasClass('sort-desc'); | ||||||||||||||
return isAscending ? 'asc' : isDescending ? 'desc' : undefined; | ||||||||||||||
} | ||||||||||||||
return undefined; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
async cellWidth(): Promise<number> { | ||||||||||||||
const hostElement = await this.host(); | ||||||||||||||
const width = await hostElement.getProperty('offsetWidth'); | ||||||||||||||
return width ?? 0; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
async clickCustomSortButton(): Promise<void> { | ||||||||||||||
const button = await this.customSortButton(); | ||||||||||||||
if (button) { | ||||||||||||||
await button.click(); | ||||||||||||||
} | ||||||||||||||
Comment on lines
+70
to
+73
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I think failing silently is not ideal here |
||||||||||||||
} | ||||||||||||||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Those should be parameter