yzt
2023-05-26 de4278af2fd46705a40bac58ec01122db6b7f3d7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { Cartesian3 } from "../../Source/Cesium.js";
import { GeocodeType } from "../../Source/Cesium.js";
import { PeliasGeocoderService } from "../../Source/Cesium.js";
import { Resource } from "../../Source/Cesium.js";
import { when } from "../../Source/Cesium.js";
 
describe("Core/PeliasGeocoderService", function () {
  it("constructor throws without url", function () {
    expect(function () {
      return new PeliasGeocoderService(undefined);
    }).toThrowDeveloperError();
  });
 
  it("returns geocoder results", function () {
    var service = new PeliasGeocoderService("http://test.invalid/v1/");
 
    var query = "some query";
    var data = {
      features: [
        {
          type: "Feature",
          geometry: {
            type: "Point",
            coordinates: [-75.172489, 39.927828],
          },
          properties: {
            label: "1826 S 16th St, Philadelphia, PA, USA",
          },
        },
      ],
    };
    spyOn(Resource.prototype, "fetchJson").and.returnValue(when.resolve(data));
 
    return service.geocode(query).then(function (results) {
      expect(results.length).toEqual(1);
      expect(results[0].displayName).toEqual(data.features[0].properties.label);
      expect(results[0].destination).toBeInstanceOf(Cartesian3);
    });
  });
 
  it("returns no geocoder results if Pelias has no results", function () {
    var service = new PeliasGeocoderService("http://test.invalid/v1/");
 
    var query = "some query";
    var data = { features: [] };
    spyOn(Resource.prototype, "fetchJson").and.returnValue(when.resolve(data));
 
    return service.geocode(query).then(function (results) {
      expect(results.length).toEqual(0);
    });
  });
 
  it("calls search endpoint if specified", function () {
    var service = new PeliasGeocoderService("http://test.invalid/v1/");
 
    var query = "some query";
    var data = { features: [] };
    spyOn(Resource.prototype, "fetchJson").and.returnValue(when.resolve(data));
    var getDerivedResource = spyOn(
      service._url,
      "getDerivedResource"
    ).and.callThrough();
 
    service.geocode(query, GeocodeType.SEARCH);
    expect(getDerivedResource).toHaveBeenCalledWith({
      url: "search",
      queryParameters: {
        text: query,
      },
    });
  });
 
  it("calls autocomplete endpoint if specified", function () {
    var service = new PeliasGeocoderService("http://test.invalid/v1/");
 
    var query = "some query";
    var data = { features: [] };
    spyOn(Resource.prototype, "fetchJson").and.returnValue(when.resolve(data));
    var getDerivedResource = spyOn(
      service._url,
      "getDerivedResource"
    ).and.callThrough();
 
    service.geocode(query, GeocodeType.AUTOCOMPLETE);
    expect(getDerivedResource).toHaveBeenCalledWith({
      url: "autocomplete",
      queryParameters: {
        text: query,
      },
    });
  });
});