mirror of
https://github.com/Redocly/redoc.git
synced 2024-11-10 19:06:34 +03:00
Redoc autoinit
This commit is contained in:
parent
5f34a700a3
commit
dda84eb118
|
@ -12,16 +12,12 @@
|
|||
</nav>
|
||||
</nav>
|
||||
|
||||
<redoc scroll-y-offset="body > nav">
|
||||
<redoc scroll-y-offset="body > nav" spec-url='swagger.json'>
|
||||
Loading...
|
||||
</redoc>
|
||||
|
||||
<!-- ReDoc built file with all dependencies included -->
|
||||
<script src="dist/redoc.full.js"> </script>
|
||||
<script src="main.js"> </script>
|
||||
<script>
|
||||
/* init redoc */
|
||||
Redoc.init('swagger.json', {scrollYOffset: 'body>nav'});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
@ -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');
|
||||
|
|
|
@ -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. */
|
||||
|
|
|
@ -7,3 +7,4 @@ export var init = Redoc.init;
|
|||
|
||||
window.Redoc = Redoc;
|
||||
enableProdMode();
|
||||
Redoc.autoInit();
|
||||
|
|
Loading…
Reference in New Issue
Block a user