Skip to content
Merged
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
104 changes: 58 additions & 46 deletions e2e/src/app.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,60 +20,72 @@ describe('ScrollCollapse Lib E2E Tests', function() {
});
});

describe('scroll direction', () => {
it('should add scrolling direction class', () => {
page.scrollTo();
expect(page.getNavElement().getAttribute('class')).not.toContain(
'sn-scrolling-down'
);
expect(page.getNavElement().getAttribute('class')).not.toContain(
'sn-scrolling-up'
);
const runTests = (height = 768) => {
describe('scroll direction', () => {
it('should add scrolling direction class', () => {
page.scrollTo();
expect(page.getNavElement().getAttribute('class')).not.toContain(
'sn-scrolling-down',
);
expect(page.getNavElement().getAttribute('class')).not.toContain(
'sn-scrolling-up',
);

page.scrollTo(0, 10);
page.scrollTo(0, 200);
expect(page.getNavElement().getAttribute('class')).toContain(
'sn-scrolling-down'
);
expect(page.getNavElement().getAttribute('class')).not.toContain(
'sn-scrolling-up'
);
page.scrollTo(0, 10);
page.scrollTo(0, 200);
expect(page.getNavElement().getAttribute('class')).toContain(
'sn-scrolling-down',
);
expect(page.getNavElement().getAttribute('class')).not.toContain(
'sn-scrolling-up',
);

page.scrollTo(0, 100);
expect(page.getNavElement().getAttribute('class')).not.toContain(
'sn-scrolling-down'
);
expect(page.getNavElement().getAttribute('class')).toContain(
'sn-scrolling-up'
);
page.scrollTo(0, 100);
expect(page.getNavElement().getAttribute('class')).not.toContain(
'sn-scrolling-down',
);
expect(page.getNavElement().getAttribute('class')).toContain(
'sn-scrolling-up',
);
});
});
});

describe('minimise mode', () => {
it('should add "sn-minimise" class', () => {
page.scrollTo();
page.scrollTo(0, 10);
expect(page.getNavElement().getAttribute('class')).not.toContain(
'sn-minimise'
);
describe('minimise mode', () => {
it('should add "sn-minimise" class', () => {
page.scrollTo();
page.scrollTo(0, 10);
expect(page.getNavElement().getAttribute('class')).not.toContain(
'sn-minimise',
);

page.scrollTo(0, 110);
expect(page.getNavElement().getAttribute('class')).toContain(
'sn-minimise'
);
page.scrollTo(0, 110);
expect(page.getNavElement().getAttribute('class')).toContain(
'sn-minimise',
);
});
});
});

describe('affix mode', () => {
it('should add "sn-affix" class', () => {
page.scrollTo();
page.scrollTo(0, 10);
expect(page.getBarElement().getAttribute('class')).not.toContain(
'sn-affix'
);
describe('affix mode', () => {
it('should add "sn-affix" class', () => {
page.scrollTo();
page.scrollTo(0, 10);
expect(page.getBarElement().getAttribute('class')).not.toContain(
'sn-affix',
);

page.scrollTo(0, 768 * 3);
expect(page.getBarElement().getAttribute('class')).toContain('sn-affix');
page.scrollTo(0, height * 3);
expect(page.getBarElement().getAttribute('class')).toContain(
'sn-affix',
);
});
});
};

runTests();

describe('resize', () => {
beforeEach(() => page.resize(1024, 600));

runTests(600);
});
});
8 changes: 8 additions & 0 deletions e2e/src/app.po.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ export class AppPage {
browser.sleep(100);
}

resize(width: number, height: number) {
browser.driver
.manage()
.window()
.setSize(width, height);
browser.sleep(100);
}

getNavElement() {
return element(by.css('.nav'));
}
Expand Down
41 changes: 33 additions & 8 deletions src/app/scroll-collapse/scroll-collapse.directive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ describe('ScrollCollapseDirective', () => {
let directive: ScrollCollapseDirective;
let ngZone;
let windowRef;
let cdRef;

beforeEach(() => {
node = document.createElement('p');
Expand All @@ -21,48 +22,52 @@ describe('ScrollCollapseDirective', () => {
.and.callFake(fn => fn()),
};
windowRef = {
triggerEvent: null,
triggerEvent: {},
pageXOffset: 0,
pageYOffset: 0,
innerWidth: 1366,
innerHeight: 768,
addEventListener: (name, fn) => (windowRef.triggerEvent = fn),
addEventListener: (name, fn) => (windowRef.triggerEvent[name] = fn),
removeEventListener: () => null,
};
directive = new ScrollCollapseDirective(el, ngZone, windowRef);
cdRef = {
detectChanges: () => null,
};
directive = new ScrollCollapseDirective(el, ngZone, cdRef, windowRef);
directive.ngAfterViewInit();
});

describe('scroll event', () => {
it('should call event handler', fakeAsync(() => {
const spy = spyOn(directive, 'onScrollOrResizeEvent').and.callThrough();
const spy = spyOn(directive, 'onScrollEvent').and.callThrough();
const events = [
{ pageXOffset: 0, pageYOffset: 0, width: 1366, height: 768 },
{ pageXOffset: 0, pageYOffset: 50, width: 1366, height: 768 },
];
directive.debounce = 100;
directive.ngAfterViewInit();
windowRef.triggerEvent();
windowRef.triggerEvent.scroll();
tick(50);
expect(spy).not.toHaveBeenCalled();
windowRef.pageYOffset = 50;
windowRef.triggerEvent();
windowRef.triggerEvent.scroll();
tick(100);
expect(spy).toHaveBeenCalledWith(events);
expect(directive.isScrollingUp).toBeFalsy();
expect(directive.isScrollingDown).toBeTruthy();

windowRef.pageYOffset = 0;
windowRef.triggerEvent();
windowRef.triggerEvent.scroll();
tick(100);
expect(spy.calls.mostRecent().args).toEqual([events.reverse()]);
expect(directive.isScrollingUp).toBeTruthy();
expect(directive.isScrollingDown).toBeFalsy();
}));

it('should remove event handler on destroy', () => {
const spy = spyOn(directive, 'onScrollOrResizeEvent').and.callThrough();
const spy = spyOn(directive, 'onScrollEvent').and.callThrough();
directive.ngOnDestroy();
windowRef.triggerEvent.scroll();
expect(spy).not.toHaveBeenCalled();
});
});
Expand Down Expand Up @@ -339,4 +344,24 @@ describe('ScrollCollapseDirective', () => {
expect(directive.affixMode).toBeFalsy();
});
});

describe('resize event', () => {
it('should call event handler', fakeAsync(() => {
const spy = spyOn(directive, 'onResizeEvent').and.callThrough();
directive.debounce = 100;
directive.ngAfterViewInit();
windowRef.triggerEvent.resize();
tick(50);
expect(spy).not.toHaveBeenCalled();
windowRef.triggerEvent.resize();
tick(100);
expect(spy).toHaveBeenCalled();
}));

it('should get original top and height', () => {
const spy = spyOn(directive, 'getOriginalTopAndHeight').and.callThrough();
directive.onResizeEvent();
expect(spy).toHaveBeenCalled();
});
});
});
Loading