Fix tests + don't crash on non-existing menu-items

This commit is contained in:
Roman Hotsiy 2017-02-02 20:44:29 +02:00
parent d9bec1bd6a
commit 82575d6a77
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0
3 changed files with 11 additions and 13 deletions

View File

@ -34,7 +34,8 @@ export class RedocSearch implements OnInit {
this.items = Object.keys(searchRes).map(id => ({
menuItem: this.menu.getItemById(id),
pointers: searchRes[id].map(el => el.pointer)
}));
})).filter(res => !!res.menuItem);
this.items.sort((a, b) => {
if (a.menuItem.depth > b.menuItem.depth) return 1;
else if (a.menuItem.depth < b.menuItem.depth) return -1;

View File

@ -4,20 +4,21 @@ import { MdRenderer } from '../../lib/utils/md-renderer';
describe('Utils', () => {
describe('Markdown renderer', () => {
let mdRender;
let mdRender: MdRenderer;
beforeEach(() => {
mdRender = new MdRenderer();
});
it('should return a level-1 heading even though level-2 is passed', () => {
it('should return a level-1 heading even though only level-2 is present', () => {
mdRender.renderMd('## Sub Intro');
expect(mdRender.firstLevelHeadings.length).toEqual(1);
expect(mdRender.firstLevelHeadings).toEqual(['Sub Intro']);
Object.keys(mdRender.headings).length.should.be.equal(1);
should.exist(mdRender.headings['sub-intro']);
});
it('should return a level-1 heading and a level-2', () => {
it('should return a level-2 heading as a child of level-1', () => {
mdRender.renderMd('# Introduction \n ## Sub Intro');
expect(mdRender.firstLevelHeadings.length).toEqual(1);
expect(mdRender.firstLevelHeadings).toEqual(['Introduction']);
expect(mdRender.secondLevelHeadings).toEqual(['Introduction/Sub Intro']);
Object.keys(mdRender.headings).length.should.be.equal(1);
should.exist(mdRender.headings['introduction']);
should.exist(mdRender.headings['introduction'].children);
Object.keys(mdRender.headings['introduction'].children).length.should.be.equal(1);
});
});
});

View File

@ -107,10 +107,6 @@ export class MdRenderer {
return this._origRules.open(tokens, idx);
} else {
let content = tokens[idx + 1].content;
// if no h1 1st, convert h2 to h1
if (this.firstLevelHeadings.length === 0 && tokens[idx].hLevel !== 1 ) {
tokens[idx].hLevel = 1;
}
if (tokens[idx].hLevel === 1 ) {
this.currentTopHeading = this.saveHeading(content);;
let id = this.currentTopHeading.id;