-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0952125
commit b575c87
Showing
8 changed files
with
194 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
'use strict'; | ||
|
||
jest.dontMock('../../utils'); | ||
|
||
describe('Utils', function () { | ||
|
||
var Utils = undefined; | ||
|
||
beforeEach(function () { | ||
delete window.google; | ||
delete window.mapsCallback; | ||
Utils = require('../../utils'); | ||
}); | ||
|
||
it('registers the callbacks if google does not exist', function () { | ||
expect(Utils.callbacks.length).toBe(0); | ||
Utils.loadMaps(null, function () {}); | ||
expect(Utils.callbacks.length).toBe(1); | ||
}); | ||
|
||
it('adds the script if not added', function () { | ||
expect(Utils.added).toBe(false); | ||
Utils.loadMaps(null, function () {}); | ||
expect(Utils.added).toBe(true); | ||
}); | ||
|
||
it('fires the callback if google exists', function () { | ||
window.google = {}; | ||
var callback = jest.genMockFunction(); | ||
Utils.loadMaps(null, callback); | ||
jest.runAllTimers(); | ||
expect(callback).toBeCalled(); | ||
}); | ||
|
||
it('fires the callbacks on mapsCallback', function () { | ||
var callback = jest.genMockFunction(); | ||
Utils.loadMaps(null, callback); | ||
window.mapsCallback(); | ||
expect(callback).toBeCalled(); | ||
}); | ||
|
||
it('deletes the global mapsCallback', function () { | ||
window.mapsCallback = {}; | ||
Utils.mapsCallback(); | ||
expect(window.mapsCallback).toBeUndefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, '__esModule', { | ||
value: true | ||
}); | ||
exports['default'] = { | ||
|
||
callbacks: [], | ||
|
||
added: false, | ||
|
||
loadMaps: function loadMaps(libraries, callback) { | ||
if (!window.google) { | ||
this.callbacks.push(callback); | ||
if (!this.added) { | ||
window.mapsCallback = this.mapsCallback.bind(this); | ||
this.addScript(libraries); | ||
} | ||
} else { | ||
console.log('sss'); | ||
setTimeout(callback); | ||
} | ||
}, | ||
|
||
addScript: function addScript(libraries) { | ||
var src = 'https://maps.googleapis.com/maps/api/js'; | ||
src += '?callback=mapsCallback'; | ||
src += '&libraries=' + (libraries || ''); | ||
var script = document.createElement('script'); | ||
script.setAttribute('src', src); | ||
document.head.appendChild(script); | ||
this.added = true; | ||
}, | ||
|
||
mapsCallback: function mapsCallback() { | ||
delete window.mapsCallback; | ||
this.callbacks.forEach(function (callback) { | ||
return callback(); | ||
}); | ||
this.callbacks = []; | ||
} | ||
|
||
}; | ||
module.exports = exports['default']; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
jest.dontMock('../../utils'); | ||
|
||
describe('Utils', () => { | ||
|
||
let Utils; | ||
|
||
beforeEach(() => { | ||
delete window.google; | ||
delete window.mapsCallback; | ||
Utils = require('../../utils'); | ||
}); | ||
|
||
it('registers the callbacks if google does not exist', () => { | ||
expect(Utils.callbacks.length).toBe(0); | ||
Utils.loadMaps(null, () => {}); | ||
expect(Utils.callbacks.length).toBe(1); | ||
}); | ||
|
||
it('adds the script if not added', () => { | ||
expect(Utils.added).toBe(false); | ||
Utils.loadMaps(null, () => {}); | ||
expect(Utils.added).toBe(true); | ||
}); | ||
|
||
it('fires the callback if google exists', () => { | ||
window.google = {}; | ||
const callback = jest.genMockFunction(); | ||
Utils.loadMaps(null, callback); | ||
jest.runAllTimers(); | ||
expect(callback).toBeCalled(); | ||
}); | ||
|
||
it('fires the callbacks on mapsCallback', () => { | ||
const callback = jest.genMockFunction(); | ||
Utils.loadMaps(null, callback); | ||
window.mapsCallback(); | ||
expect(callback).toBeCalled(); | ||
}); | ||
|
||
it('deletes the global mapsCallback', () => { | ||
window.mapsCallback = {}; | ||
Utils.mapsCallback(); | ||
expect(window.mapsCallback).toBeUndefined(); | ||
}); | ||
|
||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
export default { | ||
|
||
callbacks: [], | ||
|
||
added: false, | ||
|
||
loadMaps(libraries, callback) { | ||
if (!window.google) { | ||
this.callbacks.push(callback); | ||
if (!this.added) { | ||
window.mapsCallback = this.mapsCallback.bind(this); | ||
this.addScript(libraries); | ||
} | ||
} else { | ||
setTimeout(callback); | ||
} | ||
}, | ||
|
||
addScript(libraries) { | ||
let src = 'https://maps.googleapis.com/maps/api/js'; | ||
src += '?callback=mapsCallback'; | ||
src += `&libraries=${libraries || ''}`; | ||
const script = document.createElement('script'); | ||
script.setAttribute('src', src); | ||
document.head.appendChild(script); | ||
this.added = true; | ||
}, | ||
|
||
mapsCallback() { | ||
delete window.mapsCallback; | ||
this.callbacks.forEach(callback => callback()); | ||
this.callbacks = []; | ||
} | ||
|
||
}; |