|
1 | | -import { TestBed, ComponentFixture } from '@angular/core/testing'; |
| 1 | +import { ComponentFixture, TestBed } from '@angular/core/testing'; |
2 | 2 | import { OpenStreetMapComponent } from './open-street-map.component'; |
3 | 3 | import { TranslateModule } from '@ngx-translate/core'; |
4 | | -import { LocationService } from '../../core/services/location.service'; |
5 | | -import { HttpClient } from '@angular/common/http'; |
6 | | -// |
| 4 | +import { LocationErrorCodes, LocationPlace, LocationService } from '../../core/services/location.service'; |
| 5 | +import { of, throwError } from 'rxjs'; |
| 6 | + |
7 | 7 | describe('OpenStreetMapComponent', () => { |
8 | 8 | let component: OpenStreetMapComponent; |
9 | 9 | let fixture: ComponentFixture<OpenStreetMapComponent>; |
10 | 10 |
|
| 11 | + const locationService = jasmine.createSpyObj('locationService', { |
| 12 | + searchPlace: jasmine.createSpy('searchPlace'), |
| 13 | + searchCoordinates: jasmine.createSpy('searchCoordinates'), |
| 14 | + isValidCoordinateString: jasmine.createSpy('isValidCoordinateString'), |
| 15 | + parseCoordinates: jasmine.createSpy('parseCoordinates'), |
| 16 | + isCoordinateString: jasmine.createSpy('isCoordinateString'), |
| 17 | + }); |
| 18 | + |
| 19 | + const coordinates = '52.520008,13.404954'; |
| 20 | + const address = '10178 Berlin, Germania'; |
| 21 | + const place: LocationPlace = { |
| 22 | + coordinates: { |
| 23 | + latitude: 52.520008, |
| 24 | + longitude: 13.404954, |
| 25 | + }, |
| 26 | + displayName: '10178 Berlin, Germania' |
| 27 | + }; |
| 28 | + |
| 29 | + const errPlace: LocationPlace = { |
| 30 | + coordinates: { |
| 31 | + latitude: 52.520008, |
| 32 | + longitude: 13.404954, |
| 33 | + } |
| 34 | + }; |
| 35 | + |
| 36 | + const placeOnlyCoordinates: LocationPlace = { |
| 37 | + coordinates: { |
| 38 | + latitude: 52.520008, |
| 39 | + longitude: 13.404954, |
| 40 | + }, |
| 41 | + }; |
| 42 | + const placeWithAddress: LocationPlace = { |
| 43 | + displayName: '10178 Berlin, Germania' |
| 44 | + }; |
| 45 | + |
11 | 46 | beforeEach(async () => { |
12 | 47 | await TestBed.configureTestingModule({ |
13 | 48 | declarations: [OpenStreetMapComponent], |
14 | 49 | imports: [ |
15 | 50 | TranslateModule.forRoot(), |
16 | 51 | ], |
17 | 52 | providers: [ |
18 | | - LocationService, |
19 | | - { provide: HttpClient, useValue: {} }, |
| 53 | + { provide: LocationService, useValue: locationService }, |
20 | 54 | ] |
21 | 55 | }) |
22 | | - .compileComponents(); |
| 56 | + .compileComponents(); |
23 | 57 | }); |
24 | 58 |
|
25 | 59 | beforeEach(() => { |
26 | 60 | fixture = TestBed.createComponent(OpenStreetMapComponent); |
27 | 61 | component = fixture.componentInstance; |
28 | | - fixture.detectChanges(); |
| 62 | + |
29 | 63 | }); |
30 | 64 |
|
31 | | - it('should create the component', () => { |
32 | | - expect(component).toBeTruthy(); |
| 65 | + describe('when coordinates are given', () => { |
| 66 | + beforeEach(() => { |
| 67 | + component.coordinates = coordinates; |
| 68 | + locationService.isCoordinateString.and.returnValue(true); |
| 69 | + locationService.isValidCoordinateString.and.returnValue(true); |
| 70 | + locationService.parseCoordinates.and.returnValue(place.coordinates); |
| 71 | + }); |
| 72 | + |
| 73 | + describe('and they are found', () => { |
| 74 | + beforeEach(() => { |
| 75 | + locationService.searchCoordinates.and.returnValue(of(place.displayName)); |
| 76 | + fixture.detectChanges(); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should create the component', () => { |
| 80 | + expect(component).toBeTruthy(); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should init the place object', () => { |
| 84 | + expect(component.place.value).toEqual(place); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + describe('and they are not found', () => { |
| 89 | + beforeEach(() => { |
| 90 | + |
| 91 | + locationService.searchCoordinates.and.callFake(() => { |
| 92 | + return throwError(() => new Error('Fake error')); |
| 93 | + }); |
| 94 | + fixture.detectChanges(); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should create the component', () => { |
| 98 | + expect(component).toBeTruthy(); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should init the place object', () => { |
| 102 | + expect(component.place.value).toEqual(errPlace); |
| 103 | + }); |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + describe('when address is given', () => { |
| 108 | + beforeEach(() => { |
| 109 | + component.coordinates = address; |
| 110 | + locationService.isCoordinateString.and.returnValue(false); |
| 111 | + }); |
| 112 | + |
| 113 | + describe('and exists', () => { |
| 114 | + beforeEach(() => { |
| 115 | + locationService.searchPlace.and.returnValue(of(placeOnlyCoordinates)); |
| 116 | + fixture.detectChanges(); |
| 117 | + }); |
| 118 | + |
| 119 | + it('should create the component', () => { |
| 120 | + expect(component).toBeTruthy(); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should init the place object', () => { |
| 124 | + expect(component.place.value).toEqual(place); |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + describe('and is not found', () => { |
| 129 | + beforeEach(() => { |
| 130 | + |
| 131 | + locationService.searchPlace.and.callFake(() => { |
| 132 | + return throwError(() => new Error(LocationErrorCodes.API_ERROR)); |
| 133 | + }); |
| 134 | + fixture.detectChanges(); |
| 135 | + }); |
| 136 | + |
| 137 | + it('should create the component', () => { |
| 138 | + expect(component).toBeTruthy(); |
| 139 | + }); |
| 140 | + |
| 141 | + it('should init the place object', () => { |
| 142 | + expect(component.invalidLocationErrorCode.value).toBe(LocationErrorCodes.API_ERROR); |
| 143 | + }); |
| 144 | + }); |
33 | 145 | }); |
34 | 146 |
|
35 | 147 | }); |
0 commit comments