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

193 lines
5.7 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-12-25 20:15:24 +03:00
import { MenuService, MenuItem } from './menu.service';
2016-05-09 22:55:16 +03:00
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-12-25 20:15:24 +03:00
let menu:MenuService, 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', () => {
2016-12-25 20:15:24 +03:00
// enable all items
menu.items.forEach(item => item.ready = true);
2016-05-09 22:55:16 +03:00
scroll.$scrollParent = document.querySelector('#parent');
2016-12-25 20:15:24 +03:00
menu.activeIdx.should.be.equal(-1);
2016-05-09 22:55:16 +03:00
2016-12-25 20:15:24 +03:00
let nextElTop = menu.getEl(1).getBoundingClientRect().top;
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-12-25 20:15:24 +03:00
menu.onScroll(true);
menu.activeIdx.should.be.equal(1);
2016-05-09 22:55:16 +03:00
2016-10-31 21:15:56 +03:00
scroll.scrollY.and.returnValue(nextElTop - 2);
scroll.$scrollParent.scrollTop = nextElTop - 1;
2016-12-25 20:15:24 +03:00
menu.onScroll(false);
menu.activeIdx.should.be.equal(0);
});
describe('buildMenu method', () => {
let suitSchema = {
tags: [
{name: 'tag1', description: 'info1', 'x-traitTag': true},
{name: 'tag2', description: 'info2'},
{name: 'tag4', description: 'info2', 'x-displayName': 'Tag Four'}
],
paths: {
test: {
put: {
tags: ['tag1', 'tag3'],
summary: 'test put'
},
get: {
tags: ['tag1', 'tag2'],
summary: 'test get'
},
delete: {
tags: ['tag4'],
summary: 'test delete'
},
// no tags
post: {
summary: 'test post'
}
}
}
};
let items:MenuItem[];
beforeEach(() => {
menu.items = null;
specMgr._schema = suitSchema;
menu.buildMenu();
items = menu.items;
});
it('should return instance of Array', () => {
items.should.be.instanceof(Array);
});
it('should return Array with correct number of items', () => {
// 3 - defined tags, 1 - tag3 and 1 method item for method without tag
items.length.should.be.equal(3 + 1 + 1);
});
it('should append not defined tags to the end of list', () => {
let item = items[3];
item.name.should.be.equal('tag3');
item.items.length.should.be.equal(1);
item.items[0].name.should.be.equal('test put');
});
it('should append method items without tags to the end of list', () => {
let methodItem = items[4];
methodItem.name.should.be.equal('test post');
methodItem.metadata.type.should.be.equal('method');
should.not.exist(methodItem.items);
});
it('should map x-traitTag to empty method list', () => {
let item = items[0];
should.not.exist(item.items);
});
it('methods for tag should contain valid pointer and name', () => {
for (let item of items) {
item.should.be.an.Object();
if (item.items) {
for (let subItem of item.items) {
subItem.should.have.properties(['metadata']);
let pointer = subItem.metadata.pointer;
let methSchema = specMgr.byPointer(pointer);
should.exist(methSchema);
if (methSchema.summary) {
methSchema.summary.should.be.equal(subItem.name);
}
}
}
}
});
it('should use x-displayName to set custom names', () => {
let info = items[2];
info.id.should.be.equal('tag/tag4');
info.name.should.be.equal('Tag Four');
});
2016-05-09 22:55:16 +03:00
});
});
@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
}