redoc/lib/components/SideMenu/side-menu.spec.js

211 lines
7.3 KiB
JavaScript
Raw Normal View History

2015-12-21 22:39:40 +03:00
'use strict';
2015-12-22 18:02:03 +03:00
import { getChildDebugElement, mouseclick} from 'tests/helpers';
2016-05-05 11:05:02 +03:00
import {Component, provide, ViewMetadata} from '@angular/core';
import {BrowserDomAdapter} from '@angular/platform-browser/src/browser/browser_adapter';
2016-05-06 12:46:41 +03:00
import { OptionsService } from 'lib/services/index';
2015-12-21 22:39:40 +03:00
import {
2016-04-30 00:45:53 +03:00
inject,
async,
2015-12-21 22:39:40 +03:00
beforeEach,
beforeEachProviders,
it
2016-05-06 00:48:41 +03:00
} from '@angular/core/testing';
import { TestComponentBuilder } from '@angular/compiler/testing';
2015-12-21 22:39:40 +03:00
import {redocEvents} from 'lib/events';
import MethodsList from 'lib/components/MethodsList/methods-list';
import SideMenu from 'lib/components/SideMenu/side-menu';
import SchemaManager from 'lib/utils/SchemaManager';
2016-05-06 12:46:41 +03:00
let testOptions = new OptionsService();
2016-02-10 18:18:36 +03:00
testOptions.options = {
scrollYOffset: () => 0,
2015-12-27 00:12:10 +03:00
scrollParent: window
2015-12-26 20:44:39 +03:00
};
2016-02-10 18:18:36 +03:00
2015-12-21 22:39:40 +03:00
describe('Redoc components', () => {
describe('SideMenu Component', () => {
let builder;
let component;
let fixture;
beforeEachProviders(() => [
2015-12-26 20:44:39 +03:00
provide(SchemaManager, {useValue: new SchemaManager()}),
provide(BrowserDomAdapter, {useValue: new BrowserDomAdapter()}),
2016-05-06 12:46:41 +03:00
provide(OptionsService, {useValue: testOptions})
2015-12-21 22:39:40 +03:00
]);
2016-04-30 00:45:53 +03:00
beforeEach(async(inject([TestComponentBuilder, SchemaManager], (tcb, schemaMgr) => {
2015-12-21 22:39:40 +03:00
builder = tcb;
2016-04-30 00:45:53 +03:00
return schemaMgr.load('/tests/schemas/extended-petstore.yml');
})));
2015-12-21 22:39:40 +03:00
afterEach(() => {
if (fixture) fixture.destroy();
});
2015-12-22 18:02:03 +03:00
describe('window parent case', () => {
beforeEach((done) => {
builder.createAsync(TestApp).then(_fixture => {
fixture = _fixture;
component = getChildDebugElement(fixture.debugElement, 'side-menu').componentInstance;
fixture.detectChanges();
done();
}, err => {
throw err;
});
});
it('should init component and component data', () => {
expect(component).not.toBeNull();
expect(component.data).not.toBeNull();
});
it('should run hashScroll when redoc bootstrapped', (done) => {
2016-02-10 18:18:36 +03:00
spyOn(component.dom, 'getLocation').and.returnValue({hash: ''});
2015-12-22 18:02:03 +03:00
spyOn(component, 'hashScroll').and.stub();
spyOn(window, 'scrollTo').and.stub();
redocEvents.bootstrapped.next();
setTimeout(() => {
expect(component.hashScroll).toHaveBeenCalled();
expect(window.scrollTo).not.toHaveBeenCalled();
window.scrollTo.and.callThrough();
component.hashScroll.and.callThrough();
done();
});
});
2016-02-07 17:10:32 +03:00
it('should scroll to method when location hash is present [jp]', (done) => {
let hash = '#tag/pet/paths/~1pet~1findByStatus/get';
2016-02-10 18:18:36 +03:00
spyOn(component.dom, 'getLocation').and.returnValue({hash: hash});
2016-02-07 17:10:32 +03:00
spyOn(component, 'hashScroll').and.callThrough();
spyOn(window, 'scrollTo').and.stub();
redocEvents.bootstrapped.next();
setTimeout(() => {
expect(component.hashScroll).toHaveBeenCalled();
let scrollY = window.scrollTo.calls.argsFor(0)[1];
expect(scrollY).toBeGreaterThan(0);
done();
});
});
it('should scroll to method when location hash is present [operation]', (done) => {
let hash = '#operation/getPetById';
2016-02-10 18:18:36 +03:00
spyOn(component.dom, 'getLocation').and.returnValue({hash: hash});
2015-12-22 18:02:03 +03:00
spyOn(component, 'hashScroll').and.callThrough();
spyOn(window, 'scrollTo').and.stub();
redocEvents.bootstrapped.next();
setTimeout(() => {
expect(component.hashScroll).toHaveBeenCalled();
let scrollY = window.scrollTo.calls.argsFor(0)[1];
expect(scrollY).toBeGreaterThan(0);
done();
});
});
2015-12-21 22:39:40 +03:00
});
2016-02-10 18:18:36 +03:00
describe('scrollable div parent case', () => {
2015-12-22 18:02:03 +03:00
let menuNativeEl;
beforeEach((done) => {
let scollableDivTmpl =
`<div style="height: 500px; overflow-y: auto;">
<side-menu></side-menu>
<methods-list></methods-list>
</div>`;
builder = builder.overrideView(
TestApp, new ViewMetadata({template: scollableDivTmpl, directives: [MethodsList, SideMenu]}));
builder.createAsync(TestApp).then(_fixture => {
fixture = _fixture;
component = getChildDebugElement(fixture.debugElement, 'side-menu').componentInstance;
menuNativeEl = getChildDebugElement(fixture.debugElement, 'side-menu').nativeElement;
2016-02-10 18:18:36 +03:00
component.options.scrollParent = _fixture.nativeElement.children[0];
component.$scrollParent = _fixture.nativeElement.children[0];
2015-12-22 18:02:03 +03:00
fixture.detectChanges();
done();
}, err => {
throw err;
});
});
it('should init component and component data', () => {
expect(component).not.toBeNull();
expect(component.data).not.toBeNull();
});
2016-02-07 17:10:32 +03:00
it('should scroll to method when location hash is present [jp]', (done) => {
let hash = '#tag/pet/paths/~1pet~1findByStatus/get';
2016-02-10 18:18:36 +03:00
spyOn(component.dom, 'getLocation').and.returnValue({hash: hash});
2016-02-07 17:10:32 +03:00
spyOn(component, 'hashScroll').and.callThrough();
redocEvents.bootstrapped.next();
setTimeout(() => {
expect(component.hashScroll).toHaveBeenCalled();
2016-02-10 18:18:36 +03:00
expect(component.$scrollParent.scrollTop).toBeGreaterThan(0);
2016-02-07 17:10:32 +03:00
done();
});
});
it('should scroll to method when location hash is present [operation]', (done) => {
let hash = '#operation/getPetById';
2016-02-10 18:18:36 +03:00
spyOn(component.dom, 'getLocation').and.returnValue({hash: hash});
2015-12-22 18:02:03 +03:00
spyOn(component, 'hashScroll').and.callThrough();
redocEvents.bootstrapped.next();
setTimeout(() => {
expect(component.hashScroll).toHaveBeenCalled();
2016-02-10 18:18:36 +03:00
expect(component.$scrollParent.scrollTop).toBeGreaterThan(0);
2015-12-22 18:02:03 +03:00
done();
});
});
it('should select next/prev menu item when scrolled down/up', () => {
component.activeCatIdx.should.be.equal(0);
component.activeMethodIdx.should.be.equal(-1);
let elTop = component.getCurrentMethodEl().getBoundingClientRect().bottom;
2016-02-10 18:18:36 +03:00
component.$scrollParent.scrollTop = elTop + 1;
2015-12-22 18:02:03 +03:00
//simulate scroll down
spyOn(component, 'scrollY').and.returnValue(elTop + 2);
component.scrollHandler();
component.activeCatIdx.should.be.equal(1);
2016-02-10 18:18:36 +03:00
component.$scrollParent.scrollTop = elTop - 1;
2015-12-22 18:02:03 +03:00
//simulate scroll up
component.scrollY.and.returnValue(elTop - 2);
component.scrollHandler();
component.activeCatIdx.should.be.equal(0);
});
it('should activate menu item on click', () => {
let menuItemEl = menuNativeEl.querySelector('li');
expect(menuItemEl).not.toHaveCssClass('active');
mouseclick(menuItemEl);
fixture.detectChanges();
expect(menuItemEl).toHaveCssClass('active');
});
it('should scroll to appropriate element when click on menu label', () => {
2016-02-10 18:18:36 +03:00
component.$scrollParent.scrollTop.should.be.equal(0);
2015-12-22 18:02:03 +03:00
let menuItemEl = menuNativeEl.querySelector('li');
mouseclick(menuItemEl);
2016-02-10 18:18:36 +03:00
component.$scrollParent.scrollTop.should.be.above(0);
2015-12-21 22:39:40 +03:00
});
});
});
});
/** Test component that contains an ApiInfo. */
@Component({
selector: 'test-app',
2015-12-21 22:39:40 +03:00
directives: [MethodsList, SideMenu],
providers: [SchemaManager],
template:
`<side-menu></side-menu>
<methods-list></methods-list>`
})
class TestApp {
}