0

我想爲getAddress()寫一個單元測試,該函數從google.maps.places.autocomplete獲取地址。要觸發該功能,我需要以編程方式設置一個新位置,因爲place_changed事件處理程序調用getAddress()。當使用GUI設置地點時location.lat是一個功能,當以編程方式進行時,它是一個屬性。location.lat不是一個函數

所以要設置一個新的地方,我這樣做:

let place = { "address_components": [{ "long_name": "Auckland", "short_name": "Auckland", "types": ["locality", "political"] }, { "long_name": "Auckland", "short_name": "Auckland", "types": ["administrative_area_level_2", "political"] }, { "long_name": "Auckland", "short_name": "Auckland", "types": ["administrative_area_level_1", "political"] }, { "long_name": "New Zealand", "short_name": "NZ", "types": ["country", "political"] }], "adr_address": "<span class=\"locality\">Auckland</span>, <span class=\"country-name\">New Zealand</span>", "formatted_address": "Auckland, New Zealand", "geometry": { "location": { "lat": -36.8484597, "lng": 174.76333150000005 }, "viewport": { "south": -37.0654751, "west": 174.44380160000003, "north": -36.660571, "east": 175.2871371 } }, "icon": "https://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png", "id": "088418ddc17fef2513462d92dbee1355929b35ed", "name": "Auckland", "photos": [{ "height": 1836, "html_attributions": ["<a href=\"https://maps.google.com/maps/contrib/111644522305605838507/photos\">Johannes De Smedt</a>"], "width": 3264 }, { "height": 2988, "html_attributions": ["<a href=\"https://maps.google.com/maps/contrib/104449230804367883642/photos\">Michal Panek</a>"], "width": 5312 }, { "height": 3096, "html_attributions": ["<a href=\"https://maps.google.com/maps/contrib/115717451821958551526/photos\">Sakchhyam Malla</a>"], "width": 4128 }, { "height": 3672, "html_attributions": ["<a href=\"https://maps.google.com/maps/contrib/100726048745034015308/photos\">Maria Bitunjac</a>"], "width": 4896 }, { "height": 1836, "html_attributions": ["<a href=\"https://maps.google.com/maps/contrib/112349734260069492163/photos\">Carol Prichard</a>"], "width": 3264 }, { "height": 3120, "html_attributions": ["<a href=\"https://maps.google.com/maps/contrib/106356366323887392782/photos\">Kovács György</a>"], "width": 4208 }, { "height": 1504, "html_attributions": ["<a href=\"https://maps.google.com/maps/contrib/117137027694384717244/photos\">Martin Mobers</a>"], "width": 2006 }, { "height": 492, "html_attributions": ["<a href=\"https://maps.google.com/maps/contrib/102033645464260143092/photos\">Priyesh Bhavsar</a>"], "width": 1000 }, { "height": 2988, "html_attributions": ["<a href=\"https://maps.google.com/maps/contrib/103221298159348509467/photos\">Alvie Granito</a>"], "width": 5312 }, { "height": 3265, "html_attributions": ["<a href=\"https://maps.google.com/maps/contrib/100418135834547049744/photos\">Simon Chen</a>"], "width": 4898 }], "place_id": "ChIJ--acWvtHDW0RF5miQ2HvAAU", "reference": "CmRbAAAAEJGwhQ0l3O1QapXvbJ_s-xNVyh7UxBjd89D9Q860dfhb5Xh3xeY95UT1tIdugiTRhhZHT2qk_wzUqfd3wPS-vdX1pBljxcPtMUqhTtslzMyVccViA9ckk50Xv_cFWNXFEhCPkvW84Okrk2SJUpUDLGz8GhSEN1dm_0tJj4nEHYo0-bkRdOykYQ", "scope": "GOOGLE", "types": ["locality", "political"], "url": "https://maps.google.com/?q=Auckland,+New+Zealand&ftid=0x6d0d47fb5a9ce6fb:0x500ef6143a29917", "utc_offset": 780, "vicinity": "Auckland" } 

     testHost.areaPicker.autocomplete.set("place", place); 

關於我的地方可變我得到這個編譯時錯誤:

'"adr_address"' does not exist in type 'PlaceResult'.

但是當我這樣做,由於某種原因造成地點對象的geometry.latgeometry.lng不再是功能,但現在它們是屬性。

但是,當我在真實場景中使用自動完成功能時,它們是功能。他們應該是。我如何讓他們在我的單元測試中成爲功能?下面的完整代碼。

單元測試:

it('EmitAreaChanged_AreaEntered_AreaPlaceValueMakesItToHost',() => { 
     let place = { "address_components": [{ "long_name": "Auckland", "short_name": "Auckland", "types": ["locality", "political"] }, { "long_name": "Auckland", "short_name": "Auckland", "types": ["administrative_area_level_2", "political"] }, { "long_name": "Auckland", "short_name": "Auckland", "types": ["administrative_area_level_1", "political"] }, { "long_name": "New Zealand", "short_name": "NZ", "types": ["country", "political"] }], "adr_address": "<span class=\"locality\">Auckland</span>, <span class=\"country-name\">New Zealand</span>", "formatted_address": "Auckland, New Zealand", "geometry": { "location": { "lat": -36.8484597, "lng": 174.76333150000005 }, "viewport": { "south": -37.0654751, "west": 174.44380160000003, "north": -36.660571, "east": 175.2871371 } }, "icon": "https://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png", "id": "088418ddc17fef2513462d92dbee1355929b35ed", "name": "Auckland", "photos": [{ "height": 1836, "html_attributions": ["<a href=\"https://maps.google.com/maps/contrib/111644522305605838507/photos\">Johannes De Smedt</a>"], "width": 3264 }, { "height": 2988, "html_attributions": ["<a href=\"https://maps.google.com/maps/contrib/104449230804367883642/photos\">Michal Panek</a>"], "width": 5312 }, { "height": 3096, "html_attributions": ["<a href=\"https://maps.google.com/maps/contrib/115717451821958551526/photos\">Sakchhyam Malla</a>"], "width": 4128 }, { "height": 3672, "html_attributions": ["<a href=\"https://maps.google.com/maps/contrib/100726048745034015308/photos\">Maria Bitunjac</a>"], "width": 4896 }, { "height": 1836, "html_attributions": ["<a href=\"https://maps.google.com/maps/contrib/112349734260069492163/photos\">Carol Prichard</a>"], "width": 3264 }, { "height": 3120, "html_attributions": ["<a href=\"https://maps.google.com/maps/contrib/106356366323887392782/photos\">Kovács György</a>"], "width": 4208 }, { "height": 1504, "html_attributions": ["<a href=\"https://maps.google.com/maps/contrib/117137027694384717244/photos\">Martin Mobers</a>"], "width": 2006 }, { "height": 492, "html_attributions": ["<a href=\"https://maps.google.com/maps/contrib/102033645464260143092/photos\">Priyesh Bhavsar</a>"], "width": 1000 }, { "height": 2988, "html_attributions": ["<a href=\"https://maps.google.com/maps/contrib/103221298159348509467/photos\">Alvie Granito</a>"], "width": 5312 }, { "height": 3265, "html_attributions": ["<a href=\"https://maps.google.com/maps/contrib/100418135834547049744/photos\">Simon Chen</a>"], "width": 4898 }], "place_id": "ChIJ--acWvtHDW0RF5miQ2HvAAU", "reference": "CmRbAAAAEJGwhQ0l3O1QapXvbJ_s-xNVyh7UxBjd89D9Q860dfhb5Xh3xeY95UT1tIdugiTRhhZHT2qk_wzUqfd3wPS-vdX1pBljxcPtMUqhTtslzMyVccViA9ckk50Xv_cFWNXFEhCPkvW84Okrk2SJUpUDLGz8GhSEN1dm_0tJj4nEHYo0-bkRdOykYQ", "scope": "GOOGLE", "types": ["locality", "political"], "url": "https://maps.google.com/?q=Auckland,+New+Zealand&ftid=0x6d0d47fb5a9ce6fb:0x500ef6143a29917", "utc_offset": 780, "vicinity": "Auckland" } 

     testHost.areaPicker.autocomplete.set("place", place); 
     expect(testHost.placeValue).toBe(true); 
    }); 
下的測試,使用谷歌的自動完成我的組件的

相關部分:

ngAfterViewInit() { 
    this.autocomplete = new google.maps.places.Autocomplete(
     <HTMLInputElement>this.areaInput.nativeElement, this._options); 
    this.autocomplete.addListener('place_changed',() => { 
     this.getAddress(this.autocomplete) 
    }); 
    this.areaInput.nativeElement.setCustomValidity(""); 
} 

public getAddress(autocomplete: google.maps.places.Autocomplete): void { 
    if (this._canGetAddress) { 
     var place: google.maps.places.PlaceResult = autocomplete.getPlace(); 
     this.address = place['formatted_address']; 
     var location = place['geometry']['location']; 
     var lat = location.lat(); 
     var lng = location.lng(); 
     this.onAreaChanged.emit({areaEntered: true, place: place}); 
     this.areaInput.nativeElement.setCustomValidity(""); 
    } 
} 

這裏是我的代碼,我想測試:

public getAddress(autocomplete: google.maps.places.Autocomplete): void { 
     if (this._canGetAddress) { 
      var place: google.maps.places.PlaceResult = autocomplete.getPlace(); 
      this.address = place['formatted_address']; 
      var location = place['geometry']['location']; 
      var lat = location.lat(); 
      var lng = location.lng(); 
      this.onAreaChanged.emit({areaEntered: true, place: place}); 
      this.areaInput.nativeElement.setCustomValidity(""); 
     } 
    } 

回答

1

該位置需要是一個google.maps.LatLng對象。

+0

我該如何做到這一點?我只是使用'autocomplete.getPlace()'產生的'google.maps.places.PlaceResult' – BeniaminoBaggins