yzt
2023-05-26 2f70f6727314edd84d8ec2bfe3ce832803f1ea77
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
import { OpenCageGeocoderService } from "../../Source/Cesium.js";
import { Resource } from "../../Source/Cesium.js";
import { when } from "../../Source/Cesium.js";
 
describe("Core/OpenCageGeocoderService", function () {
  var endpoint = "https://api.opencagedata.com/geocode/v1/";
  var apiKey = "c2a490d593b14612aefa6ec2e6b77c47";
 
  it("constructor throws without url", function () {
    expect(function () {
      return new OpenCageGeocoderService(undefined);
    }).toThrowDeveloperError();
  });
 
  it("constructor throws without API Key", function () {
    expect(function () {
      return new OpenCageGeocoderService(endpoint, undefined);
    }).toThrowDeveloperError();
  });
 
  it("returns geocoder results", function () {
    var service = new OpenCageGeocoderService(endpoint, apiKey);
 
    var query = "-22.6792,+14.5272";
    var data = {
      results: [
        {
          bounds: {
            northeast: {
              lat: -22.6790826,
              lng: 14.5269016,
            },
            southwest: {
              lat: -22.6792826,
              lng: 14.5267016,
            },
          },
          formatted: "Beryl's Restaurant, Woermann St, Swakopmund, Namibia",
          geometry: {
            lat: -22.6795394,
            lng: 14.5276006,
          },
        },
      ],
    };
    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.results[0].formatted);
      expect(results[0].destination).toBeDefined();
    });
  });
 
  it("returns no geocoder results if OpenCage has no results", function () {
    var service = new OpenCageGeocoderService(endpoint, apiKey);
 
    var query = "";
    var data = { results: [] };
    spyOn(Resource.prototype, "fetchJson").and.returnValue(when.resolve(data));
 
    return service.geocode(query).then(function (results) {
      expect(results.length).toEqual(0);
    });
  });
});