Skip to content

Commit

Permalink
Add tests for LatLngBounds
Browse files Browse the repository at this point in the history
  • Loading branch information
NotWoods committed Mar 28, 2019
1 parent 0499d24 commit 0d08b43
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 11 deletions.
10 changes: 7 additions & 3 deletions index.d.ts
Expand Up @@ -12,7 +12,7 @@ export function toDegrees(radians: number): number;

export function toRadians(angleDegrees: number): number;

export class LatLng {
export class LatLng implements Iterable<number> {
/**
* @param lat Latitude
* @param lng Longitude
Expand Down Expand Up @@ -73,6 +73,10 @@ export class LatLng {
* @param precision Number of decimal places.
*/
toUrlValue(precision?: number): string;

[Symbol.iterator](): Iterator<number>;

readonly [index: number]: number;
}

/**
Expand Down Expand Up @@ -226,10 +230,10 @@ export class LatLngBounds {

/**
* Check if two bounds are equal.
* @param {LatLngBounds} other
* @param {LatLngBounds | LatLngBoundsLiteral} other
* @returns {boolean}
*/
equals(other: LatLngBounds): boolean;
equals(other: LatLngBounds | LatLngBoundsLiteral): boolean;

/**
* Mutate the bounds to include the given point.
Expand Down
26 changes: 20 additions & 6 deletions src/latlng-bounds.js
Expand Up @@ -30,15 +30,29 @@ export default class LatLngBounds {

/**
* Check if two bounds are equal.
* @param {LatLngBounds} other
* @param {LatLngBounds | LatLngBoundsLiteral} other
* @returns {boolean}
*/
equals(other) {
if (other instanceof LatLngBounds) {
if (!other) {
return false;
} else if (other instanceof LatLngBounds) {
return (
equals(this.sw, other.getSouthWest()) &&
equals(this.ne, other.getNorthEast())
);
} else if (
[other.north, other.south, other.east, other.west].every(
n => typeof n === 'number'
)
) {
const literal = this.toJSON();
return (
other.north === literal.north &&
other.south === literal.south &&
other.east === literal.east &&
other.west === literal.west
);
} else {
return false;
}
Expand Down Expand Up @@ -108,10 +122,10 @@ export default class LatLngBounds {
*/
toJSON() {
return {
east: this.ne.lat(),
north: this.ne.lng(),
south: this.sw.lng(),
west: this.sw.lat(),
east: this.ne.lng(),
north: this.ne.lat(),
south: this.sw.lat(),
west: this.sw.lng(),
};
}

Expand Down
16 changes: 16 additions & 0 deletions src/latlng.js
Expand Up @@ -186,4 +186,20 @@ export default class LatLng {
parseFloat(this[LNG].toFixed(precision))
);
}

[Symbol.iterator]() {
let i = 0;
return {
next: () => {
if (i < this.length) {
return { value: this[i++], done: false };
} else {
return { done: true };
}
},
[Symbol.iterator]() {
return this;
},
};
}
}
9 changes: 9 additions & 0 deletions test/LatLng.test.js
Expand Up @@ -73,6 +73,15 @@ describe('LatLng', () => {
expect(place.lng()).toBe(place.longitude);
}
});

it('should return an iterator', () => {
let i = 0;
for (const n of places.london) {
expect(n).toEqual(places.london[i]);
i++;
}
expect(i).toEqual(2);
});
});

describe('equalLatLngs', () => {
Expand Down
79 changes: 79 additions & 0 deletions test/LatLngBounds.test.js
@@ -0,0 +1,79 @@
const { LatLngBounds } = require('../');
const places = require('./data/places');
const googleMaps = require('./data/google-maps.json');

describe('LatLngBounds', () => {
it('should construct bounds', () => {
expect(() => new LatLngBounds()).not.toThrow();
expect(() => new LatLngBounds([0, 0])).not.toThrow();
expect(() => new LatLngBounds([0, 0], [1, 1])).not.toThrow();
});

it('should convert to string like Google Maps', () => {
expect(places.hawaii.toString()).toBe(googleMaps['bounds.toString()']);
expect(String(places.hawaii)).toBe(googleMaps['bounds.toString()']);
});

it('should convert to URL value like Google Maps', () => {
expect(places.hawaii.toUrlValue()).toBe(
googleMaps['bounds.toUrlValue()']
);
expect(places.hawaii.toUrlValue(3)).toBe(
googleMaps['bounds.toUrlValue(3)']
);
});

it('should be equal to itself', () => {
expect(places.hawaii.equals(places.hawaii)).toBe(true);
expect(places.hawaii.equals(places.bigIsland)).toBe(false);
});

it('should be equal to LatLngBoundsLiteral version', () => {
expect(places.hawaii.equals(places.hawaii.toJSON())).toBe(true);
expect(places.hawaii.equals(places.bigIsland.toJSON())).toBe(false);
});

it('should return true if a point is contained in the bounds', () => {
const corner = places.oahu.getNorthEast();
const outerCorner = places.bigIsland.getSouthWest();
expect(places.hawaii.contains(corner)).toBe(true);
expect(places.hawaii.contains(outerCorner)).toBe(false);
});

it('should return true if bounds intersect', () => {
expect(places.hawaii.intersects(places.bigIsland)).toBe(true);
expect(places.hawaii.intersects(places.oahu)).toBe(true);
});

it('should return true if empty', () => {
expect(new LatLngBounds([0, 0], [0, 0]).isEmpty()).toBe(true);
expect(places.hawaii.isEmpty()).toBe(false);
});

it('should be able to extend its bounds', () => {
const oahu1 = new LatLngBounds(
places.oahu.getSouthWest(),
places.oahu.getNorthEast()
);
expect(oahu1.extend(places.newyork).toJSON()).toEqual(
googleMaps['bounds.extend()']
);
});

it('should be able to find the union of two bounds', () => {
const hawaii1 = new LatLngBounds(
places.hawaii.getSouthWest(),
places.hawaii.getNorthEast()
);
const hawaii2 = new LatLngBounds(
places.hawaii.getSouthWest(),
places.hawaii.getNorthEast()
);
expect(hawaii1.union(places.oahu).toJSON()).toEqual(
googleMaps['bounds.union(oahu)']
);
expect(hawaii2.union(places.bigIsland).toJSON()).toEqual(
googleMaps['bounds.union(bigIsland)']
);
});
});
30 changes: 30 additions & 0 deletions test/data/google-maps.html
Expand Up @@ -16,8 +16,32 @@
new google.maps.LatLng(44.39450050731302, -79.69500459730625),
new google.maps.LatLng(44.39456351370196, -79.6948466822505),
],
bigIsland: new google.maps.LatLngBounds(
new google.maps.LatLng(18.719955, -156.007002),
new google.maps.LatLng(20.297632, -154.714628)
),
hawaii: new google.maps.LatLngBounds(
new google.maps.LatLng(18.850912, -160.66249),
new google.maps.LatLng(22.240875, -153.791741)
),
oahu: new google.maps.LatLngBounds(
new google.maps.LatLng(21.235191, -158.326847),
new google.maps.LatLng(21.744683, -157.57099)
),
};

const oahu1 = new google.maps.LatLngBounds(
googlePlaces.oahu.getSouthWest(),
googlePlaces.oahu.getNorthEast()
);
const hawaii1 = new google.maps.LatLngBounds(
googlePlaces.hawaii.getSouthWest(),
googlePlaces.hawaii.getNorthEast()
);
const hawaii2 = new google.maps.LatLngBounds(
googlePlaces.hawaii.getSouthWest(),
googlePlaces.hawaii.getNorthEast()
);
const results = {
'toString()': googlePlaces.sydney.toString(),
'toUrlValue()': googlePlaces.sydney.toUrlValue(),
Expand Down Expand Up @@ -67,6 +91,12 @@
'computeArea(...path)': google.maps.geometry.spherical.computeArea(
googlePlaces.path
),
'bounds.toString()': googlePlaces.hawaii.toString(),
'bounds.toUrlValue()': googlePlaces.hawaii.toUrlValue(),
'bounds.toUrlValue(3)': googlePlaces.hawaii.toUrlValue(3),
'bounds.extend()': oahu1.extend(googlePlaces.newyork),
'bounds.union(oahu)': hawaii1.union(googlePlaces.oahu),
'bounds.union(bigIsland)': hawaii2.union(googlePlaces.bigIsland),
};

console.log(JSON.stringify(results));
Expand Down
23 changes: 22 additions & 1 deletion test/data/google-maps.json
Expand Up @@ -20,5 +20,26 @@
"lat": -9.918509273080291,
"lng": -169.58278370355396
},
"computeArea(...path)": 123.11307432762369
"computeArea(...path)": 123.11307432762369,
"bounds.toString()": "((18.850912, -160.66249), (22.240875, -153.791741))",
"bounds.toUrlValue()": "18.850912,-160.66249,22.240875,-153.791741",
"bounds.toUrlValue(3)": "18.851,-160.662,22.241,-153.792",
"bounds.extend()": {
"south": 21.235191,
"west": -158.326847,
"north": 40.71417,
"east": -74.00639000000001
},
"bounds.union(oahu)": {
"south": 18.850912,
"west": -160.66249,
"north": 22.240875,
"east": -153.791741
},
"bounds.union(bigIsland)": {
"south": 18.719955,
"west": -160.66249,
"north": 22.240875,
"east": -153.791741
}
}
14 changes: 13 additions & 1 deletion test/data/places.js
@@ -1,4 +1,4 @@
const { LatLng } = require('../../');
const { LatLng, LatLngBounds } = require('../../');

module.exports = {
donostia: new LatLng(43.320812, -1.984447),
Expand All @@ -13,4 +13,16 @@ module.exports = {
new LatLng(44.39450050731302, -79.69500459730625),
new LatLng(44.39456351370196, -79.6948466822505),
],
bigIsland: new LatLngBounds(
new LatLng(18.719955, -156.007002),
new LatLng(20.297632, -154.714628)
),
hawaii: new LatLngBounds(
new LatLng(18.850912, -160.66249),
new LatLng(22.240875, -153.791741)
),
oahu: new LatLngBounds(
new LatLng(21.235191, -158.326847),
new LatLng(21.744683, -157.57099)
),
};

0 comments on commit 0d08b43

Please sign in to comment.