425568b685a34ee7b2906982f6134a862ca5c444
[vid.git] / vid-webpack-master / src / app / drawingBoard / service-planning / drawing-board-tree / dragAndDrop / dragAndDrop.service.spec.ts
1 import {getTestBed, TestBed} from '@angular/core/testing';
2 import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing';
3 import {NgRedux} from "@angular-redux/store";
4 import {DragAndDropService} from "./dragAndDrop.service";
5 import {AppState} from "../../../../shared/store/reducers";
6
7 class MockAppStore<T> {
8   dispatch() {
9
10   }
11
12   getState() {
13     return {
14       global: {
15         flags: {
16           "FLAG_1911_INSTANTIATION_ORDER_IN_ASYNC_ALACARTE": true
17         }
18       },
19       service: {
20         serviceInstance: {
21           "serviceInstanceId": {
22             vnfs: {
23               "vnfStoreKey": {
24                 isMissingData: true,
25                 vfModules: {
26                   "vfModulesName": {
27                     "vfModulesName": {
28                       isMissingData: true
29                     }
30                   }
31                 }
32               },
33
34               "vnfStoreKey1": {
35                 isMissingData: false,
36                 vfModules: {
37                   "vfModulesName": {
38                     "vfModulesName": {
39                       isMissingData: false
40                     }
41                   }
42                 }
43               }
44             }
45           }
46         }
47       }
48     }
49   }
50 }
51
52 describe('Drag and drop service', () => {
53   let injector;
54   let service: DragAndDropService;
55   let httpMock: HttpTestingController;
56   let store: NgRedux<AppState>;
57   let nodes;
58
59   beforeAll(done => (async () => {
60     TestBed.configureTestingModule({
61       imports: [HttpClientTestingModule],
62       providers: [
63         DragAndDropService,
64         {provide: NgRedux, useClass: MockAppStore}]
65     });
66     await TestBed.compileComponents();
67
68     injector = getTestBed();
69     service = injector.get(DragAndDropService);
70     httpMock = injector.get(HttpTestingController);
71     store = injector.get(NgRedux);
72
73
74   })().then(done).catch(done.fail));
75
76   beforeEach(() => {
77     nodes = [
78       {
79         "trackById": "ckfqe3sb3y8",
80         "componentInfoType": "VNF",
81         "parentType": "",
82         "type": "VF",
83         "typeName": "VNF",
84         "instanceName": "2017-488_PASQUALE-vPE",
85         "id": "04686zg11ur2",
86         "children": [
87           {
88             "id": "1150884479608",
89             "action": "Create",
90             "instanceName": "puwesovabe",
91             "name": "puwesovabe",
92             "type": "VFmodule",
93             "trackById": "d5if1906rqa",
94             "parentType": "VNF",
95             "position": 1,
96             "componentInfoType": "VFModule",
97             "errors": {},
98             "updatePoistionFunction": () => {
99             },
100           },
101           {
102             "id": "4637423092446",
103             "action": "Create",
104             "instanceName": "bnmgtrx",
105             "name": "bnmgtrx",
106             "type": "VFmodule",
107             "trackById": "9ei9adlh27e",
108             "parentType": "VNF",
109             "position": 2,
110             "componentInfoType": "VFModule",
111             "updatePoistionFunction": () => {
112             }
113           }
114         ],
115         "errors": {},
116       }
117     ];
118   })
119   test('drag should execute array_move when the nodes parent are same', () => {
120
121     let from = {
122       id: "04686zg11ur2",
123       index: 0,
124       data: {
125         instanceName: 'puwesovabe',
126       },
127       parent: {
128         data: {
129           type: 'VF',
130           index: 0,
131           trackById: 'ckfqe3sb3y8',
132           vnfStoreKey: '2017-488_PASQUALE-vPE 0',
133         }
134       }
135     };
136
137     let to = {
138       parent: {
139         id: "4637423092446",
140         index: 1,
141         data: {
142           instanceName: 'bnmgtrx',
143         },
144         parent: {
145           data: {
146             type: 'VF',
147             trackById: 'ckfqe3sb3y8',
148             vnfStoreKey: '2017-488_PASQUALE-vPE 0',
149           }
150         }
151       }
152     };
153
154     jest.spyOn(service, 'array_move');
155
156     service.drop(store, "serviceInstanceId", nodes, {from, to});
157
158     expect(service.array_move).toHaveBeenCalled();
159
160   });
161
162
163   test('drag shouldnt execute array_move when the nodes parent are different', () => {
164
165     let from = {
166       id: 1150884479608,
167       index: 0,
168       data: {
169         instanceName: '2017-488_PASQUALE-vPE',
170       },
171       parent: {}
172     };
173
174     let to = {
175       parent: {
176         id: 4637423092446,
177         index: 1,
178         data: {
179           instanceName: 'bnmgtrx',
180         },
181         parent: {
182           data: {
183             type: 'VF',
184             trackById: '1111',
185             vnfStoreKey: '2017-488_PASQUALE-vPE 0',
186           }
187         }
188       }
189     };
190
191
192     jest.spyOn(service, 'array_move');
193
194     service.drop(store, "serviceInstanceId", nodes, {from, to});
195
196     jest.clearAllMocks();
197
198     expect(service.array_move).not.toHaveBeenCalled();
199
200   });
201
202   test('drop should change nodes index and position', () => {
203
204     let arr: Array<any> = service.array_move(nodes[0].children, 0, 1, "serviceInstanceId", '')
205
206     expect(arr[0]).toMatchObject({instanceName: "bnmgtrx", position: 1});
207     expect(arr[1]).toMatchObject({instanceName: "puwesovabe", position: 2});
208
209   });
210 });