Catalog alignment
[sdc.git] / catalog-ui / src / app / ng2 / pages / home / home.component.spec.ts
1
2 import { SdcConfigToken, ISdcConfig } from "../../config/sdc-config.config";
3 import { SdcMenuToken, IAppMenu } from "../../config/sdc-menu.config";
4
5
6 import { async, ComponentFixture, TestBed } from "@angular/core/testing";
7 import { HomeComponent } from "./home.component";
8 import {ConfigureFn, configureTests} from "../../../../jest/test-config.helper";
9 import {NO_ERRORS_SCHEMA} from "@angular/core";
10 import { TranslateService } from "../../shared/translator/translate.service";
11 import { HomeService, CacheService, AuthenticationService, ImportVSPService } from '../../../../app/services-ng2';
12 import { ModalsHandler } from "../../../../app/utils";
13 import { SdcUiServices } from "onap-ui-angular";
14 import {ComponentType, ResourceType} from "../../../utils/constants";
15 import { FoldersMenu, FoldersItemsMenu, FoldersItemsMenuGroup } from './folders';
16 import { HomeFilter } from "../../../../app/models/home-filter";
17 import {Component} from "../../../models/components/component";
18
19
20
21
22 describe('home component', () => {
23
24     // const mockedEvent = <MouseEvent>{ target: {} }
25     let fixture: ComponentFixture<HomeComponent>;
26     // let eventServiceMock: Partial<EventListenerService>;
27
28     let importVspService: Partial<ImportVSPService>;
29     let mockStateService;
30     let modalServiceMock :Partial<SdcUiServices.ModalService>;
31     let translateServiceMock : Partial<TranslateService>;
32     let foldersItemsMenuMock;
33     let homeFilterMock :Partial<HomeFilter>;
34     let foldersMock;
35     let loaderServiceMock;
36
37
38     beforeEach(
39         async(() => {
40             modalServiceMock = {
41                 openWarningModal: jest.fn()
42             }
43
44             mockStateService = {
45                 // go: jest.fn().mockReturnValue( new Promise.resolve((resolve, reject )=> resolve()))
46                 go: jest.fn()
47             }
48
49             translateServiceMock = {
50                 translate: jest.fn()
51             }
52
53             homeFilterMock = {
54                 search: jest.fn,
55                 toUrlParam: jest.fn()
56             }
57
58             foldersMock = {
59                 setSelected: jest.fn()
60             }
61
62             loaderServiceMock = {
63                 activate: jest.fn(),
64                 deactivate: jest.fn()
65             }
66
67             const configure: ConfigureFn = testBed => {
68                 testBed.configureTestingModule({
69                     declarations: [HomeComponent],
70                     imports: [],
71                     schemas: [NO_ERRORS_SCHEMA],
72                     providers: [
73                         {provide: SdcConfigToken, useValue: {"csarFileExtension":"csar", "toscaFileExtension":"yaml,yml"}},
74                         {provide: SdcMenuToken, useValue: {}},
75                         {provide: "$state", useValue: mockStateService},
76                         {provide: HomeService, useValue: {}},
77                         {provide: AuthenticationService, useValue: {}},
78                         {provide: CacheService, useValue: {}},
79                         {provide: TranslateService, useValue: translateServiceMock},
80                         {provide: ModalsHandler, useValue: {}},
81                         {provide: SdcUiServices.ModalService, useValue: modalServiceMock},
82                         {provide: SdcUiServices.LoaderService, useValue: loaderServiceMock},
83                         {provide: ImportVSPService, useValue: {}}
84                     ],
85                 });
86             };
87
88             configureTests(configure).then(testBed => {
89                 fixture = testBed.createComponent(HomeComponent);
90             });
91         })
92     );
93
94
95     it('should match current snapshot', () => {
96         expect(fixture).toMatchSnapshot();
97     });
98
99     it('should call on home component openCreateModal with null imported file', () => {
100         const component = TestBed.createComponent(HomeComponent);
101         let componentType:string = 'test';
102         let importedFile:any = null;
103         component.componentInstance.openCreateModal(componentType, importedFile);
104         expect(mockStateService.go).toBeCalledWith('workspace.general', {type: componentType.toLowerCase()});
105     });
106
107
108     it('should call on home component openCreateModal with imported file', () => {
109         const component = TestBed.createComponent(HomeComponent);
110         component.componentInstance.initEntities = jest.fn();
111         let componentType:string = 'test';
112         let importedFile:any = 'importedFile';
113         component.componentInstance.openCreateModal(componentType, importedFile);
114         expect(component.componentInstance.initEntities).toBeCalledWith(true);
115     });
116
117
118     it ('should call on home component onImportVf without file without extension', () => {
119         const component = TestBed.createComponent(HomeComponent);
120         let file:any = {filename : 'test'};
121         let expectedTitle:string = translateServiceMock.translate("NEW_SERVICE_RESOURCE_ERROR_VALID_CSAR_EXTENSIONS_TITLE");
122         let expectedMessage:string = translateServiceMock.translate("NEW_SERVICE_RESOURCE_ERROR_VALID_CSAR_EXTENSIONS", {"csarFileExtension":"csar"});
123         component.componentInstance.onImportVf(file);
124         expect(modalServiceMock.openWarningModal).toBeCalledWith(expectedTitle, expectedMessage , 'error-invalid-csar-ext');
125     });
126
127
128     it ('should call on home component onImportVf with file without extension' , () => {
129         const component = TestBed.createComponent(HomeComponent);
130         let file:any = {filename : 'test.csar'};
131         component.componentInstance.onImportVf(file);
132         expect(mockStateService.go).toBeCalledWith('workspace.general', {
133             type: ComponentType.RESOURCE.toLowerCase(),
134                 importedFile: file,
135                 resourceType: ResourceType.VF
136         });
137     });
138
139
140     it ('should call on home component onImportVfc without file without extension', () => {
141         const component = TestBed.createComponent(HomeComponent);
142         let file:any = {filename : 'test'};
143         let expectedTitle:string = translateServiceMock.translate("NEW_SERVICE_RESOURCE_ERROR_VALID_TOSCA_EXTENSIONS_TITLE");
144         let expectedMessage:string = translateServiceMock.translate("NEW_SERVICE_RESOURCE_ERROR_VALID_TOSCA_EXTENSIONS", {"toscaFileExtension":"yaml,yml"});
145         component.componentInstance.onImportVfc(file);
146         expect(modalServiceMock.openWarningModal).toBeCalledWith(expectedTitle, expectedMessage , 'error-invalid-tosca-ext');
147     });
148
149     it ('should call on home component onImportVfc with file without extension' , () => {
150         const component = TestBed.createComponent(HomeComponent);
151         let file:any = {filename : 'test.yml'};
152         component.componentInstance.onImportVfc(file);
153         expect(mockStateService.go).toBeCalledWith('workspace.general', {
154             type: ComponentType.RESOURCE.toLowerCase(),
155             importedFile: file,
156             resourceType: ResourceType.VFC
157         });
158     });
159
160     it ('should call on home component createPNF' , () => {
161         const component = TestBed.createComponent(HomeComponent);
162         component.componentInstance.createPNF();
163         expect(mockStateService.go).toBeCalledWith('workspace.general', {
164             type: ComponentType.RESOURCE.toLowerCase(),
165             resourceType: ResourceType.PNF
166         });
167     });
168
169     it ('should call on home component createCR' , () => {
170         const component = TestBed.createComponent(HomeComponent);
171         component.componentInstance.createCR();
172         expect(mockStateService.go).toBeCalledWith('workspace.general', {
173             type: ComponentType.RESOURCE.toLowerCase(),
174             resourceType: ResourceType.CR
175         });
176     });
177
178
179     it ('should call on home component updateFilter' , () => {
180         const component = TestBed.createComponent(HomeComponent);
181         component.componentInstance.homeFilter = homeFilterMock;
182         component.componentInstance.filterHomeItems = jest.fn();
183         component.componentInstance.updateFilter();
184
185         expect(mockStateService.go).toBeCalledWith('.', homeFilterMock.toUrlParam(), {location: 'replace', notify: false});
186         // expect(spy).toHaveBeenCalledTimes(1);
187
188         // let spy = spyOn(homeFilterMock, 'toUrlParam').and.returnValue({
189         //     'filter.term': '',
190         //     'filter.distributed': '',
191         //     'filter.status':''
192         // });
193     });
194
195     // it ('should call on home component setSelectedFolder' , () => {
196     //     const component = TestBed.createComponent(HomeComponent);
197     //     let folderItem:Partial<FoldersItemsMenu> = { text:'someThing'};
198     //     let folderItem1:number;
199     //     component.componentInstance.folders = foldersMock;
200     //     expect(foldersMock.setSelected).toBeCalledWith(folderItem);
201     // });
202
203     // it ('should call on home component goToComponent' , () => {
204     //     const component = TestBed.createComponent(HomeComponent);
205     //     let componentParam:Partial<Component> = { uuid:'someThing', uniqueId:'uniqueID', componentType:'componentType'};
206     //     component.componentInstance.goToComponent(componentParam);
207     //     expect(loaderServiceMock.activate).toHaveBeenCalled();
208     //     // expect(mockStateService.go).toBeCalledWith('workspace.general', {id: componentParam.uniqueId, type: componentParam.componentType.toLowerCase()}).then(function(){
209     //     //     loaderServiceMock.deactivate();
210     //     // });
211     //     expect(mockStateService.go).toBeCalled();
212     // });
213
214     // it ('should call on home component raiseNumberOfElementToDisplay so numberOfItemToDisplay will be 0' , () => {
215     //     const component = TestBed.createComponent(HomeComponent);
216     //     component.componentInstance.raiseNumberOfElementToDisplay();
217     //     expect(component.componentInstance.numberOfItemToDisplay).toEqual(0);
218     // });
219     //
220     // it ('should call on home component raiseNumberOfElementToDisplay with min(2,70) so numberOfItemToDisplay will be 2' , () => {
221     //     const component = TestBed.createComponent(HomeComponent);
222     //     component.componentInstance.homeItems = ['item1', 'item2'];
223     //     component.componentInstance.numberOfItemToDisplay = 70;
224     //     component.componentInstance.raiseNumberOfElementToDisplay(true);
225     //     expect(component.componentInstance.numberOfItemToDisplay).toEqual(2);
226     // });
227     //
228     // it ('should call on home component raiseNumberOfElementToDisplay with min(3,35) so numberOfItemToDisplay will be 2 after fullPagesAmount is calculated' , () => {
229     //     const component = TestBed.createComponent(HomeComponent);
230     //     component.componentInstance.homeItems = ['item1', 'item2', 'item3'];
231     //     component.componentInstance.numberOfItemToDisplay = 70;
232     //     component.componentInstance.numberOfItemToDisplay = 0;
233     //     component.componentInstance.raiseNumberOfElementToDisplay(false);
234     //     expect(component.componentInstance.numberOfItemToDisplay).toEqual(3);
235     // });
236     //
237     //
238     // it ('should call on home component changeFilterTerm' , () => {
239     //     const component = TestBed.createComponent(HomeComponent);
240     //     component.componentInstance.changeFilterTerm("testStr");
241     //     // expect ( "testStr" ).toEqual(homeFilterMock.search.)
242     // });
243
244
245
246
247
248
249     // it ('should call on home component entitiesCount' , () => {
250     //     const component = TestBed.createComponent(HomeComponent);
251     //     component.componentInstance.entitiesCount("aaa");
252     //     expect(mockStateService.go).toBeCalledWith('workspace.general', {
253     //         type: ComponentType.RESOURCE.toLowerCase(),
254     //         resourceType: ResourceType.CR
255     //     });
256     // });
257
258
259     // it('should call on home component notificationIconCallback', () => {
260     //     const component = TestBed.createComponent(HomeComponent);
261     //     component.componentInstance.initEntities = jest.fn();
262     //     component.componentInstance.notificationIconCallback();
263     //     expect(mockStateService.go).toBeCalledWith('workspace.general', {});
264     // });
265
266
267
268
269
270 });