Add a few tests + remove unused code

This commit is contained in:
Roman Hotsiy 2017-01-07 18:05:08 +02:00
parent 12c3e31f64
commit 899c2b8398
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0
5 changed files with 148 additions and 28 deletions

View File

@ -0,0 +1,58 @@
'use strict';
import {
Component
} from '@angular/core';
import {
ComponentFixture,
inject,
fakeAsync,
tick,
TestBed,
} from '@angular/core/testing';
import { getChildDebugElement } from '../../../tests/helpers';
import { LoadingBar } from './loading-bar';
describe('Redoc components', () => {
describe('Loading Bar', () => {
let component: LoadingBar;
it('should init component', () => {
let fixture = TestBed.createComponent(LoadingBar);
component = fixture.componentInstance;
fixture.detectChanges();
should.exist(component);
component.progress.should.be.equal(0);
component.display.should.be.equal('block');
});
it('should hide itself in 500ms if progress is 100', fakeAsync(() => {
TestBed.configureTestingModule({ declarations: [ TestAppComponent ] });
let fixture = TestBed.createComponent(TestAppComponent);
let parentComp = fixture.componentInstance;
component = getChildDebugElement(fixture.debugElement, 'loading-bar').componentInstance;
// need to pass update through parent component as ngOnChanges is run only for view changes
parentComp.progress = 50;
fixture.detectChanges();
parentComp.progress = 100;
fixture.detectChanges();
component.display.should.be.equal('block');
tick(500);
component.display.should.be.equal('none');
}));
});
});
/** Test component that contains an ApiInfo. */
@Component({
selector: 'test-app',
template:
`<loading-bar [progress]="progress"></loading-bar>`
})
class TestAppComponent {
progress = 0;
}

View File

@ -2,14 +2,14 @@
import { getChildDebugElement } from '../../../tests/helpers'; import { getChildDebugElement } from '../../../tests/helpers';
import { Component } from '@angular/core'; import { Component } from '@angular/core';
import { OptionsService } from '../../services/index'; import { OptionsService, MenuItem } from '../../services/index';
import { import {
inject, inject,
async async
} from '@angular/core/testing'; } from '@angular/core/testing';
import { TestBed } from '@angular/core/testing'; import { TestBed, ComponentFixture } from '@angular/core/testing';
import { MethodsList, SideMenu } from '../index'; import { MethodsList, SideMenu } from '../index';
@ -23,8 +23,8 @@ describe('Redoc components', () => {
}); });
describe('SideMenu Component', () => { describe('SideMenu Component', () => {
let builder; let builder;
let component; let component: SideMenu;
let fixture; let fixture: ComponentFixture<TestAppComponent>;
let specMgr; let specMgr;
beforeEach(inject([SpecManager, OptionsService], beforeEach(inject([SpecManager, OptionsService],
@ -53,8 +53,34 @@ describe('Redoc components', () => {
}); });
it('should init component and component data', () => { it('should init component and component data', () => {
expect(component).not.toBeNull(); should.exist(component);
expect(component.data).not.toBeNull(); });
it('should clear active item and cat captions on change to null', () => {
component.activeCatCaption = 'test';
component.activeItemCaption = 'test';
component.changed(null);
component.activeCatCaption.should.be.equal('');
component.activeItemCaption.should.be.equal('');
});
it('should set active item and cat captions on change event', () => {
let parentItem: MenuItem = {
id: 'id',
name: 'Item'
};
component.changed(parentItem);
component.activeCatCaption.should.be.equal(parentItem.name);
component.activeItemCaption.should.be.equal('');
let childItem: MenuItem = {
id: 'id2',
name: 'Child',
parent: parentItem
};
component.changed(childItem);
component.activeCatCaption.should.be.equal(parentItem.name);
component.activeItemCaption.should.be.equal(childItem.name);
}); });
}); });
}); });

View File

@ -0,0 +1,56 @@
'use strict';
import { Clipboard } from './clipboard.service';
describe('Clipboard Service', () => {
let el:Node;
let copiedText = null;
function createEl(html) {
let tmpDiv = document.createElement('div');
tmpDiv.innerHTML = html;
document.body.appendChild(tmpDiv);
return tmpDiv.lastChild;
}
beforeEach(() => {
spyOn(Clipboard, 'copySelected').and.callFake(() => {
copiedText = window.getSelection().toString();
return true;
});
});
afterEach(() => {
copiedText = null;
if (el && el.parentNode) el.parentNode.removeChild(el);
(<jasmine.Spy>Clipboard.copySelected).and.callThrough();
});
it('selectElement should select element text', () => {
el = createEl('<div>Test</div>');
Clipboard.selectElement(el);
let selected = window.getSelection().toString();
selected.should.be.equal('Test');
});
it('deselect should clear selection', () => {
el = createEl('<div>Test</div>');
Clipboard.selectElement(el);
let selected = window.getSelection().toString();
selected.should.be.equal('Test');
Clipboard.deselect();
window.getSelection().toString().should.be.equal('');
});
it('copyElement should copy and deselect', () => {
el = createEl('<div>Test</div>');
Clipboard.copyElement(el);
copiedText.should.be.equal('Test');
window.getSelection().toString().should.be.equal('');
});
it('copyCustom should copy custom text', () => {
Clipboard.copyCustom('Custom text');
copiedText.should.be.equal('Custom text');
});
});

View File

@ -38,11 +38,11 @@ describe('Common components', () => {
expect(component.stickBottom).not.toHaveBeenCalled(); expect(component.stickBottom).not.toHaveBeenCalled();
}); });
it('should stick to the top on the next VM tick', (done) => { it('should stick to the top on the next animation frame', (done) => {
spyOn(component, 'stick').and.callThrough(); spyOn(component, 'stick').and.callThrough();
spyOn(component, 'stickBottom').and.callThrough(); spyOn(component, 'stickBottom').and.callThrough();
fixture.detectChanges(); fixture.detectChanges();
setTimeout(() => { requestAnimationFrame(() => {
expect(component.stick).toHaveBeenCalled(); expect(component.stick).toHaveBeenCalled();
expect(component.stickBottom).toHaveBeenCalled(); expect(component.stickBottom).toHaveBeenCalled();
done(); done();

View File

@ -14,16 +14,6 @@ export class BrowserDomAdapter {
return () => { el.removeEventListener(evt, listener, false); }; return () => { el.removeEventListener(evt, listener, false); };
} }
static addClass(element: any /** TODO #9100 */, className: string) { element.classList.add(className); }
static removeClass(element: any /** TODO #9100 */, className: string) {
element.classList.remove(className);
}
static hasClass(element: any /** TODO #9100 */, className: string): boolean {
return element.classList.contains(className);
}
static attributeMap(element: any /** TODO #9100 */): Map<string, string> { static attributeMap(element: any /** TODO #9100 */): Map<string, string> {
var res = new Map<string, string>(); var res = new Map<string, string>();
var elAttrs = element.attributes; var elAttrs = element.attributes;
@ -59,15 +49,5 @@ export class BrowserDomAdapter {
return element.getAttribute(attribute); return element.getAttribute(attribute);
} }
static setAttribute(element: any /** TODO #9100 */, name: string, value: string) {
element.setAttribute(name, value);
}
static removeAttribute(element: any /** TODO #9100 */, attribute: string) {
element.removeAttribute(attribute);
}
static getLocation(): Location { return window.location; }
static defaultDoc(): HTMLDocument { return document; } static defaultDoc(): HTMLDocument { return document; }
} }