This repository was archived by the owner on Sep 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathgoogleApi.test.js
More file actions
81 lines (66 loc) · 2.64 KB
/
googleApi.test.js
File metadata and controls
81 lines (66 loc) · 2.64 KB
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
import sinon from 'sinon';
import GoogleApi from '../../js/googleApi.js';
describe('googleApi', function() {
describe('can geocode', function() {
let geocodeRequest;
beforeEach(function() {
geocodeRequest = sinon.stub(GoogleApi, 'geocodeRequest').returns('yo');
});
afterEach(function() {
GoogleApi.geocodeRequest.restore();
});
it ('position', async function() {
let ret = await GoogleApi.geocodePosition('myKey', {lat: 1.234, lng: 1.14});
expect(geocodeRequest).to.have.been.calledWith(
'https://maps.google.com/maps/api/geocode/json?key=myKey&latlng=1.234,1.14');
expect(ret).to.eql('yo');
});
it ('position', async function() {
let ret = await GoogleApi.geocodePosition('myKey', {lat: 0, lng: 0});
expect(geocodeRequest).to.have.been.calledWith(
'https://maps.google.com/maps/api/geocode/json?key=myKey&latlng=0,0');
expect(ret).to.eql('yo');
});
it ('address', async function() {
let ret = await GoogleApi.geocodeAddress('myKey', "london");
expect(geocodeRequest).to.have.been.calledWith(
'https://maps.google.com/maps/api/geocode/json?key=myKey&address=london');
expect(ret).to.eql('yo');
});
});
describe('geocodeRequest', function() {
function mockFetch(ret) {
global.fetch = sinon.stub().returns(Promise.resolve({
json: () => ret
}))
}
it ('throws if invalid results', function() {
mockFetch({ status: "NOT_OK" });
return GoogleApi.geocodeRequest().then(
() => { throw new Error('cannot be there') },
(err) => { expect(err.message).to.contain("NOT_OK"); }
);
});
describe('returns formatted results', function() {
it ('for waterloo-bridge', async function() {
mockFetch(require('./fixtures/waterloo-bridge.js'));
let [first, ...ret] = await GoogleApi.geocodeRequest();
expect(first.countryCode).to.eql('GB');
expect(first.feature).to.be.eql(null);
expect(first.locality).to.eql('London');
expect(first.streetName).to.eql('Waterloo Bridge');
expect(first.streetNumber).to.eql('76');
});
it ('for yosemite park', async function() {
mockFetch(require('./fixtures/yosemite-park.js'));
let [first, ...ret] = await GoogleApi.geocodeRequest();
expect(first.countryCode).to.eql('US');
expect(first.feature).to.be.eql('Yosemite National Park');
expect(first.streetName).to.be.eql(null);
expect(first.streetNumber).to.be.eql(null);
expect(first.postalCode).to.be.eql(null);
expect(first.locality).to.be.eql(null);
});
});
});
});