From e905a91fd1135f767afc1146c6137d8cf8d7d6c8 Mon Sep 17 00:00:00 2001 From: Piotr Pekala Date: Wed, 26 Jun 2019 02:48:02 -0700 Subject: [PATCH] Unit test added --- .../project-map-menu.component.spec.ts | 67 +++++++++++++++++++ .../project-map/project-map.component.spec.ts | 42 +++--------- 2 files changed, 76 insertions(+), 33 deletions(-) diff --git a/src/app/components/project-map/project-map-menu/project-map-menu.component.spec.ts b/src/app/components/project-map/project-map-menu/project-map-menu.component.spec.ts index e69de29b..f998feac 100644 --- a/src/app/components/project-map/project-map-menu/project-map-menu.component.spec.ts +++ b/src/app/components/project-map/project-map-menu/project-map-menu.component.spec.ts @@ -0,0 +1,67 @@ +import { ProjectMapMenuComponent } from "./project-map-menu.component"; +import { ComponentFixture, async, TestBed } from '@angular/core/testing'; +import { MockedDrawingService } from '../project-map.component.spec'; +import { MapSettingService } from '../../../services/mapsettings.service'; +import { MatIconModule, MatToolbarModule, MatMenuModule, MatCheckboxModule } from '@angular/material'; +import { CommonModule } from '@angular/common'; +import { NoopAnimationsModule } from '@angular/platform-browser/animations'; +import { DrawingService } from '../../../services/drawing.service'; +import { ToolsService } from '../../../services/tools.service'; +import { D3MapComponent } from '../../../cartography/components/d3-map/d3-map.component'; +import { ANGULAR_MAP_DECLARATIONS } from '../../../cartography/angular-map.imports'; +import { NO_ERRORS_SCHEMA } from '@angular/core'; + +describe('ProjectMapMenuComponent', () => { + let component: ProjectMapMenuComponent; + let fixture: ComponentFixture; + let drawingService = new MockedDrawingService(); + let mapSettingService = new MapSettingService(); + + beforeEach(async(() => { + TestBed.configureTestingModule({ + imports: [MatIconModule, MatToolbarModule, MatMenuModule, MatCheckboxModule, CommonModule, NoopAnimationsModule], + providers: [ + { provide: DrawingService, useValue: drawingService }, + { provide: ToolsService }, + { provide: MapSettingService, useValue: mapSettingService } + ], + declarations: [ProjectMapMenuComponent, D3MapComponent, ...ANGULAR_MAP_DECLARATIONS], + schemas: [NO_ERRORS_SCHEMA] + }).compileComponents(); + })); + + beforeEach(() => { + fixture = TestBed.createComponent(ProjectMapMenuComponent); + component = fixture.componentInstance; + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); + + it('should reset choice on draw menu after saving drawing', () => { + spyOn(component, 'resetDrawToolChoice'); + + component.onDrawingSaved(); + + expect(component.resetDrawToolChoice).toHaveBeenCalled(); + }); + + it('should call map settings service when lock value was changed', () => { + spyOn(mapSettingService, 'changeMapLockValue'); + + component.changeLockValue(); + + expect(mapSettingService.changeMapLockValue).toHaveBeenCalled(); + }); + + it('should call map settings service with proper value', () => { + spyOn(mapSettingService, 'changeMapLockValue'); + + component.changeLockValue(); + expect(mapSettingService.changeMapLockValue).toHaveBeenCalledWith(true); + + component.changeLockValue(); + expect(mapSettingService.changeMapLockValue).toHaveBeenCalledWith(false); + }); +}); diff --git a/src/app/components/project-map/project-map.component.spec.ts b/src/app/components/project-map/project-map.component.spec.ts index 2d34ba48..0fc724c0 100644 --- a/src/app/components/project-map/project-map.component.spec.ts +++ b/src/app/components/project-map/project-map.component.spec.ts @@ -44,6 +44,7 @@ import { CapturingSettings } from '../../models/capturingSettings'; import { LinkWidget } from '../../cartography/widgets/link'; import { NodeCreatedLabelStylesFixer } from './helpers/node-created-label-styles-fixer'; import { MapSettingService } from '../../services/mapsettings.service'; +import { ProjectMapMenuComponent } from './project-map-menu/project-map-menu.component'; export class MockedProgressService { public activate() {} @@ -185,7 +186,6 @@ describe('ProjectMapComponent', () => { let nodesDataSource = new MockedNodesDataSource(); let linksDataSource = new MockedLinksDataSource(); let nodeCreatedLabelStylesFixer; - let mapSettingService = new MapSettingService(); beforeEach(async(() => { nodeCreatedLabelStylesFixer = { @@ -222,10 +222,9 @@ describe('ProjectMapComponent', () => { provide: RecentlyOpenedProjectService, useClass: RecentlyOpenedProjectService }, - { provide: NodeCreatedLabelStylesFixer, useValue: nodeCreatedLabelStylesFixer}, - { provide: MapSettingService, useValue: mapSettingService } + { provide: NodeCreatedLabelStylesFixer, useValue: nodeCreatedLabelStylesFixer} ], - declarations: [ProjectMapComponent, D3MapComponent, ...ANGULAR_MAP_DECLARATIONS], + declarations: [ProjectMapComponent, ProjectMapMenuComponent, D3MapComponent, ...ANGULAR_MAP_DECLARATIONS], schemas: [NO_ERRORS_SCHEMA] }).compileComponents(); })); @@ -233,6 +232,9 @@ describe('ProjectMapComponent', () => { beforeEach(() => { fixture = TestBed.createComponent(ProjectMapComponent); component = fixture.componentInstance; + component.projectMapMenuComponent = { + resetDrawToolChoice(){} + } as ProjectMapMenuComponent; }); afterEach(() => { @@ -246,38 +248,12 @@ describe('ProjectMapComponent', () => { it('should hide draw tools when hide menu is called', () => { var dummyElement = document.createElement('map'); document.getElementsByClassName = jasmine.createSpy('HTML element').and.callFake(() => { - return [dummyElement]; + return [dummyElement]; }); - spyOn(component, 'resetDrawToolChoice'); + spyOn(component.projectMapMenuComponent, 'resetDrawToolChoice').and.returnValue(); component.hideMenu(); - expect(component.resetDrawToolChoice).toHaveBeenCalled(); - }); - - it('should reset choice on draw menu after saving drawing', () => { - spyOn(component, 'resetDrawToolChoice'); - - component.onDrawingSaved(); - - expect(component.resetDrawToolChoice).toHaveBeenCalled(); - }); - - it('should call map settings service when lock value was changed', () => { - spyOn(mapSettingService, 'changeMapLockValue'); - - component.changeLockValue(); - - expect(mapSettingService.changeMapLockValue).toHaveBeenCalled(); - }); - - it('should call map settings service with proper value', () => { - spyOn(mapSettingService, 'changeMapLockValue'); - - component.changeLockValue(); - expect(mapSettingService.changeMapLockValue).toHaveBeenCalledWith(true); - - component.changeLockValue(); - expect(mapSettingService.changeMapLockValue).toHaveBeenCalledWith(false); + expect(component.projectMapMenuComponent.resetDrawToolChoice).toHaveBeenCalled(); }); });