Skip to content

Commit bd114fb

Browse files
author
Dennis Labordus
committed
Fix for opening files and fixed some tests.
Signed-off-by: Dennis Labordus <[email protected]>
1 parent a836d6d commit bd114fb

13 files changed

+188
-218
lines changed

src/compas/CompasExistsIn.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,20 @@ export function CompasExistsIn<TBase extends LitElementConstructor>(
2626
protected updated(_changedProperties: PropertyValues): void {
2727
super.updated(_changedProperties);
2828

29-
if (
30-
_changedProperties.has('docId') ||
31-
_changedProperties.has('docName')
32-
) {
29+
if (_changedProperties.has('docId')) {
30+
this.existInCompas = undefined;
3331
this.checkExistInCompas();
3432
}
3533
}
3634

3735
callService(docType: string, docId: string) {
36+
// Use the versions call to check if any exist, because then the document also exists
37+
// And it saves bandwidth not to retrieve the whole document.
3838
return CompasSclDataService().listVersions(docType, docId);
3939
}
4040

4141
checkExistInCompas(): void {
42-
this.existInCompas = undefined;
43-
4442
if (this.docId) {
45-
// Use the versions call to check if any exist, because then the document also exists
46-
// And it saves bandwidth not to retrieve the whole document.
4743
const docType = getTypeFromDocName(this.docName);
4844
this.callService(docType, this.docId)
4945
.then(() => (this.existInCompas = true))

src/compas/CompasOpen.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,20 @@ import './CompasScl.js';
2323

2424
/* Event that will be used when a SCL Document is retrieved. */
2525
export interface DocRetrievedDetail {
26+
localFile: boolean;
2627
doc: Document;
2728
docName?: string;
2829
}
2930
export type DocRetrievedEvent = CustomEvent<DocRetrievedDetail>;
3031
export function newDocRetrievedEvent(
32+
localFile: boolean,
3133
doc: Document,
3234
docName?: string
3335
): DocRetrievedEvent {
3436
return new CustomEvent<DocRetrievedDetail>('docRetrieved', {
3537
bubbles: true,
3638
composed: true,
37-
detail: { doc, docName },
39+
detail: { localFile, doc, docName },
3840
});
3941
}
4042

@@ -51,7 +53,7 @@ export default class CompasOpenElement extends LitElement {
5153
.getSclDocument(this.selectedType ?? '', id ?? '')
5254
.catch(reason => createLogEvent(this, reason));
5355
if (sclDocument instanceof Document) {
54-
this.dispatchEvent(newDocRetrievedEvent(sclDocument));
56+
this.dispatchEvent(newDocRetrievedEvent(false, sclDocument));
5557
}
5658
}
5759

@@ -63,7 +65,7 @@ export default class CompasOpenElement extends LitElement {
6365
const docName = file.name;
6466
const doc = new DOMParser().parseFromString(text, 'application/xml');
6567

66-
this.dispatchEvent(newDocRetrievedEvent(doc, docName));
68+
this.dispatchEvent(newDocRetrievedEvent(true, doc, docName));
6769
this.sclFileUI.onchange = null;
6870
}
6971

src/compas/CompasSclTypeSelect.ts

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
SDS_NAMESPACE,
1818
} from '../compas-services/CompasSclDataService.js';
1919
import { Select } from '@material/mwc-select';
20+
import { repeat } from 'lit-html/directives/repeat';
2021

2122
@customElement('compas-scltype-select')
2223
export class CompasSclTypeSelect extends LitElement {
@@ -37,12 +38,15 @@ export class CompasSclTypeSelect extends LitElement {
3738
}
3839

3940
getSelectedValue(): string | null {
40-
return (<Select>this.shadowRoot!.querySelector('mwc-select'))?.value;
41+
return (
42+
(<Select>this.shadowRoot!.querySelector('mwc-select'))?.selected?.value ??
43+
null
44+
);
4145
}
4246

4347
valid(): boolean {
4448
const newValue = this.getSelectedValue();
45-
return newValue !== null && newValue !== undefined;
49+
return !!newValue;
4650
}
4751

4852
render(): TemplateResult {
@@ -60,20 +64,26 @@ export class CompasSclTypeSelect extends LitElement {
6064
naturalMenuWidth="true"
6165
label="${translate('compas.sclType')}"
6266
>
63-
${this.sclTypes.map(type => {
64-
const code =
67+
${repeat(
68+
this.sclTypes,
69+
type =>
6570
type.getElementsByTagNameNS(SDS_NAMESPACE, 'Code').item(0)!
66-
.textContent ?? '';
67-
const description =
68-
type.getElementsByTagNameNS(SDS_NAMESPACE, 'Description').item(0)!
69-
.textContent ?? '';
70-
return html`<mwc-list-item
71-
value="${code}"
72-
?selected="${code === this.value}"
73-
>
74-
<span>${description} (${code})</span>
75-
</mwc-list-item>`;
76-
})}
71+
.textContent ?? '',
72+
type => {
73+
const code =
74+
type.getElementsByTagNameNS(SDS_NAMESPACE, 'Code').item(0)!
75+
.textContent ?? '';
76+
const description =
77+
type.getElementsByTagNameNS(SDS_NAMESPACE, 'Description').item(0)!
78+
.textContent ?? '';
79+
return html`<mwc-list-item
80+
value="${code}"
81+
?selected="${code === this.value}"
82+
>
83+
<span>${description} (${code})</span>
84+
</mwc-list-item>`;
85+
}
86+
)}
7787
</mwc-select>`;
7888
}
7989

src/menu/CompasOpen.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import { html, LitElement } from 'lit-element';
22
import { get } from 'lit-translate';
33

4-
import { newPendingStateEvent, newWizardEvent, Wizard } from '../foundation.js';
4+
import {
5+
newLogEvent,
6+
newOpenDocEvent,
7+
newPendingStateEvent,
8+
newWizardEvent,
9+
Wizard,
10+
} from '../foundation.js';
511

612
import { DocRetrievedEvent } from '../compas/CompasOpen.js';
713
import { updateDocumentInOpenSCD } from '../compas/foundation.js';
@@ -14,8 +20,17 @@ export default class CompasOpenMenuPlugin extends LitElement {
1420
plugin: CompasOpenMenuPlugin,
1521
event: DocRetrievedEvent
1622
): Promise<void> {
17-
updateDocumentInOpenSCD(plugin, event.detail.doc, event.detail.docName);
18-
plugin.dispatchEvent(newWizardEvent());
23+
if (event.detail.localFile) {
24+
plugin.dispatchEvent(newLogEvent({ kind: 'reset' }));
25+
plugin.dispatchEvent(
26+
newOpenDocEvent(event.detail.doc, event.detail.docName!, {
27+
detail: { docId: undefined },
28+
})
29+
);
30+
} else {
31+
updateDocumentInOpenSCD(plugin, event.detail.doc, event.detail.docName);
32+
plugin.dispatchEvent(newWizardEvent());
33+
}
1934
}
2035

2136
return [

src/menu/CompasSave.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export default class CompasSaveMenuPlugin extends LitElement {
3030
compasSaveElement!: CompasSaveElement;
3131

3232
async run(): Promise<void> {
33+
await this.compasSaveElement.requestUpdate();
3334
this.dialog.open = true;
3435
}
3536

src/wizard-textfield.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ export class WizardTextField extends TextField {
122122
this.setCustomValidity(get('textfield.unique'));
123123
return false;
124124
}
125-
this.setCustomValidity(''); //Reset. Otherwise super.checkValidity always falseM
125+
// Reset to prevent super.checkValidity to always return false
126+
this.setCustomValidity('');
126127
return super.checkValidity();
127128
}
128129

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { expect, fixture, html } from '@open-wc/testing';
2-
import { WizardTextField } from '../../../src/wizard-textfield.js';
32

43
import { CompasCommentElement } from '../../../src/compas/CompasComment.js';
54
import '../../../src/compas/CompasComment.js';
@@ -10,31 +9,22 @@ describe('compas-comment', () => {
109
element = await fixture(html`<compas-comment></compas-comment>`);
1110
});
1211

13-
it('will always be valid', () => {
14-
// When nothing entered it will be valid.
12+
it('will be valid', () => {
13+
// When nothing entered it will also be valid.
1514
expect(element.valid()).to.be.true;
1615

17-
setValue('Some comments');
16+
element.value = 'Some comments';
1817
expect(element.valid()).to.be.true;
1918
});
2019

2120
it('will return entered value', () => {
2221
const value = 'Some comments';
23-
setValue(value);
22+
element.value = value;
2423

2524
expect(element.value).to.be.equal(value);
2625
});
2726

2827
it('looks like the latest snapshot', async () => {
2928
await expect(element).shadowDom.to.equalSnapshot();
3029
});
31-
32-
function setValue(value: string) {
33-
const item = <WizardTextField>(
34-
element
35-
.shadowRoot!.querySelectorAll('wizard-textfield[id="comment"]')
36-
.item(0)
37-
);
38-
item.value = value;
39-
}
4030
});

test/unit/compas/CompasExistsIn.test.ts

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1-
import {customElement, LitElement} from "lit-element";
2-
import {expect, fixtureSync, html, waitUntil} from "@open-wc/testing";
3-
import sinon, {SinonStub} from "sinon";
1+
import { expect, fixtureSync, html, waitUntil } from '@open-wc/testing';
2+
import sinon, { SinonStub } from 'sinon';
43

5-
import {NOT_FOUND_ERROR, SERVER_ERROR} from "../../../src/compas-services/foundation.js";
6-
import {CompasExistsIn, CompasExistsInElement} from "../../../src/compas/CompasExistsIn.js";
4+
import { customElement, LitElement } from 'lit-element';
5+
6+
import {
7+
NOT_FOUND_ERROR,
8+
SERVER_ERROR,
9+
} from '../../../src/compas-services/foundation.js';
10+
11+
import {
12+
CompasExistsIn,
13+
CompasExistsInElement,
14+
} from '../../../src/compas/CompasExistsIn.js';
715

816
@customElement('mock-compas-exists-in')
917
export class MockSetter extends CompasExistsIn(LitElement) {}
@@ -16,7 +24,8 @@ describe('CompasExistsInElement', () => {
1624
beforeEach(async () => {
1725
// Overwrite, because this scenario is the only on different.
1826
element = fixtureSync(
19-
html`<mock-compas-exists-in></mock-compas-exists-in>`);
27+
html`<mock-compas-exists-in></mock-compas-exists-in>`
28+
);
2029

2130
stubCallService = sinon.stub(element, 'callService').callsFake(() => {
2231
return Promise.reject();
@@ -35,10 +44,16 @@ describe('CompasExistsInElement', () => {
3544
describe('when service call returns a message', () => {
3645
beforeEach(async () => {
3746
element = fixtureSync(
38-
html`<mock-compas-exists-in docId="some-id" docName="some-scl.scd"></mock-compas-exists-in>`);
47+
html`<mock-compas-exists-in
48+
docId="some-id"
49+
docName="some-scl.scd"
50+
></mock-compas-exists-in>`
51+
);
3952

4053
stubCallService = sinon.stub(element, 'callService').callsFake(() => {
41-
const doc = <Document>document.implementation.createDocument("", "", null);
54+
const doc = <Document>(
55+
document.implementation.createDocument('', '', null)
56+
);
4257
return Promise.resolve(doc);
4358
});
4459

@@ -48,17 +63,21 @@ describe('CompasExistsInElement', () => {
4863

4964
it('the document exists', () => {
5065
expect(element.existInCompas).to.be.equal(true);
51-
sinon.assert.calledOnce(stubCallService)
66+
sinon.assert.calledTwice(stubCallService);
5267
});
5368
});
5469

5570
describe('when service call returns a not found error', () => {
5671
beforeEach(async () => {
5772
element = fixtureSync(
58-
html`<mock-compas-exists-in docId="some-id" docName="some-scl.scd"></mock-compas-exists-in>`);
73+
html`<mock-compas-exists-in
74+
docId="some-id"
75+
docName="some-scl.scd"
76+
></mock-compas-exists-in>`
77+
);
5978

6079
stubCallService = sinon.stub(element, 'callService').callsFake(() => {
61-
return Promise.reject({type: NOT_FOUND_ERROR});
80+
return Promise.reject({ type: NOT_FOUND_ERROR });
6281
});
6382

6483
await element;
@@ -67,25 +86,29 @@ describe('CompasExistsInElement', () => {
6786

6887
it('the document does not exists', () => {
6988
expect(element.existInCompas).to.be.equal(false);
70-
sinon.assert.calledOnce(stubCallService)
89+
sinon.assert.calledTwice(stubCallService);
7190
});
7291
});
7392

7493
describe('when service call returns a other error', () => {
7594
beforeEach(async () => {
7695
element = fixtureSync(
77-
html`<mock-compas-exists-in docId="some-id" docName="some-scl.scd"></mock-compas-exists-in>`);
96+
html`<mock-compas-exists-in
97+
docId="some-id"
98+
docName="some-scl.scd"
99+
></mock-compas-exists-in>`
100+
);
78101

79102
stubCallService = sinon.stub(element, 'callService').callsFake(() => {
80-
return Promise.reject({type: SERVER_ERROR});
103+
return Promise.reject({ type: SERVER_ERROR });
81104
});
82105

83106
await element;
84107
});
85108

86109
it('boolean stays undefined', () => {
87110
expect(element.existInCompas).to.be.undefined;
88-
sinon.assert.calledOnce(stubCallService)
111+
sinon.assert.calledTwice(stubCallService);
89112
});
90113
});
91114

0 commit comments

Comments
 (0)