Catalog alignment
[sdc.git] / catalog-ui / src / app / ng2 / pages / composition / palette / palette.component.spec.ts
1 import {async, ComponentFixture, TestBed} from "@angular/core/testing";
2 import {NO_ERRORS_SCHEMA} from "@angular/core";
3 import {CompositionPaletteService} from "./services/palette.service";
4 import {EventListenerService} from "../../../../services/event-listener-service";
5 import {PaletteElementComponent} from "./palette-element/palette-element.component";
6 import {PaletteComponent} from "./palette.component";
7 import {ConfigureFn, configureTests} from "../../../../../jest/test-config.helper";
8 import {GRAPH_EVENTS} from "../../../../utils/constants";
9 import {KeyValuePipe} from "../../../pipes/key-value.pipe";
10 import {ResourceNamePipe} from "../../../pipes/resource-name.pipe";
11 import {LeftPaletteComponent} from "../../../../models/components/displayComponent";
12 import {Observable} from "rxjs/Observable";
13 import {leftPaletteElements} from "../../../../../jest/mocks/left-paeltte-elements.mock";
14 import {NgxsModule, Select} from '@ngxs/store';
15 import { WorkspaceState } from 'app/ng2/store/states/workspace.state';
16
17
18 describe('palette component', () => {
19
20     const mockedEvent = <MouseEvent>{ target: {} }
21     let fixture: ComponentFixture<PaletteComponent>;
22     let eventServiceMock: Partial<EventListenerService>;
23     let compositionPaletteMockService: Partial<CompositionPaletteService>;
24
25     beforeEach(
26         async(() => {
27             eventServiceMock = {
28                 notifyObservers: jest.fn()
29             }
30             compositionPaletteMockService = {
31                 subscribeToLeftPaletteElements:  jest.fn().mockImplementation(()=>  Observable.of(leftPaletteElements)),
32                 getLeftPaletteElements: jest.fn().mockImplementation(()=>  leftPaletteElements)
33             }
34             const configure: ConfigureFn = testBed => {
35                 testBed.configureTestingModule({
36                     declarations: [PaletteComponent, PaletteElementComponent, KeyValuePipe, ResourceNamePipe],
37                     imports: [NgxsModule.forRoot([WorkspaceState])],
38                     schemas: [NO_ERRORS_SCHEMA],
39                     providers: [
40                         {provide: CompositionPaletteService, useValue: compositionPaletteMockService},
41                         {provide: EventListenerService, useValue: eventServiceMock}
42                     ],
43                 });
44             };
45
46             configureTests(configure).then(testBed => {
47                 fixture = testBed.createComponent(PaletteComponent);
48             });
49         })
50     );
51
52     it('should match current snapshot of palette component', () => {
53         expect(fixture).toMatchSnapshot();
54     });
55
56     it('should call on palette component hover in event', () => {
57         let paletteObject =  <LeftPaletteComponent>{categoryType: 'COMPONENT'};
58         fixture.componentInstance.onMouseOver(mockedEvent, paletteObject);
59         expect(eventServiceMock.notifyObservers).toHaveBeenCalledWith(GRAPH_EVENTS.ON_PALETTE_COMPONENT_HOVER_IN, paletteObject);
60     });
61
62     it('should call on palette component hover out event', () => {
63         let paletteObject =  <LeftPaletteComponent>{categoryType: 'COMPONENT'};
64         fixture.componentInstance.onMouseOut(paletteObject);
65         expect(eventServiceMock.notifyObservers).toHaveBeenCalledWith(GRAPH_EVENTS.ON_PALETTE_COMPONENT_HOVER_OUT);
66     });
67
68     it('should call show popup panel event', () => {
69         let paletteObject =  <LeftPaletteComponent>{categoryType: 'GROUP'};
70         fixture.componentInstance.onMouseOver(mockedEvent, paletteObject);
71         expect(eventServiceMock.notifyObservers).toHaveBeenCalledWith(GRAPH_EVENTS.ON_PALETTE_COMPONENT_SHOW_POPUP_PANEL, paletteObject, mockedEvent.target);
72     });
73
74     it('should call  hide popup panel event', () => {
75         let paletteObject =  <LeftPaletteComponent>{categoryType: 'GROUP'};
76         fixture.componentInstance.onMouseOut(paletteObject);
77         expect(eventServiceMock.notifyObservers).toHaveBeenCalledWith(GRAPH_EVENTS.ON_PALETTE_COMPONENT_HIDE_POPUP_PANEL);
78     });
79
80     it('should build Palette By Categories without searchText', () => {
81         fixture.componentInstance.buildPaletteByCategories();
82         expect(fixture.componentInstance.paletteElements["Generic"]["Network"].length).toBe(5);
83         expect(fixture.componentInstance.paletteElements["Generic"]["Network"][0].searchFilterTerms).toBe("extvirtualmachineinterfacecp external port for virtual machine interface extvirtualmachineinterfacecp 3.0");
84         expect(fixture.componentInstance.paletteElements["Generic"]["Network"][1].searchFilterTerms).toBe("newservice2 asdfasdfa newservice2 0.3");
85
86         expect(fixture.componentInstance.paletteElements["Generic"]["Configuration"].length).toBe(1);
87         expect(fixture.componentInstance.paletteElements["Generic"]["Configuration"][0].systemName).toBe("Extvirtualmachineinterfacecp");
88     });
89
90     it('should build Palette By Categories with searchText', () => {
91         fixture.componentInstance.buildPaletteByCategories("testVal");
92         expect(fixture.componentInstance.paletteElements["Generic"]["Network"].length).toBe(1);
93         expect(fixture.componentInstance.paletteElements["Generic"]["Network"][0].searchFilterTerms).toBe("testVal and other values");
94     });
95
96     it('should change numbers of elements', () => {
97         fixture.componentInstance.buildPaletteByCategories();
98         expect(fixture.componentInstance.numberOfElements).toEqual(6);
99         fixture.componentInstance.buildPaletteByCategories("testVal");
100         expect(fixture.componentInstance.numberOfElements).toEqual(1);
101     });
102 });