-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Add tests for sticky-header #6074
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
Merged
Merged
Changes from 25 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
ff4c1a4
# This is a combination of 9 commits.
sllethe 096b553
# This is a combination of 5 commits.
sllethe 2848f2c
# This is a combination of 16 commits.
sllethe fbf6350
add lib files for sticky-header
sllethe b0172d1
added tests for sticky-header
sllethe d354ef0
6e58b23
32b1b93
a555a23
optimize provider
sllethe 469036d
4100311
fix
sllethe 44201f0
4aa498a
4adcd52
783274c
rename stickyHeaderDir to stickyHeader
sllethe 9c28f34
b037d83
13d7aca
824b305
72706d8
886b622
089a0ca
3391650
54dccf6
change CSS indent to +2
sllethe cc53d28
nit
sllethe 0abc5ee
Added test for sticky-header without StickyRegion
sllethe b943a62
94e6474
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
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
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,180 @@ | ||
import {async, ComponentFixture, TestBed, fakeAsync, tick} from '@angular/core/testing'; | ||
import {Component, DebugElement, ViewChild} from '@angular/core'; | ||
import { | ||
StickyHeaderModule, | ||
CdkStickyRegion, | ||
CdkStickyHeader, | ||
STICKY_HEADER_SUPPORT_STRATEGY | ||
} from './index'; | ||
import {OverlayModule, Scrollable} from '../core/overlay/index'; | ||
import {PlatformModule} from '../core/platform/index'; | ||
import {By} from '@angular/platform-browser'; | ||
import {dispatchFakeEvent} from '@angular/cdk/testing'; | ||
|
||
|
||
|
||
describe('sticky-header with positioning not supported', () => { | ||
let fixture: ComponentFixture<StickyHeaderTest>; | ||
let testComponent: StickyHeaderTest; | ||
let stickyElement: DebugElement; | ||
let stickyParentElement: DebugElement; | ||
let scrollableElement: DebugElement; | ||
let stickyHeader: CdkStickyHeader; | ||
|
||
beforeEach(async(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [ OverlayModule, PlatformModule, StickyHeaderModule ], | ||
declarations: [StickyHeaderTest], | ||
providers: [ | ||
{provide: STICKY_HEADER_SUPPORT_STRATEGY, useValue: false}, | ||
], | ||
}); | ||
TestBed.compileComponents(); | ||
})); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(StickyHeaderTest); | ||
fixture.detectChanges(); | ||
testComponent = fixture.debugElement.componentInstance; | ||
stickyElement = fixture.debugElement.query(By.directive(CdkStickyHeader)); | ||
stickyParentElement = fixture.debugElement.query(By.directive(CdkStickyRegion)); | ||
stickyHeader = stickyElement.injector.get<CdkStickyHeader>(CdkStickyHeader); | ||
scrollableElement = fixture.debugElement.query(By.directive(Scrollable)); | ||
}); | ||
|
||
it('should be able to find stickyParent', () => { | ||
expect(stickyHeader.stickyParent).not.toBe(null); | ||
}); | ||
|
||
it('should be able to find scrollableContainer', () => { | ||
expect(stickyHeader.upperScrollableContainer).not.toBe(null); | ||
}); | ||
|
||
it('should stick in the right place when scrolled to the top of the container', fakeAsync(() => { | ||
let scrollableContainerTop = stickyHeader.upperScrollableContainer | ||
.getBoundingClientRect().top; | ||
expect(stickyHeader.element.getBoundingClientRect().top).not.toBe(scrollableContainerTop); | ||
tick(); | ||
|
||
// Scroll the scrollableContainer up to stick | ||
fixture.componentInstance.scrollDown(); | ||
// wait till animation has finished | ||
tick(100); | ||
|
||
expect(stickyHeader.element.getBoundingClientRect().top).toBe(scrollableContainerTop); | ||
})); | ||
|
||
it('should unstuck when scrolled off the top of the container', fakeAsync(() => { | ||
let scrollableContainerTop = stickyHeader.upperScrollableContainer | ||
.getBoundingClientRect().top; | ||
expect(stickyHeader.element.getBoundingClientRect().top).not.toBe(scrollableContainerTop); | ||
tick(); | ||
|
||
// Scroll the scrollableContainer up to stick | ||
fixture.componentInstance.scrollDown(); | ||
// wait till animation has finished | ||
tick(100); | ||
|
||
expect(stickyHeader.element.getBoundingClientRect().top).toBe(scrollableContainerTop); | ||
|
||
// Scroll the scrollableContainer down to unstuck | ||
fixture.componentInstance.scrollBack(); | ||
tick(100); | ||
|
||
expect(stickyHeader.element.getBoundingClientRect().top).not.toBe(scrollableContainerTop); | ||
|
||
})); | ||
}); | ||
|
||
describe('sticky-header with positioning supported', () => { | ||
let fixture: ComponentFixture<StickyHeaderTest>; | ||
let testComponent: StickyHeaderTest; | ||
let stickyElement: DebugElement; | ||
let stickyParentElement: DebugElement; | ||
let stickyHeader: CdkStickyHeader; | ||
|
||
beforeEach(async(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [ OverlayModule, PlatformModule, StickyHeaderModule ], | ||
declarations: [StickyHeaderTest], | ||
providers: [ | ||
{provide: STICKY_HEADER_SUPPORT_STRATEGY, useValue: true}, | ||
], | ||
}); | ||
TestBed.compileComponents(); | ||
})); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(StickyHeaderTest); | ||
fixture.detectChanges(); | ||
testComponent = fixture.debugElement.componentInstance; | ||
stickyElement = fixture.debugElement.query(By.directive(CdkStickyHeader)); | ||
stickyParentElement = fixture.debugElement.query(By.directive(CdkStickyRegion)); | ||
stickyHeader = stickyElement.injector.get<CdkStickyHeader>(CdkStickyHeader); | ||
}); | ||
|
||
it('should find sticky positioning is applied', () => { | ||
let position = window.getComputedStyle(stickyHeader.element).position; | ||
expect(position).not.toBe(null); | ||
expect(/sticky/i.test(position!)).toBe(true); | ||
}); | ||
}); | ||
|
||
@Component({ | ||
// Use styles to define the style of scrollable container and header, | ||
// which help test to make sure whether the header is stuck at the right position. | ||
styles:[` | ||
.scrollable-style { | ||
text-align: center; | ||
-webkit-appearance: none; | ||
-moz-appearance: none; | ||
height: 300px; | ||
overflow: auto; | ||
} | ||
.heading-style { | ||
background: whitesmoke; | ||
padding: 5px; | ||
} | ||
`], | ||
template: ` | ||
<div cdk-scrollable class="scrollable-style"> | ||
<p *ngFor="let item of items"> {{item.name}} : {{item.message}}</p> | ||
<div cdkStickyRegion> | ||
<div cdkStickyHeader class="heading-style"> | ||
<h2>Heading 1</h2> | ||
</div> | ||
<p *ngFor="let item of items"> {{item.name}} : {{item.message}}</p> | ||
<p *ngFor="let item of items"> {{item.name}} : {{item.message}}</p> | ||
<p *ngFor="let item of items"> {{item.name}} : {{item.message}}</p> | ||
</div> | ||
</div> | ||
`}) | ||
class StickyHeaderTest { | ||
@ViewChild(Scrollable) scrollingContainer: Scrollable; | ||
|
||
items: any[] = [ | ||
{'name': 'Forrest', 'message': 'Life was like a box of chocolates'}, | ||
{'name': 'Gump', 'message': 'you never know what you are gonna get'}, | ||
{'name': 'Lion King', 'message': 'Everything you see exists together'}, | ||
{'name': 'Jack', 'message': 'in a delicate balance'}, | ||
{'name': 'Garfield', 'message': 'Save Water'}, | ||
{'name': 'Shawshank', 'message': 'There is something inside'}, | ||
{'name': 'Jone', 'message': 'Enough movies?'}, | ||
]; | ||
|
||
scrollDown() { | ||
const scrollingContainerEl = this.scrollingContainer.getElementRef().nativeElement; | ||
scrollingContainerEl.scrollTop = 300; | ||
|
||
// Emit a scroll event from the scrolling element in our component. | ||
dispatchFakeEvent(scrollingContainerEl, 'scroll'); | ||
} | ||
|
||
scrollBack() { | ||
const scrollingContainerEl = this.scrollingContainer.getElementRef().nativeElement; | ||
scrollingContainerEl.scrollTop = 0; | ||
|
||
// Emit a scroll event from the scrolling element in our component. | ||
dispatchFakeEvent(scrollingContainerEl, 'scroll'); | ||
} | ||
} |
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
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.
Add a test case when there's no user-specified
cdkStickyRegion
?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.
👍 Added new test for ' no user-specified
cdkStickyRegion
'