From 5f34a700a3fa7a6509268f4b2ba377d9021ae4b1 Mon Sep 17 00:00:00 2001 From: Roman Hotsiy Date: Wed, 20 Jan 2016 16:37:23 +0200 Subject: [PATCH 1/4] Redoc dispose testcase --- lib/components/Redoc/redoc.js | 22 ++++++++----- lib/components/Redoc/redoc.spec.js | 53 +++++++++++++++++++++++++++++- 2 files changed, 66 insertions(+), 9 deletions(-) diff --git a/lib/components/Redoc/redoc.js b/lib/components/Redoc/redoc.js index cf50f815..6345d6e6 100644 --- a/lib/components/Redoc/redoc.js +++ b/lib/components/Redoc/redoc.js @@ -106,16 +106,22 @@ export default class Redoc extends BaseComponent { 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..211d1269 100644 --- a/lib/components/Redoc/redoc.spec.js +++ b/lib/components/Redoc/redoc.spec.js @@ -129,8 +129,59 @@ 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(); + }); + }); +}); /** Test component that contains a Redoc. */ @Component({selector: 'test-app'}) From dda84eb1180e1f7f9f73703a0e23889dec3559d3 Mon Sep 17 00:00:00 2001 From: Roman Hotsiy Date: Wed, 20 Jan 2016 17:05:43 +0200 Subject: [PATCH 2/4] Redoc autoinit --- demo/index.html | 6 +----- lib/components/Redoc/redoc.js | 11 +++++++++++ lib/components/Redoc/redoc.spec.js | 29 +++++++++++++++++++++++++++++ lib/index.js | 1 + 4 files changed, 42 insertions(+), 5 deletions(-) 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 6345d6e6..a0ccd4d5 100644 --- a/lib/components/Redoc/redoc.js +++ b/lib/components/Redoc/redoc.js @@ -103,6 +103,17 @@ 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'); diff --git a/lib/components/Redoc/redoc.spec.js b/lib/components/Redoc/redoc.spec.js index 211d1269..ea67adaa 100644 --- a/lib/components/Redoc/redoc.spec.js +++ b/lib/components/Redoc/redoc.spec.js @@ -181,6 +181,35 @@ describe('Redoc init', () => { 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. */ 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(); From 265762068e83171b426362f1b14633dcf02a4919 Mon Sep 17 00:00:00 2001 From: Roman Hotsiy Date: Wed, 20 Jan 2016 17:07:15 +0200 Subject: [PATCH 3/4] Add email to travis npm config --- .travis.yml | 1 + 1 file changed, 1 insertion(+) 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: From b6182ecbb824554686e5e4f32477b11bcdc70c47 Mon Sep 17 00:00:00 2001 From: Roman Hotsiy Date: Wed, 20 Jan 2016 17:07:56 +0200 Subject: [PATCH 4/4] v0.2.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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"