Redoc autoinit

This commit is contained in:
Roman Hotsiy 2016-01-20 17:05:43 +02:00
parent 5f34a700a3
commit dda84eb118
4 changed files with 42 additions and 5 deletions

View File

@ -12,16 +12,12 @@
</nav> </nav>
</nav> </nav>
<redoc scroll-y-offset="body > nav"> <redoc scroll-y-offset="body > nav" spec-url='swagger.json'>
Loading... Loading...
</redoc> </redoc>
<!-- ReDoc built file with all dependencies included --> <!-- ReDoc built file with all dependencies included -->
<script src="dist/redoc.full.js"> </script> <script src="dist/redoc.full.js"> </script>
<script src="main.js"> </script> <script src="main.js"> </script>
<script>
/* init redoc */
Redoc.init('swagger.json', {scrollYOffset: 'body>nav'});
</script>
</body> </body>
</html> </html>

View File

@ -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() { static dispose() {
let dom = new BrowserDomAdapter(); let dom = new BrowserDomAdapter();
let el = dom.query('redoc'); let el = dom.query('redoc');

View File

@ -181,6 +181,35 @@ describe('Redoc init', () => {
expect(Redoc.appRef).toBeNull(); 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. */ /** Test component that contains a Redoc. */

View File

@ -7,3 +7,4 @@ export var init = Redoc.init;
window.Redoc = Redoc; window.Redoc = Redoc;
enableProdMode(); enableProdMode();
Redoc.autoInit();