Redoc dispose testcase

This commit is contained in:
Roman Hotsiy 2016-01-20 16:37:23 +02:00
parent bc9e62141b
commit 5f34a700a3
2 changed files with 66 additions and 9 deletions

View File

@ -106,16 +106,22 @@ export default class Redoc extends BaseComponent {
static dispose() { static dispose() {
let dom = new BrowserDomAdapter(); let dom = new BrowserDomAdapter();
let el = dom.query('redoc'); let el = dom.query('redoc');
let parent = el.parentElement; let parent;
let nextSibling = el.nextElementSibling; let nextSibling;
if (el) {
parent = el.parentElement;
nextSibling = el.nextElementSibling;
}
Redoc.appRef && Redoc.appRef.dispose(); if (Redoc.appRef) {
Redoc.appRef.dispose();
Redoc.appRef = null; Redoc.appRef = null;
// Redoc dispose removes host element, so need to restore it // Redoc dispose removes host element, so need to restore it
el = dom.createElement('redoc'); el = dom.createElement('redoc');
el.innerText = 'Loading...'; el.innerText = 'Loading...';
parent.insertBefore(el, nextSibling); parent && parent.insertBefore(el, nextSibling);
}
} }
} }
Redoc.parameters = Redoc.parameters.concat([[OptionsManager], [ElementRef], [BrowserDomAdapter]]); Redoc.parameters = Redoc.parameters.concat([[OptionsManager], [ElementRef], [BrowserDomAdapter]]);

View File

@ -129,8 +129,59 @@ describe('Redoc init', () => {
done.fail('Error handler should not been called'); done.fail('Error handler should not been called');
}); });
}); });
});
describe('Redoc dispose', () => {
let builder;
let fixture;
let element;
let disposeSpy;
let dom = new BrowserDomAdapter();
beforeEachProviders(() => [
provide(SchemaManager, {useValue: new SchemaManager()}),
provide(BrowserDomAdapter, {useValue: new BrowserDomAdapter()}),
provide(OptionsManager, {useValue: optsMgr})
]);
beforeEach(injectAsync([TestComponentBuilder, SchemaManager], (tcb, schemaMgr) => {
builder = tcb;
return schemaMgr.load('/tests/schemas/methods-list-component.json').then(() => null, (err) => { throw err; });
}));
beforeEach((done) => {
builder.createAsync(TestApp).then(_fixture => {
fixture = _fixture;
element = getChildDebugElement(fixture.debugElement, 'methods-list').nativeElement;
disposeSpy = jasmine.createSpy('spy');
Redoc.appRef = {
dispose: disposeSpy
};
fixture.detectChanges();
done();
}, err => { throw err; });
});
afterEach(()=> {
fixture.destroy();
Redoc.appRef = null;
});
it('should call componentRef.dispose', () => {
Redoc.dispose();
expect(disposeSpy).toHaveBeenCalled();
});
it('should create new host element', () => {
element.parentElement.removeChild(element);
Redoc.dispose();
expect(dom.query('redoc')).not.toBeNull();
dom.query('redoc').should.not.be.equal(element);
});
it('should set to null appRef', () => {
Redoc.dispose();
expect(Redoc.appRef).toBeNull();
});
});
});
/** Test component that contains a Redoc. */ /** Test component that contains a Redoc. */
@Component({selector: 'test-app'}) @Component({selector: 'test-app'})