Blame view

node_modules/loopback-datasource-juggler/test/geo.test.js 3.69 KB
f7563de62   Palak Handa   first commit
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
  // Copyright IBM Corp. 2014,2016. All Rights Reserved.
  // Node module: loopback-datasource-juggler
  // This file is licensed under the MIT License.
  // License text available at https://opensource.org/licenses/MIT
  'use strict';
  
  /*global describe,it*/
  /*jshint expr:true */
  
  require('should');
  
  var GeoPoint = require('../lib/geo').GeoPoint;
  var DELTA = 0.0000001;
  
  describe('GeoPoint', function() {
    describe('constructor', function() {
      it('should support a valid array', function() {
        var point = new GeoPoint([-34, 150]);
  
        point.lat.should.equal(-34);
        point.lng.should.equal(150);
      });
  
      it('should support a valid object', function() {
        var point = new GeoPoint({lat: -34, lng: 150});
  
        point.lat.should.equal(-34);
        point.lng.should.equal(150);
      });
  
      it('should support valid string geo coordinates', function() {
        var point = new GeoPoint('-34,150');
  
        point.lat.should.equal(-34);
        point.lng.should.equal(150);
      });
  
      it('should support coordinates as inline parameters', function() {
        var point = new GeoPoint(-34, 150);
  
        point.lat.should.equal(-34);
        point.lng.should.equal(150);
      });
  
      it('should reject invalid parameters', function() {
        /*jshint -W024 */
        var fn = function() {
          new GeoPoint('150,-34');
        };
        fn.should.throw();
  
        fn = function() {
          new GeoPoint('invalid_string');
        };
        fn.should.throw();
  
        fn = function() {
          new GeoPoint([150, -34]);
        };
        fn.should.throw();
  
        fn = function() {
          new GeoPoint({
            lat: 150,
            lng: null,
          });
        };
        fn.should.throw();
  
        fn = function() {
          new GeoPoint(150, -34);
        };
        fn.should.throw();
  
        fn = function() {
          new GeoPoint();
        };
        fn.should.throw();
      });
    });
  
    describe('toString()', function() {
      it('should return a string in the form "lat,lng"', function() {
        var point = new GeoPoint({lat: -34, lng: 150});
        point.toString().should.equal('-34,150');
      });
    });
  
    describe('distance calculation between two points', function() {
      var here = new GeoPoint({lat: 40.77492964101182, lng: -73.90950187151662});
      var there = new GeoPoint({lat: 40.7753227, lng: -73.909217});
  
      it('should return value in miles by default', function() {
        var distance = GeoPoint.distanceBetween(here, there);
        distance.should.be.a.Number;
        distance.should.be.approximately(0.03097916611592679, DELTA);
      });
  
      it('should return value using specified unit', function() {
        /* Supported units:
         * - `radians`
         * - `kilometers`
         * - `meters`
         * - `miles`
         * - `feet`
         * - `degrees`
         */
  
        var distance = here.distanceTo(there, {type: 'radians'});
        distance.should.be.a.Number;
        distance.should.be.approximately(0.000007825491914348416, DELTA);
  
        distance = here.distanceTo(there, {type: 'kilometers'});
        distance.should.be.a.Number;
        distance.should.be.approximately(0.04985613511367009, DELTA);
  
        distance = here.distanceTo(there, {type: 'meters'});
        distance.should.be.a.Number;
        distance.should.be.approximately(49.856135113670085, DELTA);
  
        distance = here.distanceTo(there, {type: 'miles'});
        distance.should.be.a.Number;
        distance.should.be.approximately(0.03097916611592679, DELTA);
  
        distance = here.distanceTo(there, {type: 'feet'});
        distance.should.be.a.Number;
        distance.should.be.approximately(163.56999709209347, DELTA);
  
        distance = here.distanceTo(there, {type: 'degrees'});
        distance.should.be.a.Number;
        distance.should.be.approximately(0.0004483676593058972, DELTA);
      });
    });
  });