Skip to content

Commit

Permalink
chore: Fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
clepski committed Aug 5, 2024
1 parent b9feea2 commit af226c2
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 51 deletions.
57 changes: 57 additions & 0 deletions packages/compas-open-scd/test/mock-compas-open-scd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import {
customElement,
TemplateResult,
html,
queryAssignedNodes,
query,
} from 'lit-element';
import { OscdWizards } from '@openscd/open-scd/src/addons/Wizards.js';
import { WizardFactory } from '@openscd/open-scd/src/foundation.js';
import { OpenSCD } from '../src/open-scd.js';
import { WizardDialog } from '@openscd/open-scd/src/wizard-dialog.js';

import { CompasHistory } from '../src/addons/CompasHistory.js';
import { CompasLayout } from '../src/addons/CompasLayout.js';

@customElement('mock-compas-open-scd')
export class MockCompasOpenSCD extends OpenSCD {
@queryAssignedNodes()
_plugins!: Array<HTMLElement>;

@query('oscd-wizards')
wizards!: OscdWizards;

@query('compas-history')
historyAddon!: CompasHistory;

@query('compas-layout')
layout!: CompasLayout;

renderHosting(): TemplateResult {
return html`<slot></slot>`;
}

render(): TemplateResult {
return html`
${this.renderHosting()}
${super.render()}`;
}

getPlugin<T extends HTMLElement>(name: string): T | undefined {
return this._plugins.find(
p => p.tagName.toLowerCase() === name.toLowerCase()
) as T | undefined;
}

getActivePlugin<T extends HTMLElement>(): T {
return this._plugins[0]! as T;
}

get wizardUI(): WizardDialog {
return this.wizards.wizardUI;
}

get workflow(): WizardFactory[] {
return this.wizards.workflow;
}
}
82 changes: 40 additions & 42 deletions packages/compas-open-scd/test/unit/CompasPlugging.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { expect, fixture, html } from '@open-wc/testing';

import './mock-compas-plugger.js';
import { MockCompasPlugger } from './mock-compas-plugger.js';
'../mock-compas-open-scd.js';
import { MockCompasOpenSCD } from '../mock-compas-open-scd.js';

import { TextField } from '@material/mwc-textfield';

describe('PluggingElement', () => {
let element: MockCompasPlugger;
describe('CompasOpenSCD-Plugin', () => {
let element: MockCompasOpenSCD;
let doc: XMLDocument;
const docName = 'testDoc';

afterEach(async () => {
await new Promise(resolve => setTimeout(resolve, 50)); // await animation
Expand All @@ -17,14 +17,11 @@ describe('PluggingElement', () => {
doc = await fetch('/test/testfiles/valid2007B4.scd')
.then(response => response.text())
.then(str => new DOMParser().parseFromString(str, 'application/xml'));
element = <MockCompasPlugger>(
await fixture(
html`<mock-compas-plugger
.doc=${doc}
docName="testDoc"
></mock-compas-plugger>`
)
);
element = <MockCompasOpenSCD>(
await fixture(
html`<mock-compas-open-scd .doc=${doc} .docName=${docName}></mock-compas-open-scd>`
)
);
await element.updateComplete;
});

Expand All @@ -37,19 +34,19 @@ describe('PluggingElement', () => {
let primaryAction: HTMLElement;

beforeEach(async () => {
element.pluginUI.show();
await element.pluginUI.updateComplete;
element.layout.pluginUI.show();
await element.layout.pluginUI.updateComplete;
firstEditorPlugin = <HTMLElement>(
element.pluginList.querySelector(
element.layout.pluginList.querySelector(
'mwc-check-list-item:not([noninteractive])'
)
);

resetAction = <HTMLElement>(
element.pluginUI.querySelector('mwc-button[slot="secondaryAction"]')
element.layout.pluginUI.querySelector('mwc-button[slot="secondaryAction"]')
);
primaryAction = <HTMLElement>(
element.pluginUI.querySelector('mwc-button[slot="primaryAction"]')
element.layout.pluginUI.querySelector('mwc-button[slot="primaryAction"]')
);
});

Expand All @@ -60,25 +57,25 @@ describe('PluggingElement', () => {
});

it('enables selected plugins', async () => {
(<HTMLElement>element.pluginList.firstElementChild).click();
(<HTMLElement>element.layout.pluginList.firstElementChild).click();
await element.updateComplete;
(<HTMLElement>element.pluginList.firstElementChild).click();
(<HTMLElement>element.layout.pluginList.firstElementChild).click();
await element.updateComplete;
expect(element).property('editors').to.have.lengthOf(9);
expect(element.layout).property('editors').to.have.lengthOf(9);
});

it('resets plugins to default on reset button click', async () => {
(<HTMLElement>element.pluginList.firstElementChild).click();
(<HTMLElement>element.layout.pluginList.firstElementChild).click();
await element.updateComplete;
resetAction.click();
await element.updateComplete;
expect(element).property('editors').to.have.lengthOf(9);
expect(element.layout).property('editors').to.have.lengthOf(9);
});

it('opens the custom plugin dialog on add button click', async () => {
primaryAction.click();
await element.updateComplete;
expect(element)
expect(element.layout)
.property('pluginDownloadUI')
.to.have.property('open', true);
});
Expand All @@ -93,50 +90,51 @@ describe('PluggingElement', () => {

beforeEach(async () => {
src = <TextField>(
element.pluginDownloadUI.querySelector('#pluginSrcInput')
element.layout.pluginDownloadUI.querySelector('#pluginSrcInput')
);
name = <TextField>(
element.pluginDownloadUI.querySelector('#pluginNameInput')
element.layout.pluginDownloadUI.querySelector('#pluginNameInput')
);
primaryAction = <HTMLElement>(
element.pluginDownloadUI.querySelector(
element.layout.pluginDownloadUI.querySelector(
'mwc-button[slot="primaryAction"]'
)
);
element.pluginDownloadUI.show();
await element.pluginDownloadUI.updateComplete;
element.layout.pluginDownloadUI.show();
await element.layout.pluginDownloadUI.updateComplete;
await element.updateComplete;
menuKindOption = <HTMLElement>(
element.pluginDownloadUI.querySelector(
element.layout.pluginDownloadUI.querySelector(
'#pluginKindList > mwc-radio-list-item[id="menu"]'
)
);
validatorKindOption = <HTMLElement>(
element.pluginDownloadUI.querySelector(
element.layout.pluginDownloadUI.querySelector(
'#pluginKindList > mwc-radio-list-item[id="validator"]'
)
);
});

it('requires a name and a valid URL to add a plugin', async () => {
primaryAction.click();
expect(element.pluginDownloadUI).to.have.property('open', true);
expect(element.layout.pluginDownloadUI).to.have.property('open', true);

src.value = 'http://example.com/plugin.js';
await src.updateComplete;
primaryAction.click();
expect(element.pluginDownloadUI).to.have.property('open', true);
expect(element.layout.pluginDownloadUI).to.have.property('open', true);

src.value = 'notaURL';
name.value = 'testName';
await src.updateComplete;
await name.updateComplete;
primaryAction.click();
expect(element.pluginDownloadUI).to.have.property('open', true);
expect(element.layout.pluginDownloadUI).to.have.property('open', true);

src.value = 'http://example.com/plugin.js';
await src.updateComplete;
primaryAction.click();
expect(element.pluginDownloadUI).to.have.property('open', false);
expect(element.layout.pluginDownloadUI).to.have.property('open', false);
});

it('adds a new editor kind plugin on add button click', async () => {
Expand All @@ -146,18 +144,18 @@ describe('PluggingElement', () => {
await name.updateComplete;
primaryAction.click();
await element.updateComplete;
expect(element.editors).to.have.lengthOf(10);
expect(element.layout.editors).to.have.lengthOf(10);
});
it('adds a new menu kind plugin on add button click', async () => {
const lengthMenuKindPlugins = element.menuEntries.length;
const lengthMenuKindPlugins = element.layout.menuEntries.length;
src.value = 'http://example.com/plugin.js';
name.value = 'testName';
menuKindOption.click();
await src.updateComplete;
await name.updateComplete;
primaryAction.click();
await element.updateComplete;
expect(element.menuEntries).to.have.lengthOf(lengthMenuKindPlugins + 1);
expect(element.layout.menuEntries).to.have.lengthOf(lengthMenuKindPlugins + 1);
});
it('sets requireDoc and position for new menu kind plugin', async () => {
src.value = 'http://example.com/plugin.js';
Expand All @@ -169,22 +167,22 @@ describe('PluggingElement', () => {
await element.updateComplete;

expect(
element.menuEntries[element.menuEntries.length - 1]
element.layout.menuEntries[element.layout.menuEntries.length - 1]
).to.have.property('requireDoc');
expect(
element.menuEntries[element.menuEntries.length - 1]
element.layout.menuEntries[element.layout.menuEntries.length - 1]
).to.have.property('position');
});
it('adds a new validator kind plugin on add button click', async () => {
expect(element.validators).to.have.lengthOf(2);
expect(element.layout.validators).to.have.lengthOf(2);
src.value = 'http://example.com/plugin.js';
name.value = 'testName';
validatorKindOption.click();
await src.updateComplete;
await name.updateComplete;
primaryAction.click();
await element.updateComplete;
expect(element.validators).to.have.lengthOf(3);
expect(element.layout.validators).to.have.lengthOf(3);
});
});
});
9 changes: 0 additions & 9 deletions packages/compas-open-scd/test/unit/mock-compas-plugger.ts

This file was deleted.

0 comments on commit af226c2

Please sign in to comment.