@@ -25,6 +25,7 @@ import {
25
25
cloneElement ,
26
26
depth ,
27
27
getUniqueElementName ,
28
+ newLnInstGenerator ,
28
29
} from '../../src/foundation.js' ;
29
30
30
31
import { MockAction } from './mock-actions.js' ;
@@ -604,4 +605,72 @@ describe('foundation', () => {
604
605
it ( 'returns Infinity if given a circularly defined object or array' , ( ) =>
605
606
expect ( depth ( circular ) ) . to . not . be . finite ) ;
606
607
} ) ;
608
+
609
+ describe ( 'generator function for new `lnInst` attribute' , ( ) => {
610
+ let lnInstGenerator : ( lnClass : string ) => string | undefined ;
611
+ let parent : Element ;
612
+
613
+ describe ( 'with existing unique lnInst' , ( ) => {
614
+ beforeEach ( ( ) => {
615
+ parent = new DOMParser ( ) . parseFromString (
616
+ `<Function name="someName">
617
+ <LNode name="None" lnClass="CSWI" lnInst="1"/>
618
+ <LNode name="None" lnClass="XCBR" lnInst="1"/>
619
+ <LNode name="None" lnClass="CILO" lnInst="1"/>
620
+ <LNode name="None" lnClass="CSWI" lnInst="2"/>
621
+ <LNode name="None" lnClass="PDIS" lnInst="1"/>
622
+ <LNode name="None" lnClass="CSWI" lnInst="5"/>
623
+ <LNode name="None" lnClass="CSWI" lnInst="6"/>
624
+ <LNode name="None" lnClass="CSWI" lnInst="8"/>
625
+ </Function>` ,
626
+ 'application/xml'
627
+ ) . documentElement ;
628
+
629
+ lnInstGenerator = newLnInstGenerator ( parent ) ;
630
+ } ) ;
631
+
632
+ it ( 'returns unique lnInst called once' , ( ) =>
633
+ expect ( lnInstGenerator ( 'CSWI' ) ) . to . equal ( '3' ) ) ;
634
+
635
+ it ( 'returns unique lnInst called several times' , ( ) => {
636
+ expect ( lnInstGenerator ( 'CSWI' ) ) . to . equal ( '3' ) ;
637
+ expect ( lnInstGenerator ( 'CSWI' ) ) . to . equal ( '4' ) ;
638
+ expect ( lnInstGenerator ( 'CSWI' ) ) . to . equal ( '7' ) ;
639
+ expect ( lnInstGenerator ( 'CSWI' ) ) . to . equal ( '9' ) ;
640
+ } ) ;
641
+
642
+ it ( 'returns unique lnInst called several times' , ( ) => {
643
+ expect ( lnInstGenerator ( 'CSWI' ) ) . to . equal ( '3' ) ;
644
+ expect ( lnInstGenerator ( 'CSWI' ) ) . to . equal ( '4' ) ;
645
+ expect ( lnInstGenerator ( 'CSWI' ) ) . to . equal ( '7' ) ;
646
+ expect ( lnInstGenerator ( 'CSWI' ) ) . to . equal ( '9' ) ;
647
+ } ) ;
648
+ } ) ;
649
+
650
+ describe ( 'with missing unique lnInst for lnClass PDIS' , ( ) => {
651
+ beforeEach ( ( ) => {
652
+ parent = new DOMParser ( ) . parseFromString (
653
+ `<Function name="someName">
654
+ </Function>` ,
655
+ 'application/xml'
656
+ ) . documentElement ;
657
+
658
+ for ( let i = 1 ; i <= 99 ; i ++ ) {
659
+ const lNode = new DOMParser ( ) . parseFromString (
660
+ `<LNode iedName="None" lnClass="PDIS" lnInst="${ i } " />` ,
661
+ 'application/xml'
662
+ ) . documentElement ;
663
+ parent . appendChild ( lNode ) ;
664
+ }
665
+
666
+ lnInstGenerator = newLnInstGenerator ( parent ) ;
667
+ } ) ;
668
+
669
+ it ( 'return undefined for the lnClass PDIS' , ( ) =>
670
+ expect ( lnInstGenerator ( 'PDIS' ) ) . to . be . undefined ) ;
671
+
672
+ it ( 'return unique lnInst for another lnClass' , ( ) =>
673
+ expect ( lnInstGenerator ( 'CSWI' ) ) . to . equal ( '1' ) ) ;
674
+ } ) ;
675
+ } ) ;
607
676
} ) ;
0 commit comments