redoc/lib/services/menu.service.spec.ts

102 lines
3.2 KiB
TypeScript
Raw Normal View History

2016-05-09 22:55:16 +03:00
'use strict';
2016-11-24 16:29:29 +03:00
import { Component } from '@angular/core';
2016-05-09 22:55:16 +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-11-24 16:29:29 +03:00
import { MethodsList } from '../components/MethodsList/methods-list';
2016-05-09 22:55:16 +03:00
import { MenuService } from './menu.service';
import { Hash } from './hash.service';
2016-11-23 02:23:32 +03:00
import { LazyTasksService } from '../shared/components/LazyFor/lazy-for';
2016-11-24 16:29:29 +03:00
import { ScrollService } from './scroll.service';
import { SchemaHelper } from './schema-helper.service';
2016-10-23 20:18:42 +03:00
import { SpecManager } from '../utils/spec-manager';;
2016-05-09 22:55:16 +03:00
describe('Menu service', () => {
2016-09-02 23:18:31 +03:00
beforeEach(() => {
2016-11-24 16:29:29 +03:00
TestBed.configureTestingModule({ declarations: [ TestAppComponent, MethodsList ] });
2016-09-02 23:18:31 +03:00
});
2016-11-23 02:23:32 +03:00
let menu, hashService, scroll, tasks;
2016-11-24 16:29:29 +03:00
let specMgr;
2016-05-09 22:55:16 +03:00
2016-11-24 16:29:29 +03:00
beforeEach(inject([SpecManager, Hash, ScrollService, LazyTasksService],
2016-11-23 02:23:32 +03:00
( _specMgr, _hash, _scroll, _tasks) => {
2016-05-09 22:55:16 +03:00
hashService = _hash;
scroll = _scroll;
2016-11-23 02:23:32 +03:00
tasks = _tasks;
specMgr = _specMgr;
2016-11-24 16:29:29 +03:00
SchemaHelper.setSpecManager(specMgr);
}));
beforeEach(done => {
specMgr.load('/tests/schemas/extended-petstore.yml').then(done, done.fail);
});
2016-05-09 22:55:16 +03:00
2016-07-01 15:53:16 +03:00
beforeEach(() => {
2016-11-24 16:29:29 +03:00
menu = TestBed.get(MenuService);
2016-09-02 23:18:31 +03:00
let fixture = TestBed.createComponent(TestAppComponent);
2016-07-01 15:53:16 +03:00
fixture.detectChanges();
2016-05-09 22:55:16 +03:00
});
it('should scroll to method when location hash is present [jp]', (done) => {
2016-07-26 12:03:15 +03:00
let hash = '#tag/pet/paths/~1pet~1findByStatus/get';
2016-11-23 02:23:32 +03:00
spyOn(menu, 'scrollToActive').and.callThrough();
2016-05-09 22:55:16 +03:00
spyOn(window, 'scrollTo').and.stub();
2016-10-23 20:18:42 +03:00
hashService.value.subscribe((hash) => {
if (!hash) return;
2016-11-23 02:23:32 +03:00
expect(menu.scrollToActive).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();
});
2016-10-23 20:18:42 +03:00
hashService.value.next(hash);
2016-05-09 22:55:16 +03:00
});
2016-11-24 16:29:29 +03:00
//
2016-05-09 22:55:16 +03:00
it('should scroll to method when location hash is present [operation]', (done) => {
let hash = '#operation/getPetById';
2016-11-23 02:23:32 +03:00
spyOn(menu, 'scrollToActive').and.callThrough();
2016-05-09 22:55:16 +03:00
spyOn(window, 'scrollTo').and.stub();
2016-10-23 20:18:42 +03:00
hashService.value.subscribe((hash) => {
if (!hash) return;
2016-11-23 02:23:32 +03:00
expect(menu.scrollToActive).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();
});
2016-10-23 20:18:42 +03:00
hashService.value.next(hash);
2016-05-09 22:55:16 +03:00
});
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);
2016-10-31 21:15:56 +03:00
let nextElTop = menu.getRelativeCatOrItem(1).getBoundingClientRect().top;
2016-05-09 22:55:16 +03:00
2016-10-31 21:15:56 +03:00
scroll.$scrollParent.scrollTop = nextElTop + 1;
2016-05-09 22:55:16 +03:00
//simulate scroll down
2016-10-31 21:15:56 +03:00
spyOn(scroll, 'scrollY').and.returnValue(nextElTop + 10);
2016-05-09 22:55:16 +03:00
menu.scrollUpdate(true);
menu.activeCatIdx.should.be.equal(1);
2016-10-31 21:15:56 +03:00
scroll.scrollY.and.returnValue(nextElTop - 2);
scroll.$scrollParent.scrollTop = nextElTop - 1;
2016-05-09 22:55:16 +03:00
menu.scrollUpdate(false);
menu.activeCatIdx.should.be.equal(0);
});
});
@Component({
selector: 'test-app',
template:
`<div id='parent' style='height: 500px; overflow:auto'>
2016-10-31 11:15:04 +03:00
<api-info></api-info>
2016-05-09 22:55:16 +03:00
<methods-list></methods-list>
</div>`
})
2016-06-13 20:54:24 +03:00
class TestAppComponent {
2016-05-09 22:55:16 +03:00
}