Skip to content

Commit a06b1d8

Browse files
committed
feat(server): maintain selection as part of inner state
1 parent d8fe68a commit a06b1d8

File tree

2 files changed

+56
-13
lines changed

2 files changed

+56
-13
lines changed

src/ApiConsole.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,9 @@ export class ApiConsole extends AmfHelperMixin(LitElement) {
248248
redirectUri,
249249
eventsTarget,
250250
baseUri,
251-
noDocs
251+
noDocs,
252+
selectedServerValue,
253+
selectedServerType,
252254
} = this;
253255
return html`<api-request-panel
254256
.amf="${amf}"
@@ -264,7 +266,10 @@ export class ApiConsole extends AmfHelperMixin(LitElement) {
264266
.allowHideOptional="${allowHideOptional}"
265267
.baseUri="${baseUri}"
266268
.noDocs="${noDocs}"
267-
.eventsTarget="${eventsTarget}">
269+
.selectedServerValue="${selectedServerValue}"
270+
.selectedServerType="${selectedServerType}"
271+
.eventsTarget="${eventsTarget}"
272+
>
268273
<slot name="custom-base-uri" slot="custom-base-uri"></slot>
269274
</api-request-panel>`;
270275
}
@@ -281,7 +286,9 @@ export class ApiConsole extends AmfHelperMixin(LitElement) {
281286
narrow,
282287
scrollTarget,
283288
redirectUri,
284-
baseUri
289+
baseUri,
290+
selectedServerValue,
291+
selectedServerType,
285292
} = this;
286293
return html`<api-documentation
287294
.amf="${amf}"
@@ -296,6 +303,8 @@ export class ApiConsole extends AmfHelperMixin(LitElement) {
296303
.redirectUri="${redirectUri}"
297304
.scrollTarget="${scrollTarget}"
298305
@api-navigation-selection-changed="${this._apiNavigationOcurred}"
306+
.selectedServerValue="${selectedServerValue}"
307+
.selectedServerType="${selectedServerType}"
299308
></api-documentation>`;
300309
}
301310

@@ -551,6 +560,8 @@ export class ApiConsole extends AmfHelperMixin(LitElement) {
551560
* **This is an experimental option and may dissapear without warning.**
552561
*/
553562
rearrangeEndpoints: { type: Boolean },
563+
selectedServerValue: { type: String },
564+
selectedServerType: { type: String },
554565
};
555566
}
556567

@@ -630,6 +641,7 @@ export class ApiConsole extends AmfHelperMixin(LitElement) {
630641
constructor() {
631642
super();
632643
this._tryitHandler = this._tryitHandler.bind(this);
644+
this._handleServerChange = this._handleServerChange.bind(this);
633645

634646
this.page = 'docs';
635647
this.drawerAlign = 'left';
@@ -641,6 +653,7 @@ export class ApiConsole extends AmfHelperMixin(LitElement) {
641653
super.connectedCallback();
642654
}
643655
this.addEventListener('tryit-requested', this._tryitHandler);
656+
this.addEventListener('api-server-changed', this._handleServerChange);
644657
this._notifyApicExtension();
645658
if (window.ShadyCSS) {
646659
window.ShadyCSS.styleElement(this);
@@ -653,6 +666,7 @@ export class ApiConsole extends AmfHelperMixin(LitElement) {
653666
super.disconnectedCallback();
654667
}
655668
this.removeEventListener('tryit-requested', this._tryitHandler);
669+
this.removeEventListener('api-server-changed', this._handleServerChange);
656670
}
657671

658672
firstUpdated() {
@@ -861,4 +875,10 @@ export class ApiConsole extends AmfHelperMixin(LitElement) {
861875
this._extensionBannerActive = true;
862876
}
863877
}
878+
879+
_handleServerChange(e) {
880+
const { selectedValue, selectedType } = e.detail;
881+
this.selectedServerValue = selectedValue;
882+
this.selectedServerType = selectedType;
883+
}
864884
}

test/api-console.basic.test.js

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { fixture, assert, aTimeout, html } from '@open-wc/testing';
1+
import { fixture, assert, aTimeout, html, nextFrame } from '@open-wc/testing';
22
import * as sinon from 'sinon/pkg/sinon-esm.js';
33
import * as MockInteractions from '@polymer/iron-test-helpers/mock-interactions.js';
44
import { isChrome } from '../src/ApiConsole.js';
@@ -8,7 +8,7 @@ import '../api-console.js';
88
// Tests for computations that do not require AMF model.
99
//
1010

11-
describe('<api-console>', function() {
11+
describe('<api-console>', function () {
1212
async function basicFixture() {
1313
return (await fixture(`<api-console></api-console>`));
1414
}
@@ -63,7 +63,7 @@ describe('<api-console>', function() {
6363
let requests;
6464
before(() => {
6565
xhr = sinon.useFakeXMLHttpRequest();
66-
xhr.onCreate = function(xhr) {
66+
xhr.onCreate = function (xhr) {
6767
requests.push(xhr);
6868
};
6969
});
@@ -86,7 +86,8 @@ describe('<api-console>', function() {
8686
element.modelLocation = 'apip.json';
8787
assert.equal(requests.length, 1);
8888
requests[0].respond(200, {
89-
'Content-Type': 'application/json' },
89+
'Content-Type': 'application/json'
90+
},
9091
'[{"@context":{}, "@id": "","@type": []}]');
9192
assert.typeOf(element.amf, 'array');
9293
});
@@ -100,7 +101,8 @@ describe('<api-console>', function() {
100101
const callback = sinon.spy(element, '_apiLoadErrorHandler');
101102
element.modelLocation = 'error.json';
102103
requests[0].respond(404, {
103-
'Content-Type': 'text/plain' },
104+
'Content-Type': 'text/plain'
105+
},
104106
'nothing');
105107
assert.isTrue(callback.called);
106108
});
@@ -230,7 +232,7 @@ describe('<api-console>', function() {
230232
});
231233
});
232234

233-
describe('Attribution', function() {
235+
describe('Attribution', function () {
234236
it('Attribution logo is rendered', async () => {
235237
const element = await basicFixture();
236238
const node = element.shadowRoot.querySelector('.powered-by');
@@ -327,10 +329,10 @@ describe('<api-console>', function() {
327329
function basicFixture(loc) {
328330
return new Promise((resolve, reject) => {
329331
fixture(html`<api-console modelLocation="${loc}"></api-console>`)
330-
.then((element) => {
331-
element.addEventListener('model-load-success', () => resolve(element));
332-
element.addEventListener('model-load-error', () => reject(element));
333-
})
332+
.then((element) => {
333+
element.addEventListener('model-load-success', () => resolve(element));
334+
element.addEventListener('model-load-error', () => reject(element));
335+
})
334336
});
335337
}
336338

@@ -464,4 +466,25 @@ describe('<api-console>', function() {
464466
});
465467
});
466468
});
469+
470+
describe('Server selection change', () => {
471+
let element;
472+
473+
beforeEach(async () => {
474+
element = await basicFixture();
475+
});
476+
477+
it('Should update selectedServerValue and selectedServerType', async () => {
478+
const e = new CustomEvent('api-server-changed', {
479+
detail: {
480+
selectedValue: 'https://example.org',
481+
selectedType: 'custom',
482+
}
483+
});
484+
element.dispatchEvent(e);
485+
await nextFrame();
486+
assert.equal(element.selectedServerValue, 'https://example.org');
487+
assert.equal(element.selectedServerType, 'custom');
488+
});
489+
});
467490
});

0 commit comments

Comments
 (0)