2016-05-09 22:55:16 +03:00
|
|
|
'use strict';
|
|
|
|
import { provide, Component } from '@angular/core';
|
|
|
|
import { BrowserDomAdapter } from '@angular/platform-browser/src/browser/browser_adapter';
|
|
|
|
import {
|
|
|
|
inject,
|
|
|
|
beforeEach,
|
|
|
|
describe,
|
|
|
|
beforeEachProviders,
|
2016-06-23 17:36:38 +03:00
|
|
|
it,
|
|
|
|
async
|
2016-05-09 22:55:16 +03:00
|
|
|
} from '@angular/core/testing';
|
|
|
|
|
|
|
|
import { TestComponentBuilder } from '@angular/compiler/testing';
|
|
|
|
|
|
|
|
import { MenuService } from './menu.service';
|
|
|
|
import { Hash } from './hash.service';
|
|
|
|
import { ScrollService } from './scroll.service';
|
2016-06-12 20:44:34 +03:00
|
|
|
import { MethodsList } from '../components/index';
|
2016-06-22 21:17:48 +03:00
|
|
|
import { SpecManager } from '../utils/SpecManager';;
|
2016-05-09 22:55:16 +03:00
|
|
|
|
|
|
|
describe('Menu service', () => {
|
|
|
|
let menu, hashService, scroll;
|
|
|
|
let builder;
|
2016-06-23 17:36:38 +03:00
|
|
|
let specMgr = new SpecManager();
|
2016-05-09 22:55:16 +03:00
|
|
|
|
|
|
|
beforeEachProviders(() => [
|
2016-06-23 17:36:38 +03:00
|
|
|
provide(BrowserDomAdapter, {useClass: BrowserDomAdapter}),
|
|
|
|
provide(Hash, {useClass: Hash}),
|
|
|
|
provide(ScrollService, {useClass: ScrollService}),
|
|
|
|
provide(SpecManager, {useValue: new SpecManager()})
|
2016-05-09 22:55:16 +03:00
|
|
|
]);
|
|
|
|
|
2016-06-23 17:36:38 +03:00
|
|
|
beforeEach(async(inject([Hash, ScrollService, TestComponentBuilder, SpecManager],
|
|
|
|
(_hash, _scroll, tcb, _specMgr) => {
|
2016-05-09 22:55:16 +03:00
|
|
|
hashService = _hash;
|
|
|
|
scroll = _scroll;
|
|
|
|
builder = tcb;
|
2016-06-23 17:36:38 +03:00
|
|
|
specMgr = _specMgr;
|
|
|
|
})));
|
2016-05-09 22:55:16 +03:00
|
|
|
|
2016-06-23 17:36:38 +03:00
|
|
|
beforeEach(done => {
|
|
|
|
specMgr.load('/tests/schemas/extended-petstore.yml').then(r => {
|
2016-05-09 22:55:16 +03:00
|
|
|
done();
|
2016-06-23 17:36:38 +03:00
|
|
|
}).catch(e => {
|
|
|
|
done.fail(e);
|
|
|
|
});
|
2016-05-09 22:55:16 +03:00
|
|
|
});
|
|
|
|
|
2016-06-23 17:36:38 +03:00
|
|
|
beforeEach(done => {
|
|
|
|
menu = new MenuService(hashService, scroll, specMgr);
|
|
|
|
builder.createAsync(TestAppComponent).then(fixture => {
|
2016-05-09 22:55:16 +03:00
|
|
|
fixture.detectChanges();
|
|
|
|
done();
|
2016-06-23 17:36:38 +03:00
|
|
|
}).catch(err => done.fail(err) );
|
2016-05-09 22:55:16 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should run hashScroll when hash changed', (done) => {
|
|
|
|
spyOn(menu, 'hashScroll').and.callThrough();
|
|
|
|
hashService.changed.subscribe(() => {
|
|
|
|
expect(menu.hashScroll).toHaveBeenCalled();
|
|
|
|
menu.hashScroll.and.callThrough();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
hashService.changed.next();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should scroll to method when location hash is present [jp]', (done) => {
|
|
|
|
let hash = '#tag/pet/paths/~1pet~1findByStatus/get';
|
|
|
|
spyOn(menu, 'hashScroll').and.callThrough();
|
|
|
|
spyOn(window, 'scrollTo').and.stub();
|
|
|
|
hashService.changed.subscribe(() => {
|
|
|
|
expect(menu.hashScroll).toHaveBeenCalled();
|
2016-06-12 20:44:34 +03:00
|
|
|
let scrollY = (<jasmine.Spy>window.scrollTo).calls.argsFor(0)[1];
|
2016-05-09 22:55:16 +03:00
|
|
|
expect(scrollY).toBeGreaterThan(0);
|
2016-06-12 20:44:34 +03:00
|
|
|
(<jasmine.Spy>window.scrollTo).and.callThrough();
|
2016-05-09 22:55:16 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
hashService.changed.next(hash);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should scroll to method when location hash is present [operation]', (done) => {
|
|
|
|
let hash = '#operation/getPetById';
|
|
|
|
spyOn(menu, 'hashScroll').and.callThrough();
|
|
|
|
spyOn(window, 'scrollTo').and.stub();
|
|
|
|
hashService.changed.subscribe(() => {
|
|
|
|
expect(menu.hashScroll).toHaveBeenCalled();
|
2016-06-12 20:44:34 +03:00
|
|
|
let scrollY = (<jasmine.Spy>window.scrollTo).calls.argsFor(0)[1];
|
2016-05-09 22:55:16 +03:00
|
|
|
expect(scrollY).toBeGreaterThan(0);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
hashService.changed.next(hash);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should select next/prev menu item when scrolled down/up', () => {
|
|
|
|
scroll.$scrollParent = document.querySelector('#parent');
|
|
|
|
menu.activeCatIdx.should.be.equal(0);
|
|
|
|
menu.activeMethodIdx.should.be.equal(-1);
|
|
|
|
let elTop = menu.getCurrentMethodEl().getBoundingClientRect().bottom;
|
|
|
|
|
|
|
|
scroll.$scrollParent.scrollTop = elTop + 1;
|
|
|
|
|
|
|
|
//simulate scroll down
|
|
|
|
spyOn(scroll, 'scrollY').and.returnValue(elTop + 2);
|
|
|
|
menu.scrollUpdate(true);
|
|
|
|
menu.activeCatIdx.should.be.equal(1);
|
|
|
|
|
|
|
|
scroll.scrollY.and.returnValue(elTop - 2);
|
|
|
|
scroll.$scrollParent.scrollTop = elTop - 1;
|
|
|
|
menu.scrollUpdate(false);
|
|
|
|
menu.activeCatIdx.should.be.equal(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'test-app',
|
|
|
|
directives: [ MethodsList ],
|
|
|
|
template:
|
|
|
|
`<div id='parent' style='height: 500px; overflow:auto'>
|
|
|
|
<methods-list></methods-list>
|
|
|
|
</div>`
|
|
|
|
})
|
2016-06-13 20:54:24 +03:00
|
|
|
class TestAppComponent {
|
2016-05-09 22:55:16 +03:00
|
|
|
}
|