Skip to content

Commit 60c27e8

Browse files
author
Rob Tjalma
authored
Merge pull request #60 from com-pas/test-coverage
Small improvements in versions plugin.
2 parents f4a65a5 + 1a3d259 commit 60c27e8

File tree

4 files changed

+48
-19
lines changed

4 files changed

+48
-19
lines changed

__snapshots__/compas-versions-plugin.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@
8282
>
8383
demo_station1 (2.0.0)
8484
</mwc-list-item>
85+
<mwc-list-item
86+
aria-disabled="false"
87+
mwc-list-item=""
88+
tabindex="-1"
89+
>
90+
3b572a56-51cc-479b-97fd-e404ebf9ae67 (2.1.0)
91+
</mwc-list-item>
8592
</mwc-list>
8693
</section>
8794
</div>

src/compas-editors/CompasVersions.ts

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ export default class CompasVersionsPlugin extends LitElement {
3636
this.dispatchEvent(newWizardEvent(confirmRestoreCompasWizard(this.docName, this.docId, version)))
3737
}
3838

39+
private getElementbyName(parent: Element, namespace: string, tagName: string): Element | null {
40+
const elements = parent.getElementsByTagNameNS(namespace, tagName);
41+
if (elements.length > 0) {
42+
return elements.item(0);
43+
}
44+
return null;
45+
}
46+
3947
render(): TemplateResult {
4048
if (!this.scls) {
4149
return html `
@@ -58,11 +66,12 @@ export default class CompasVersionsPlugin extends LitElement {
5866
<h1>${translate('compas.versions.title')}</h1>
5967
<mwc-list>
6068
${this.scls.map( item => {
61-
let name = item.getElementsByTagNameNS(SDS_NAMESPACE, "Name").item(0)!.textContent ?? '';
62-
if (name === '') {
63-
name = item.getElementsByTagNameNS(SDS_NAMESPACE, "Id").item(0)!.textContent ?? '';
69+
let element = this.getElementbyName(item, SDS_NAMESPACE, "Name");
70+
if (element === null) {
71+
element = this.getElementbyName(item, SDS_NAMESPACE, "Id");
6472
}
65-
const version = item.getElementsByTagNameNS(SDS_NAMESPACE, "Version").item(0)!.textContent ?? '';
73+
const name = element!.textContent ?? '';
74+
const version = this.getElementbyName(item, SDS_NAMESPACE, "Version")!.textContent ?? '';
6675
return html`<mwc-list-item tabindex="0"
6776
@click=${() => {
6877
this.confirmRestoreCompas(version);
@@ -97,18 +106,22 @@ export default class CompasVersionsPlugin extends LitElement {
97106
`;
98107
}
99108

109+
function fetchScl(type: string, docId: string, version: string) {
110+
CompasSclDataService().getSclDocumentVersion(type, docId, version)
111+
.then(response => {
112+
// Copy the SCL Result from the Response and create a new Document from it.
113+
const sclElement = response.querySelectorAll("SCL").item(0);
114+
const sclDocument = document.implementation.createDocument("", "", null);
115+
sclDocument.getRootNode().appendChild(sclElement.cloneNode(true));
116+
117+
updateDocumentInOpenSCD(sclDocument);
118+
});
119+
}
120+
100121
function openScl(docName: string, docId: string, version: string) {
101122
return function () {
102123
const type = getTypeFromDocName(docName);
103-
CompasSclDataService().getSclDocumentVersion(type, docId, version)
104-
.then(response => {
105-
// Copy the SCL Result from the Response and create a new Document from it.
106-
const sclElement = response.querySelectorAll("SCL").item(0);
107-
const sclDocument = document.implementation.createDocument("", "", null);
108-
sclDocument.getRootNode().appendChild(sclElement.cloneNode(true));
109-
110-
updateDocumentInOpenSCD(sclDocument);
111-
});
124+
fetchScl(type, docId, version);
112125

113126
// Close the Restore Dialog.
114127
getOpenScdElement().dispatchEvent(newWizardEvent());

test/integration/compas-editors/CompasVersions.test.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import sinon, {SinonStub} from "sinon";
33

44
import {Editing} from '../../../src/Editing.js';
55
import {Wizarding} from '../../../src/Wizarding.js';
6+
67
import {
78
BASIC_VERSIONS_LIST_RESPONSE,
89
stubFetchResponseFunction,
@@ -23,7 +24,8 @@ describe('compas-versions-plugin', () => {
2324

2425
describe('no-compas-document', () => {
2526
beforeEach(async () => {
26-
element = fixtureSync(html`<compas-versions-plugin></compas-versions-plugin>`);
27+
element = fixtureSync(html`
28+
<compas-versions-plugin></compas-versions-plugin>`);
2729

2830
stub = stubFetchResponseFunction(element, FETCH_FUNCTION, undefined, VERSION_ENTRY_ELEMENT_NAME,
2931
() => {
@@ -47,7 +49,8 @@ describe('compas-versions-plugin', () => {
4749

4850
describe('show-loading', () => {
4951
beforeEach(async () => {
50-
element = fixtureSync(html`<compas-versions-plugin></compas-versions-plugin>`);
52+
element = fixtureSync(html`
53+
<compas-versions-plugin></compas-versions-plugin>`);
5154
element.docId = docId;
5255

5356
stub = stubFetchResponseFunction(element, FETCH_FUNCTION, undefined, VERSION_ENTRY_ELEMENT_NAME,
@@ -70,7 +73,8 @@ describe('compas-versions-plugin', () => {
7073

7174
describe('no-items-in-list', () => {
7275
beforeEach(async () => {
73-
element = fixtureSync(html`<compas-versions-plugin></compas-versions-plugin>`);
76+
element = fixtureSync(html`
77+
<compas-versions-plugin></compas-versions-plugin>`);
7478
element.docId = docId;
7579

7680
stub = stubFetchResponseFunction(element, FETCH_FUNCTION, undefined, VERSION_ENTRY_ELEMENT_NAME,
@@ -95,7 +99,8 @@ describe('compas-versions-plugin', () => {
9599

96100
describe('items-in-list', () => {
97101
beforeEach(async () => {
98-
element = fixtureSync(html`<compas-versions-plugin></compas-versions-plugin>`);
102+
element = fixtureSync(html`
103+
<compas-versions-plugin></compas-versions-plugin>`);
99104
element.docId = docId;
100105

101106
stub = stubFetchResponseFunction(element, FETCH_FUNCTION, BASIC_VERSIONS_LIST_RESPONSE, VERSION_ENTRY_ELEMENT_NAME,
@@ -111,9 +116,9 @@ describe('compas-versions-plugin', () => {
111116
sinon.restore();
112117
});
113118

114-
it('has 2 item entries', () => {
119+
it('has 3 item entries', () => {
115120
expect(element.shadowRoot!.querySelectorAll('mwc-list > mwc-list-item'))
116-
.to.have.length(2)
121+
.to.have.length(3)
117122
});
118123

119124
it('looks like the latest snapshot', async () => {

test/unit/compas/CompasSclDataServiceResponses.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ export const BASIC_VERSIONS_LIST_RESPONSE = `
4242
<Name>demo_station1</Name>
4343
<Version>2.0.0</Version>
4444
</Item>
45+
<Item>
46+
<Id>3b572a56-51cc-479b-97fd-e404ebf9ae67</Id>
47+
<Version>2.1.0</Version>
48+
</Item>
4549
</ListResponse>`;
4650

4751
export function stubFetchResponseFunction(element: any,

0 commit comments

Comments
 (0)