1
+ import { expect } from 'chai' ;
2
+ import { ElementService } from '../../src/application/ElementService.js' ;
3
+ import { MockElement } from './MockElement.js' ; // A mock element class for testing
4
+ import { Position } from '../../src/domain/valueObjects/Position.js' ;
5
+ import { Properties } from '../../src/domain/valueObjects/Properties.js' ;
6
+ import { Label } from '../../src/domain/valueObjects/Label.js' ;
7
+
8
+ describe ( 'Element Service Tests' , ( ) => {
9
+ describe ( 'Element Creation' , ( ) => {
10
+ it ( 'should create an element successfully' , ( ) => {
11
+ const terminals = [ new Position ( 10 , 20 ) , new Position ( 30 , 40 ) ] ;
12
+ const properties = new Properties ( { resistance : 100 } ) ;
13
+ const element = ElementService . createElement ( MockElement , 'E1' , terminals , [ null , properties ] ) ;
14
+
15
+ expect ( element ) . to . be . instanceOf ( MockElement ) ;
16
+ expect ( element . id ) . to . equal ( 'E1' ) ;
17
+ expect ( element . terminals ) . to . deep . equal ( terminals ) ;
18
+ expect ( element . properties ) . to . equal ( properties ) ;
19
+ } ) ;
20
+
21
+ it ( 'should throw an error if creation inputs are invalid' , ( ) => {
22
+ expect ( ( ) => ElementService . createElement ( MockElement , 'E2' , [ 10 , 20 ] , [ null , new Properties ( ) ] ) ) . to . throw (
23
+ "Terminals must be an array of Position instances."
24
+ ) ;
25
+ } ) ;
26
+ } ) ;
27
+
28
+ describe ( 'Element Deletion' , ( ) => {
29
+ it ( 'should delete an element successfully' , ( ) => {
30
+ const terminals = [ new Position ( 10 , 20 ) ] ;
31
+ const properties = new Properties ( ) ;
32
+ const element = new MockElement ( 'E3' , terminals , null , properties ) ;
33
+
34
+ let elements = [ element ] ;
35
+ elements = ElementService . deleteElement ( elements , 'E3' ) ;
36
+
37
+ expect ( elements ) . to . be . an ( 'array' ) . that . is . empty ;
38
+ } ) ;
39
+
40
+ it ( 'should do nothing when deleting a non-existent element' , ( ) => {
41
+ const terminals = [ new Position ( 10 , 20 ) ] ;
42
+ const properties = new Properties ( ) ;
43
+ const element = new MockElement ( 'E4' , terminals , null , properties ) ;
44
+
45
+ let elements = [ element ] ;
46
+ elements = ElementService . deleteElement ( elements , 'NonExistentID' ) ;
47
+
48
+ expect ( elements ) . to . have . lengthOf ( 1 ) ;
49
+ expect ( elements [ 0 ] . id ) . to . equal ( 'E4' ) ;
50
+ } ) ;
51
+ } ) ;
52
+
53
+ describe ( 'Element Movement' , ( ) => {
54
+ it ( 'should move an element successfully' , ( ) => {
55
+ const terminals = [ new Position ( 10 , 20 ) , new Position ( 30 , 40 ) ] ;
56
+ const element = new MockElement ( 'E5' , terminals , null , new Properties ( ) ) ;
57
+
58
+ ElementService . moveElement ( element , new Position ( 20 , 30 ) ) ;
59
+
60
+ expect ( element . terminals ) . to . deep . equal ( [
61
+ new Position ( 20 , 30 ) ,
62
+ new Position ( 40 , 50 ) ,
63
+ ] ) ;
64
+ } ) ;
65
+
66
+ it ( 'should not change terminals if moved to the same position' , ( ) => {
67
+ const terminals = [ new Position ( 10 , 20 ) , new Position ( 30 , 40 ) ] ;
68
+ const element = new MockElement ( 'E6' , terminals , null , new Properties ( ) ) ;
69
+
70
+ ElementService . moveElement ( element , new Position ( 10 , 20 ) ) ;
71
+
72
+ expect ( element . terminals ) . to . deep . equal ( terminals ) ;
73
+ } ) ;
74
+ } ) ;
75
+
76
+ describe ( 'Element Rotation' , ( ) => {
77
+ it ( 'should rotate an element by 90 degrees' , ( ) => {
78
+ const terminals = [ new Position ( 10 , 10 ) , new Position ( 20 , 10 ) ] ;
79
+ const element = new MockElement ( 'E7' , terminals , null , new Properties ( ) ) ;
80
+
81
+ ElementService . rotateElement ( element , 90 ) ;
82
+
83
+ expect ( element . terminals ) . to . deep . equal ( [
84
+ new Position ( 10 , 10 ) , // Reference terminal remains unchanged
85
+ new Position ( 10 , 20 ) , // Rotated position
86
+ ] ) ;
87
+ } ) ;
88
+
89
+ it ( 'should rotate an element by 180 degrees' , ( ) => {
90
+ const terminals = [ new Position ( 10 , 10 ) , new Position ( 20 , 10 ) ] ;
91
+ const element = new MockElement ( 'E8' , terminals , null , new Properties ( ) ) ;
92
+
93
+ ElementService . rotateElement ( element , 180 ) ;
94
+
95
+ expect ( element . terminals ) . to . deep . equal ( [
96
+ new Position ( 10 , 10 ) , // Reference terminal remains unchanged
97
+ new Position ( 0 , 10 ) , // Rotated position
98
+ ] ) ;
99
+ } ) ;
100
+
101
+ it ( 'should throw an error for invalid rotation angles' , ( ) => {
102
+ const terminals = [ new Position ( 10 , 10 ) , new Position ( 20 , 10 ) ] ;
103
+ const element = new MockElement ( 'E9' , terminals , null , new Properties ( ) ) ;
104
+
105
+ expect ( ( ) => ElementService . rotateElement ( element , 45 ) ) . to . throw (
106
+ "Orientation must be one of 0, 90, 180, or 270 degrees."
107
+ ) ;
108
+ } ) ;
109
+ } ) ;
110
+ } ) ;
0 commit comments