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

107 lines
3.2 KiB
TypeScript
Raw Normal View History

2016-05-09 22:55:16 +03:00
'use strict';
2016-07-01 15:53:16 +03:00
import { Component } from '@angular/core';
2016-05-09 22:55:16 +03:00
import {
inject,
2016-07-01 15:53:16 +03:00
async,
2016-09-02 23:18:31 +03:00
TestBed
2016-05-09 22:55:16 +03:00
} from '@angular/core/testing';
import { MenuService } from './menu.service';
import { Hash } from './hash.service';
import { ScrollService } from './scroll.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(() => {
TestBed.configureTestingModule({ declarations: [ TestAppComponent ] });
});
2016-05-09 22:55:16 +03:00
let menu, hashService, scroll;
2016-07-01 15:53:16 +03:00
let specMgr;
2016-05-09 22:55:16 +03:00
2016-09-02 23:18:31 +03:00
beforeEach(async(inject([SpecManager, Hash, ScrollService],
( _specMgr, _hash, _scroll, _menu) => {
2016-05-09 22:55:16 +03:00
hashService = _hash;
scroll = _scroll;
2016-09-02 23:18:31 +03:00
specMgr = _specMgr;
2016-07-01 15:53:16 +03:00
return specMgr.load('/tests/schemas/extended-petstore.yml');
})));
2016-05-09 22:55:16 +03:00
2016-07-01 15:53:16 +03:00
beforeEach(() => {
menu = new MenuService(hashService, scroll, specMgr);
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 run hashScroll when hash changed', (done) => {
spyOn(menu, 'hashScroll').and.callThrough();
2016-10-23 20:18:42 +03:00
hashService.value.subscribe((hash) => {
if (!hash) return;
2016-05-09 22:55:16 +03:00
expect(menu.hashScroll).toHaveBeenCalled();
menu.hashScroll.and.callThrough();
done();
});
2016-10-23 20:18:42 +03:00
hashService.value.next('nonFalsy');
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-05-09 22:55:16 +03:00
spyOn(menu, 'hashScroll').and.callThrough();
spyOn(window, 'scrollTo').and.stub();
2016-10-23 20:18:42 +03:00
hashService.value.subscribe((hash) => {
if (!hash) return;
2016-05-09 22:55:16 +03:00
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();
});
2016-10-23 20:18:42 +03:00
hashService.value.next(hash);
2016-05-09 22:55:16 +03:00
});
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();
2016-10-23 20:18:42 +03:00
hashService.value.subscribe((hash) => {
if (!hash) return;
2016-05-09 22:55:16 +03:00
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();
});
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
}