Skip to content

Commit 7364ca3

Browse files
committed
[GLAM-610] Add tests
1 parent f44e258 commit 7364ca3

2 files changed

Lines changed: 123 additions & 10 deletions

File tree

src/app/cris-layout/cris-layout-matrix/cris-layout-box-container/boxes/metadata/rendering-types/open-street-map/open-street-map-rendering.component.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ describe('OpenStreetMapRenderingComponent', () => {
7575

7676
it('should create', () => {
7777
expect(component).toBeTruthy();
78+
expect(component.coordinates).toBe(metadataValue.value);
7879
});
7980

8081
});
Lines changed: 122 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,147 @@
1-
import { TestBed, ComponentFixture } from '@angular/core/testing';
1+
import { ComponentFixture, TestBed } from '@angular/core/testing';
22
import { OpenStreetMapComponent } from './open-street-map.component';
33
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+
77
describe('OpenStreetMapComponent', () => {
88
let component: OpenStreetMapComponent;
99
let fixture: ComponentFixture<OpenStreetMapComponent>;
1010

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+
1146
beforeEach(async () => {
1247
await TestBed.configureTestingModule({
1348
declarations: [OpenStreetMapComponent],
1449
imports: [
1550
TranslateModule.forRoot(),
1651
],
1752
providers: [
18-
LocationService,
19-
{ provide: HttpClient, useValue: {} },
53+
{ provide: LocationService, useValue: locationService },
2054
]
2155
})
22-
.compileComponents();
56+
.compileComponents();
2357
});
2458

2559
beforeEach(() => {
2660
fixture = TestBed.createComponent(OpenStreetMapComponent);
2761
component = fixture.componentInstance;
28-
fixture.detectChanges();
62+
2963
});
3064

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+
});
33145
});
34146

35147
});

0 commit comments

Comments
 (0)