redoc/lib/shared/components/StickySidebar/sticky-sidebar.spec.ts

89 lines
2.4 KiB
TypeScript
Raw Normal View History

2015-12-26 20:44:39 +03:00
'use strict';
2016-06-12 20:44:34 +03:00
import { getChildDebugElementByType } from '../../../../tests/helpers';
2016-07-01 15:53:16 +03:00
import { Component } from '@angular/core';
2015-12-26 20:44:39 +03:00
import {
inject,
2016-09-02 23:18:31 +03:00
TestBed
2016-05-09 22:55:16 +03:00
} from '@angular/core/testing';
2016-06-12 20:44:34 +03:00
import { StickySidebar } from '../index';
2015-12-26 20:44:39 +03:00
describe('Common components', () => {
2016-09-02 23:18:31 +03:00
beforeEach(() => {
TestBed.configureTestingModule({ declarations: [ TestApp ] });
});
2015-12-26 20:44:39 +03:00
describe('StickySidebar Component', () => {
let builder;
let component;
let fixture;
2016-12-02 13:10:12 +03:00
beforeEach(() => {
2016-09-02 23:18:31 +03:00
fixture = TestBed.createComponent(TestApp);
2016-07-01 15:53:16 +03:00
let debugEl = getChildDebugElementByType(fixture.debugElement, StickySidebar);
component = debugEl.injector.get(StickySidebar);
2016-12-02 13:10:12 +03:00
});
2015-12-26 20:44:39 +03:00
it('should init component', () => {
expect(component).not.toBeNull();
});
2016-08-31 23:51:56 +03:00
it('should start unsticked', () => {
2015-12-26 20:44:39 +03:00
spyOn(component, 'stick').and.callThrough();
2016-08-31 23:51:56 +03:00
spyOn(component, 'stickBottom').and.callThrough();
2015-12-26 20:44:39 +03:00
fixture.detectChanges();
2016-08-31 23:51:56 +03:00
expect(component.stick).not.toHaveBeenCalled();
expect(component.stickBottom).not.toHaveBeenCalled();
});
2017-01-07 19:05:08 +03:00
it('should stick to the top on the next animation frame', (done) => {
2016-08-31 23:51:56 +03:00
spyOn(component, 'stick').and.callThrough();
spyOn(component, 'stickBottom').and.callThrough();
fixture.detectChanges();
2017-01-07 19:05:08 +03:00
requestAnimationFrame(() => {
2016-08-31 23:51:56 +03:00
expect(component.stick).toHaveBeenCalled();
expect(component.stickBottom).toHaveBeenCalled();
done();
});
2015-12-26 20:44:39 +03:00
});
2015-12-27 00:12:10 +03:00
it('should stick if scrolled more than scrollYOffset', () => {
2015-12-26 20:44:39 +03:00
spyOn(component, 'stick').and.callThrough();
fixture.detectChanges();
2016-01-17 22:50:42 +03:00
component.scrollParent = {
pageYOffset: 40
};
2015-12-26 20:44:39 +03:00
component.updatePosition();
expect(component.stick).toHaveBeenCalled();
});
2016-08-31 23:51:56 +03:00
// TODO: add tests for stickBottom
2015-12-26 20:44:39 +03:00
});
});
/** Test component that contains an ApiInfo. */
@Component({
selector: 'test-app',
2015-12-26 20:44:39 +03:00
template:
`<div style="padding-top: 20px">
<div style="height: 20px; position: fixed; top: 0;"> </div>
<div style="position: relative">
2016-01-21 01:09:42 +03:00
<div sticky-sidebar [scrollParent]="scrollParent" [scrollYOffset]="options.scrollYOffset">
</div>
2015-12-26 20:44:39 +03:00
</div>
</div>`
})
class TestApp {
2016-06-12 20:44:34 +03:00
options: any;
scrollParent: Window;
2015-12-26 20:44:39 +03:00
constructor() {
this.options = {};
2015-12-27 00:12:10 +03:00
this.scrollParent = window;
this.options.scrollYOffset = () => 20;
2015-12-26 20:44:39 +03:00
}
}