Skip to content
This repository has been archived by the owner on Aug 17, 2021. It is now read-only.

Commit

Permalink
Check for grecaptcha.render function availability
Browse files Browse the repository at this point in the history
Instead of checking for the variable `grecaptcha` to be defined, now we check for the `grecaptcha.render` function to be defined and to be a function.

It has happened in Firefox(due to browser cache), that sometimes the `grecaptcha` variable is already defined, but as empty object, causing the library to fail due to the lack of the render function.
  • Loading branch information
Ismael Ambrosi committed May 3, 2018
1 parent eca1c7f commit 7d98663
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/service.js
Expand Up @@ -139,8 +139,8 @@
}


// Check if grecaptcha is not defined already.
if (ng.isDefined($window.grecaptcha)) {
// Check if grecaptcha.render is not defined already.
if (ng.isFunction(($window.grecaptcha || {}).render)) {
callback();
} else if ($window.document.querySelector('script[src^="https://www.google.com/recaptcha/api.js"]')) {
// wait for script to be loaded.
Expand Down
29 changes: 29 additions & 0 deletions tests/service_test.js
Expand Up @@ -90,6 +90,35 @@ describe('service', function () {
});
});

// Regresion test for https://git.io/vp2SO
describe('without loaded api, loaded grecaptcha from cache without render func', () => {
const grecaptchaMock = {};
const _key = '1234567890123456789012345678901234567890';

beforeEach(function () {
const doc = [{
querySelector: () => ({}),
}];

driver
.given.apiLoaded(grecaptchaMock)
.given.mockDocument(doc)
.when.created();
});

it('should not try to render recaptcha', () => {
const spy = jasmine.createSpy('recaptchaCreate');

driver.service.create('<div></div>', {
key: _key,
callback: spy
});

expect(() => driver.applyChanges()).not.toThrow();
expect(spy).not.toHaveBeenCalled();
});
});

describe('without loaded api, with script tag', function () {
var createElement,
appendSpy,
Expand Down

0 comments on commit 7d98663

Please sign in to comment.