diff --git a/.travis.yml b/.travis.yml index 0c1c5103..d419783a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -44,6 +44,7 @@ deploy: branch: master - provider: npm skip_cleanup: true + email: gotsijroman@gmail.com api_key: secure: PuhWLERrCEFmXmdFpw2OVFlqpOIVDmgwk5JUJOYaFdVCh/smp0+jZCQ4vrdFpuG96rnDVirD+A8xvW6NgsNNaRthLgOB/LRdFN69rU6Gvn3At6wlnC55t5dlhxPvCfnzJcHVBLXX4EmMkjnZqDg2uczXTzPodr3FnQJNuXmP8B33fzDVLyHccvXZ90abwXWVrgRIXPU28niqCR8DOC2OTzs7wqz+BLNkYDRRbyYXsg62HWuD33x5iof5IqBmhzBt3usCGmF3QGcgHrXHdZw3sZnit8+Bua++3KrXR0x6HGXXN1AoXVmCAkCa5OTQ5R3tCRxiJN3P2KLnvWeZR74sTFkovJB/6pGCvbJ/c7Wnuw6sD7SgOUBD359ULB6lAf5OnxBLoNebX4JxxVXF+zA4E3Bl44VxkzDpPWc15xqBPMB5vBREzMVmJ5mExn2s5cmLQjADbl9h0y6gZnhnNJ+iTmqtrVyM0ZkF2rPrzrTdGD+ULmRIlTMkdD1bh+/TJ3RdXT3P4/zNUJmiNnvgnnJVYYvsGaXWF+7uCVHT/8k2RsoSHqgkqh0gkDqGSwVix55y5mC7T2Vk9lMBhm6MvFJXaonOX0kxJS4EDQ3plPd6/ybG+TLhwggYnQ8o9msU5Nt6FpUShKiezjKurIhbQZdwlVivX3tahjW2QjNDO58xGgY= on: diff --git a/demo/index.html b/demo/index.html index b863a23c..89d6f4d2 100644 --- a/demo/index.html +++ b/demo/index.html @@ -12,16 +12,12 @@ - + Loading... - diff --git a/lib/components/Redoc/redoc.js b/lib/components/Redoc/redoc.js index cf50f815..a0ccd4d5 100644 --- a/lib/components/Redoc/redoc.js +++ b/lib/components/Redoc/redoc.js @@ -103,19 +103,36 @@ export default class Redoc extends BaseComponent { ); } + static autoInit() { + const specUrlAttributeName = 'spec-url'; + let dom = new BrowserDomAdapter(); + let redocEl = dom.query('redoc'); + if (!redocEl) return; + if (dom.hasAttribute(redocEl, specUrlAttributeName)) { + let url = dom.getAttribute(redocEl, specUrlAttributeName); + Redoc.init(url); + } + } + static dispose() { let dom = new BrowserDomAdapter(); let el = dom.query('redoc'); - let parent = el.parentElement; - let nextSibling = el.nextElementSibling; + let parent; + let nextSibling; + if (el) { + parent = el.parentElement; + nextSibling = el.nextElementSibling; + } - Redoc.appRef && Redoc.appRef.dispose(); - Redoc.appRef = null; + if (Redoc.appRef) { + Redoc.appRef.dispose(); + Redoc.appRef = null; - // Redoc dispose removes host element, so need to restore it - el = dom.createElement('redoc'); - el.innerText = 'Loading...'; - parent.insertBefore(el, nextSibling); + // Redoc dispose removes host element, so need to restore it + el = dom.createElement('redoc'); + el.innerText = 'Loading...'; + parent && parent.insertBefore(el, nextSibling); + } } } Redoc.parameters = Redoc.parameters.concat([[OptionsManager], [ElementRef], [BrowserDomAdapter]]); diff --git a/lib/components/Redoc/redoc.spec.js b/lib/components/Redoc/redoc.spec.js index a9642299..ea67adaa 100644 --- a/lib/components/Redoc/redoc.spec.js +++ b/lib/components/Redoc/redoc.spec.js @@ -129,8 +129,88 @@ describe('Redoc init', () => { 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(); + }); + }); + + describe('Redoc autoInit', () => { + const testURL = 'testurl'; + let dom = new BrowserDomAdapter(); + let elem; + beforeEach(() => { + spyOn(Redoc, 'init').and.stub(); + elem = dom.createElement('redoc'); + dom.defaultDoc().body.appendChild(elem); + dom.setAttribute(elem, 'spec-url', testURL); + }); + + it('should call Redoc.init with url from param spec-url', () => { + Redoc.autoInit(); + expect(Redoc.init).toHaveBeenCalled(); + expect(Redoc.init.calls.argsFor(0)).toEqual([testURL]); + }); + + it('should not call Redoc.init when spec-url param is not provided', () => { + dom.removeAttribute(elem, 'spec-url'); + Redoc.autoInit(); + expect(Redoc.init).not.toHaveBeenCalled(); + }); + + afterEach(() => { + Redoc.init.and.callThrough(); + dom.defaultDoc().body.removeChild(elem); + }); + }); +}); /** Test component that contains a Redoc. */ @Component({selector: 'test-app'}) diff --git a/lib/index.js b/lib/index.js index 5e8735fa..70104b76 100644 --- a/lib/index.js +++ b/lib/index.js @@ -7,3 +7,4 @@ export var init = Redoc.init; window.Redoc = Redoc; enableProdMode(); +Redoc.autoInit(); diff --git a/package.json b/package.json index ce96a48a..8e4baf1e 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "redoc", "description": "Swagger-generated API Reference Documentation", - "version": "0.1.0", + "version": "0.2.0", "repository": { "type": "git", "url": "git://github.com/Rebilly/ReDoc"