import { html , fixture , expect } from '@open-wc/testing' ; import '../../../mock-wizard-editor.js' ; import { MockWizardEditor } from '../../../mock-wizard-editor.js' ; import { ListItem } from '@material/mwc-list/mwc-list-item' ; import { Select } from '@material/mwc-select' ; import { FilteredList } from '../../../../src/filtered-list.js' ; import { WizardTextField } from '../../../../src/wizard-textfield.js' ; import TemplatesPlugin from '../../../../src/editors/Templates.js' ; describe ( 'DAType wizards' , ( ) => { if ( customElements . get ( 'templates-editor' ) === undefined ) customElements . define ( 'templates-editor' , TemplatesPlugin ) ; let doc ; let parent ; let templates ; let dATypeList ; beforeEach ( async ( ) => { parent = await fixture ( html `<mock-wizard-editor
        ><templates-editor></templates-editor
      ></mock-wizard-editor>` ) ; templates = < TemplatesPlugin > parent . querySelector ( 'templates-editor' ) ! ; doc = await fetch ( '/test/testfiles/templates/datypes.scd' ) . then ( response => response . text ( ) ) . then ( str => new DOMParser ( ) . parseFromString ( str , 'application/xml' ) ) ; templates . doc = doc ; await templates . updateComplete ; dATypeList = < FilteredList > ( templates . shadowRoot ?. querySelector ( 'filtered-list[id="datypelist"]' ) ) ; } ) ; describe ( 'defines a createDATypeWizard' , ( ) => { let selector ; let idField ; let primayAction ; beforeEach ( async ( ) => { const button = < HTMLElement > ( templates ?. shadowRoot ?. querySelectorAll ( 'mwc-icon-button[icon="playlist_add"]' ) [ 2 ] ) ; button . click ( ) ; await parent . updateComplete ; await new Promise ( resolve => setTimeout ( resolve , 100 ) ) ; selector = < Select > ( parent . wizardUI . dialog ?. querySelector ( 'mwc-select[label="values"]' ) ) ; idField = < WizardTextField > ( parent . wizardUI . dialog ?. querySelector ( 'wizard-textfield[label="id"]' ) ) ; primayAction = < HTMLElement > ( parent . wizardUI . dialog ?. querySelector ( 'mwc-button[slot="primaryAction"]' ) ) ; } ) ; it ( 'looks like the latest snapshot' , async ( ) => { await expect ( parent . wizardUI . dialog ) . to . equalSnapshot ( ) ; } ) ; it ( 'allows to add empty DATypes to the project' , async ( ) => { expect ( doc . querySelector ( 'DAType[id="myGeneralDAType"]' ) ) . to . not . exist ; idField . maybeValue = 'myGeneralDAType' ; await parent . requestUpdate ( ) ; primayAction . click ( ) ; await parent . updateComplete ; expect ( doc . querySelector ( 'DAType[id="myGeneralDAType"]' ) ) . to . exist ; } ) ; it ( 'respects the sequence defined in the standard' , async ( ) => { idField . maybeValue = 'myGeneralDAType' ; await parent . requestUpdate ( ) ; primayAction . click ( ) ; await parent . updateComplete ; const element = doc . querySelector ( 'DAType[id="myGeneralDAType"]' ) ; expect ( element ?. nextElementSibling ?. tagName ) . to . equal ( 'DAType' ) ; expect ( element ?. previousElementSibling ?. tagName ) . to . equal ( 'DOType' ) ; } ) ; it ( 'recursively add missing! subsequent EnumType elements' , async ( ) => { expect ( doc . querySelector ( 'DAType[id="myOriginator"]' ) ) . to . not . exist ; selector . value = 'OpenSCD_Originator' ; idField . maybeValue = 'myOriginator' ; await parent . requestUpdate ( ) ; primayAction . click ( ) ; await parent . updateComplete ; expect ( doc . querySelector ( 'DAType[id="myOriginator"]' ) ) . to . exist ; expect ( doc . querySelector ( 'EnumType[id="OriginatorCategoryKind"]' ) ) . to . exist ; expect ( doc . querySelectorAll ( 'EnumType[id="OriginatorCategoryKind"]' ) . length ) . to . equal ( 1 ) ; } ) ; it ( 'recursively add missing! subsequent DAType elements' , async ( ) => { expect ( doc . querySelector ( 'DAType[id="OpenSCD_AnalogueValue_FLOAT32"]' ) ) . to . not . exist ; selector . value = 'OpenSCD_RangeConfig' ; idField . maybeValue = 'myOriginator' ; await parent . requestUpdate ( ) ; primayAction . click ( ) ; await parent . updateComplete ; expect ( doc . querySelector ( 'DAType[id="OpenSCD_AnalogueValue_FLOAT32"]' ) ) . to . exist ; expect ( doc . querySelectorAll ( 'DAType[id="OpenSCD_AnalogueValue_FLOAT32"]' ) . length ) . to . equal ( 1 ) ; } ) ; } ) ; describe ( 'defines a dATypeWizard' , ( ) => { let idField ; let primayAction ; let deleteButton ; beforeEach ( async ( ) => { ( < ListItem > ( dATypeList . querySelector ( 'mwc-list-item[value="#Dummy.LLN0.Mod.SBOw"]' ) ) ) . click ( ) ; await parent . requestUpdate ( ) ; await new Promise ( resolve => setTimeout ( resolve , 100 ) ) ; idField = < WizardTextField > ( parent . wizardUI . dialog ?. querySelector ( 'wizard-textfield[label="id"]' ) ) ; primayAction = < HTMLElement > ( parent . wizardUI . dialog ?. querySelector ( 'mwc-button[slot="primaryAction"]' ) ) ; deleteButton = < HTMLElement > ( parent . wizardUI . dialog ?. querySelector ( 'mwc-button[icon="delete"]' ) ) ; } ) ; it ( 'looks like the latest snapshot' , async ( ) => { await expect ( parent . wizardUI . dialog ) . to . equalSnapshot ( ) ; } ) ; it ( 'edits DAType attributes id' , async ( ) => { expect ( doc . querySelector ( 'DAType[id="Dummy.LLN0.Mod.SBOw"]' ) ) . to . exist ; idField . value = 'changedDAType' ; await parent . requestUpdate ( ) ; primayAction . click ( ) ; await parent . requestUpdate ( ) ; expect ( doc . querySelector ( 'DAType[id="Dummy.LLN0.Mod.SBOw"]' ) ) . to . not . exist ; expect ( doc . querySelector ( 'DAType[id="changedDAType"]' ) ) . to . exist ; } ) ; it ( 'deletes the DAType attribute on delete button click' , async ( ) => { expect ( doc . querySelector ( 'DAType[id="Dummy.LLN0.Mod.SBOw"]' ) ) . to . exist ; expect ( doc . querySelectorAll ( 'DAType' ) . length ) . to . equal ( 5 ) ; deleteButton . click ( ) ; await parent . requestUpdate ( ) ; expect ( doc . querySelector ( 'DAType[id="Dummy.LLN0.Mod.SBOw"]' ) ) . to . not . exist ; expect ( doc . querySelectorAll ( 'DAType' ) . length ) . to . equal ( 4 ) ; } ) ; it ( 'does not edit DAType element without changes' , async ( ) => { const originData = ( < Element > ( doc . querySelector ( 'DAType[id="Dummy.LLN0.Mod.SBOw"]' ) ) ) . cloneNode ( true ) ; primayAction . click ( ) ; await parent . requestUpdate ( ) ; expect ( originData . isEqualNode ( doc . querySelector ( 'DAType[id="Dummy.LLN0.Mod.SBOw"]' ) ) ) . to . be . true ; } ) ; } ) ; } ) ;