|
1 |
| -// import { async, ComponentFixture, TestBed } from '@angular/core/testing'; |
2 |
| - |
3 |
| -// import { LocationPickerComponent } from './location-picker.component'; |
4 |
| - |
5 |
| -// describe('LocationPickerComponent', () => { |
6 |
| -// let component: LocationPickerComponent; |
7 |
| -// let fixture: ComponentFixture<LocationPickerComponent>; |
8 |
| - |
9 |
| -// beforeEach(async(() => { |
10 |
| -// TestBed.configureTestingModule({ |
11 |
| -// declarations: [ LocationPickerComponent ] |
12 |
| -// }) |
13 |
| -// .compileComponents(); |
14 |
| -// })); |
15 |
| - |
16 |
| -// beforeEach(() => { |
17 |
| -// fixture = TestBed.createComponent(LocationPickerComponent); |
18 |
| -// component = fixture.componentInstance; |
19 |
| -// fixture.detectChanges(); |
20 |
| -// }); |
21 |
| - |
22 |
| -// it('should create', () => { |
23 |
| -// expect(component).toBeTruthy(); |
24 |
| -// }); |
25 |
| -// }); |
| 1 | +import { Shallow } from 'shallow-render'; |
| 2 | + |
| 3 | +import { FileBrowserComponentsModule } from '@fileBrowser/file-browser-components.module'; |
| 4 | +import { ApiService } from '@shared/services/api/api.service'; |
| 5 | +import { MessageService } from '@shared/services/message/message.service'; |
| 6 | +import { EditService } from '@core/services/edit/edit.service'; |
| 7 | +import { LocationPickerComponent } from './location-picker.component'; |
| 8 | + |
| 9 | +(globalThis as any).google = { |
| 10 | + maps: { |
| 11 | + LatLng: class { |
| 12 | + private _lat: number; |
| 13 | + private _lng: number; |
| 14 | + constructor(lat: number, lng: number) { |
| 15 | + this._lat = lat; |
| 16 | + this._lng = lng; |
| 17 | + } |
| 18 | + lat() { |
| 19 | + return this._lat; |
| 20 | + } |
| 21 | + lng() { |
| 22 | + return this._lng; |
| 23 | + } |
| 24 | + }, |
| 25 | + places: { |
| 26 | + Autocomplete: class { |
| 27 | + constructor(public input: any) {} |
| 28 | + setFields(_: string[]) {} |
| 29 | + addListener(_: string, cb: () => void) { |
| 30 | + cb(); // immediately invoke the callback for test |
| 31 | + } |
| 32 | + getPlace() { |
| 33 | + return { |
| 34 | + name: 'Mock Place', |
| 35 | + address_components: [], |
| 36 | + geometry: { |
| 37 | + location: { |
| 38 | + lat: () => 12.34, |
| 39 | + lng: () => 56.78, |
| 40 | + }, |
| 41 | + }, |
| 42 | + }; |
| 43 | + } |
| 44 | + }, |
| 45 | + }, |
| 46 | + Map: class {}, |
| 47 | + MapMouseEvent: class {}, |
| 48 | + }, |
| 49 | +}; |
| 50 | + |
| 51 | +const mockApiService = {}; |
| 52 | + |
| 53 | +describe('LocationPickerComponent', () => { |
| 54 | + let shallow: Shallow<LocationPickerComponent>; |
| 55 | + let instance: LocationPickerComponent; |
| 56 | + let fixture; |
| 57 | + let messageShown = false; |
| 58 | + let mockEditService: jasmine.SpyObj<EditService>; |
| 59 | + |
| 60 | + beforeEach(async () => { |
| 61 | + mockEditService = jasmine.createSpyObj('EditService', ['updateItems']); |
| 62 | + mockEditService.updateItems.and.resolveTo({}); |
| 63 | + |
| 64 | + shallow = new Shallow(LocationPickerComponent, FileBrowserComponentsModule) |
| 65 | + .mock(ApiService, mockApiService) |
| 66 | + .mock(MessageService, { |
| 67 | + showError: () => { |
| 68 | + messageShown = true; |
| 69 | + }, |
| 70 | + }) |
| 71 | + .mock(EditService, mockEditService); |
| 72 | + |
| 73 | + const renderResult = await shallow.render({ detectChanges: false }); |
| 74 | + instance = renderResult.instance; |
| 75 | + fixture = renderResult.fixture; |
| 76 | + |
| 77 | + instance.map = { |
| 78 | + panTo: () => {}, |
| 79 | + } as any; |
| 80 | + |
| 81 | + spyOn(instance, 'onAutocompletePlaceSelect').and.stub(); |
| 82 | + |
| 83 | + fixture.detectChanges(); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should create', async () => { |
| 87 | + expect(instance).toBeTruthy(); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should update item with current location and call editService.updateItems()', async () => { |
| 91 | + const mockLocation = { latitude: 1, longitude: 2 } as any; |
| 92 | + |
| 93 | + const mockItem = { |
| 94 | + LocnVO: null, |
| 95 | + locnId: 123, |
| 96 | + update: jasmine.createSpy('update'), |
| 97 | + }; |
| 98 | + |
| 99 | + instance.item = mockItem as any; |
| 100 | + instance.currentLocation = mockLocation; |
| 101 | + |
| 102 | + await instance.saveItem(); |
| 103 | + |
| 104 | + expect(mockItem.update).toHaveBeenCalledWith({ LocnVO: mockLocation }); |
| 105 | + |
| 106 | + expect(mockEditService.updateItems).toHaveBeenCalled(); |
| 107 | + |
| 108 | + const clonedArg = mockEditService.updateItems.calls.mostRecent().args[0][0]; |
| 109 | + |
| 110 | + expect(clonedArg.locnId).toBeUndefined(); // make sure it was deleted |
| 111 | + }); |
| 112 | +}); |
0 commit comments