aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/ng2/pages/home/home.component.spec.ts
blob: df854024fa669c27c4d9a6c438065fd6c2d6b0ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import { SdcConfigToken, ISdcConfig } from "../../config/sdc-config.config";
import { SdcMenuToken, IAppMenu } from "../../config/sdc-menu.config";


import { async, ComponentFixture, TestBed } from "@angular/core/testing";
import { HomeComponent } from "./home.component";
import {ConfigureFn, configureTests} from "../../../../jest/test-config.helper";
import {NO_ERRORS_SCHEMA} from "@angular/core";
import { TranslateService } from "../../shared/translator/translate.service";
import { HomeService, CacheService, AuthenticationService, ImportVSPService } from '../../../../app/services-ng2';
import { ModalsHandler } from "../../../../app/utils";
import { SdcUiServices } from "onap-ui-angular";
import {ComponentType, ResourceType} from "../../../utils/constants";
import { FoldersMenu, FoldersItemsMenu, FoldersItemsMenuGroup } from './folders';
import { HomeFilter } from "../../../../app/models/home-filter";
import {Component} from "../../../models/components/component";




describe('home component', () => {

    // const mockedEvent = <MouseEvent>{ target: {} }
    let fixture: ComponentFixture<HomeComponent>;
    // let eventServiceMock: Partial<EventListenerService>;

    let importVspService: Partial<ImportVSPService>;
    let mockStateService;
    let modalServiceMock :Partial<SdcUiServices.ModalService>;
    let translateServiceMock : Partial<TranslateService>;
    let foldersItemsMenuMock;
    let homeFilterMock :Partial<HomeFilter>;
    let foldersMock;
    let loaderServiceMock;


    beforeEach(
        async(() => {
            modalServiceMock = {
                openWarningModal: jest.fn()
            }

            mockStateService = {
                // go: jest.fn().mockReturnValue( new Promise.resolve((resolve, reject )=> resolve()))
                go: jest.fn()
            }

            translateServiceMock = {
                translate: jest.fn()
            }

            homeFilterMock = {
                search: jest.fn,
                toUrlParam: jest.fn()
            }

            foldersMock = {
                setSelected: jest.fn()
            }

            loaderServiceMock = {
                activate: jest.fn(),
                deactivate: jest.fn()
            }

            const configure: ConfigureFn = testBed => {
                testBed.configureTestingModule({
                    declarations: [HomeComponent],
                    imports: [],
                    schemas: [NO_ERRORS_SCHEMA],
                    providers: [
                        {provide: SdcConfigToken, useValue: {"csarFileExtension":"csar", "toscaFileExtension":"yaml,yml"}},
                        {provide: SdcMenuToken, useValue: {}},
                        {provide: "$state", useValue: mockStateService},
                        {provide: HomeService, useValue: {}},
                        {provide: AuthenticationService, useValue: {}},
                        {provide: CacheService, useValue: {}},
                        {provide: TranslateService, useValue: translateServiceMock},
                        {provide: ModalsHandler, useValue: {}},
                        {provide: SdcUiServices.ModalService, useValue: modalServiceMock},
                        {provide: SdcUiServices.LoaderService, useValue: loaderServiceMock},
                        {provide: ImportVSPService, useValue: {}}
                    ],
                });
            };

            configureTests(configure).then(testBed => {
                fixture = testBed.createComponent(HomeComponent);
            });
        })
    );


    it('should match current snapshot', () => {
        expect(fixture).toMatchSnapshot();
    });

    it('should call on home component openCreateModal with null imported file', () => {
        const component = TestBed.createComponent(HomeComponent);
        let componentType:string = 'test';
        let importedFile:any = null;
        component.componentInstance.openCreateModal(componentType, importedFile);
        expect(mockStateService.go).toBeCalledWith('workspace.general', {type: componentType.toLowerCase()});
    });


    it('should call on home component openCreateModal with imported file', () => {
        const component = TestBed.createComponent(HomeComponent);
        component.componentInstance.initEntities = jest.fn();
        let componentType:string = 'test';
        let importedFile:any = 'importedFile';
        component.componentInstance.openCreateModal(componentType, importedFile);
        expect(component.componentInstance.initEntities).toBeCalledWith(true);
    });


    it ('should call on home component onImportVf without file without extension', () => {
        const component = TestBed.createComponent(HomeComponent);
        let file:any = {filename : 'test'};
        let expectedTitle:string = translateServiceMock.translate("NEW_SERVICE_RESOURCE_ERROR_VALID_CSAR_EXTENSIONS_TITLE");
        let expectedMessage:string = translateServiceMock.translate("NEW_SERVICE_RESOURCE_ERROR_VALID_CSAR_EXTENSIONS", {"csarFileExtension":"csar"});
        component.componentInstance.onImportVf(file);
        expect(modalServiceMock.openWarningModal).toBeCalledWith(expectedTitle, expectedMessage , 'error-invalid-csar-ext');
    });


    it ('should call on home component onImportVf with file without extension' , () => {
        const component = TestBed.createComponent(HomeComponent);
        let file:any = {filename : 'test.csar'};
        component.componentInstance.onImportVf(file);
        expect(mockStateService.go).toBeCalledWith('workspace.general', {
            type: ComponentType.RESOURCE.toLowerCase(),
                importedFile: file,
                resourceType: ResourceType.VF
        });
    });


    it ('should call on home component onImportVfc without file without extension', () => {
        const component = TestBed.createComponent(HomeComponent);
        let file:any = {filename : 'test'};
        let expectedTitle:string = translateServiceMock.translate("NEW_SERVICE_RESOURCE_ERROR_VALID_TOSCA_EXTENSIONS_TITLE");
        let expectedMessage:string = translateServiceMock.translate("NEW_SERVICE_RESOURCE_ERROR_VALID_TOSCA_EXTENSIONS", {"toscaFileExtension":"yaml,yml"});
        component.componentInstance.onImportVfc(file);
        expect(modalServiceMock.openWarningModal).toBeCalledWith(expectedTitle, expectedMessage , 'error-invalid-tosca-ext');
    });

    it ('should call on home component onImportVfc with file without extension' , () => {
        const component = TestBed.createComponent(HomeComponent);
        let file:any = {filename : 'test.yml'};
        component.componentInstance.onImportVfc(file);
        expect(mockStateService.go).toBeCalledWith('workspace.general', {
            type: ComponentType.RESOURCE.toLowerCase(),
            importedFile: file,
            resourceType: ResourceType.VFC
        });
    });

    it ('should call on home component createPNF' , () => {
        const component = TestBed.createComponent(HomeComponent);
        component.componentInstance.createPNF();
        expect(mockStateService.go).toBeCalledWith('workspace.general', {
            type: ComponentType.RESOURCE.toLowerCase(),
            resourceType: ResourceType.PNF
        });
    });

    it ('should call on home component createCR' , () => {
        const component = TestBed.createComponent(HomeComponent);
        component.componentInstance.createCR();
        expect(mockStateService.go).toBeCalledWith('workspace.general', {
            type: ComponentType.RESOURCE.toLowerCase(),
            resourceType: ResourceType.CR
        });
    });


    it ('should call on home component updateFilter' , () => {
        const component = TestBed.createComponent(HomeComponent);
        component.componentInstance.homeFilter = homeFilterMock;
        component.componentInstance.filterHomeItems = jest.fn();
        component.componentInstance.updateFilter();

        expect(mockStateService.go).toBeCalledWith('.', homeFilterMock.toUrlParam(), {location: 'replace', notify: false});
        // expect(spy).toHaveBeenCalledTimes(1);

        // let spy = spyOn(homeFilterMock, 'toUrlParam').and.returnValue({
        //     'filter.term': '',
        //     'filter.distributed': '',
        //     'filter.status':''
        // });
    });

    // it ('should call on home component setSelectedFolder' , () => {
    //     const component = TestBed.createComponent(HomeComponent);
    //     let folderItem:Partial<FoldersItemsMenu> = { text:'someThing'};
    //     let folderItem1:number;
    //     component.componentInstance.folders = foldersMock;
    //     expect(foldersMock.setSelected).toBeCalledWith(folderItem);
    // });

    // it ('should call on home component goToComponent' , () => {
    //     const component = TestBed.createComponent(HomeComponent);
    //     let componentParam:Partial<Component> = { uuid:'someThing', uniqueId:'uniqueID', componentType:'componentType'};
    //     component.componentInstance.goToComponent(componentParam);
    //     expect(loaderServiceMock.activate).toHaveBeenCalled();
    //     // expect(mockStateService.go).toBeCalledWith('workspace.general', {id: componentParam.uniqueId, type: componentParam.componentType.toLowerCase()}).then(function(){
    //     //     loaderServiceMock.deactivate();
    //     // });
    //     expect(mockStateService.go).toBeCalled();
    // });

    // it ('should call on home component raiseNumberOfElementToDisplay so numberOfItemToDisplay will be 0' , () => {
    //     const component = TestBed.createComponent(HomeComponent);
    //     component.componentInstance.raiseNumberOfElementToDisplay();
    //     expect(component.componentInstance.numberOfItemToDisplay).toEqual(0);
    // });
    //
    // it ('should call on home component raiseNumberOfElementToDisplay with min(2,70) so numberOfItemToDisplay will be 2' , () => {
    //     const component = TestBed.createComponent(HomeComponent);
    //     component.componentInstance.homeItems = ['item1', 'item2'];
    //     component.componentInstance.numberOfItemToDisplay = 70;
    //     component.componentInstance.raiseNumberOfElementToDisplay(true);
    //     expect(component.componentInstance.numberOfItemToDisplay).toEqual(2);
    // });
    //
    // it ('should call on home component raiseNumberOfElementToDisplay with min(3,35) so numberOfItemToDisplay will be 2 after fullPagesAmount is calculated' , () => {
    //     const component = TestBed.createComponent(HomeComponent);
    //     component.componentInstance.homeItems = ['item1', 'item2', 'item3'];
    //     component.componentInstance.numberOfItemToDisplay = 70;
    //     component.componentInstance.numberOfItemToDisplay = 0;
    //     component.componentInstance.raiseNumberOfElementToDisplay(false);
    //     expect(component.componentInstance.numberOfItemToDisplay).toEqual(3);
    // });
    //
    //
    // it ('should call on home component changeFilterTerm' , () => {
    //     const component = TestBed.createComponent(HomeComponent);
    //     component.componentInstance.changeFilterTerm("testStr");
    //     // expect ( "testStr" ).toEqual(homeFilterMock.search.)
    // });






    // it ('should call on home component entitiesCount' , () => {
    //     const component = TestBed.createComponent(HomeComponent);
    //     component.componentInstance.entitiesCount("aaa");
    //     expect(mockStateService.go).toBeCalledWith('workspace.general', {
    //         type: ComponentType.RESOURCE.toLowerCase(),
    //         resourceType: ResourceType.CR
    //     });
    // });


    // it('should call on home component notificationIconCallback', () => {
    //     const component = TestBed.createComponent(HomeComponent);
    //     component.componentInstance.initEntities = jest.fn();
    //     component.componentInstance.notificationIconCallback();
    //     expect(mockStateService.go).toBeCalledWith('workspace.general', {});
    // });





});