-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate out the creation of the Recaptcha script so we can put it on…
… pages that the action is not being run on
- Loading branch information
1 parent
ae87a95
commit 517e012
Showing
8 changed files
with
73 additions
and
67 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
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
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,8 @@ | ||
export function initializeRecaptcha () { | ||
const script = this.$window.document.createElement('script') | ||
script.src = `https://www.google.com/recaptcha/enterprise.js?render=${this.envService.read('recaptchaKey')}` | ||
script.id = 'give-checkout-recaptcha' | ||
if (!this.$window.document.getElementById(script.id)) { | ||
this.$window.document.head.appendChild(script) | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/common/services/checkoutHelpers/checkout.service.spec.js
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,42 @@ | ||
import * as checkoutService from './checkout.service' | ||
|
||
describe('initializeRecaptcha()', () => { | ||
const $ctrl = { | ||
$window: { | ||
document: document | ||
}, | ||
envService: { | ||
read: jest.fn() | ||
} | ||
} | ||
const script = document.createElement('script') | ||
|
||
beforeEach(() => { | ||
script.src = 'https://www.google.com/recaptcha/enterprise.js?render=123' | ||
script.id = 'test-script' | ||
$ctrl.envService.read.mockReturnValue('123') | ||
}) | ||
|
||
afterEach(() => { | ||
const foundScript = document.getElementById('give-checkout-recaptcha') | ||
if (foundScript) { | ||
document.head.removeChild(foundScript) | ||
} | ||
}) | ||
|
||
it('should add a script even if one already exists', () => { | ||
document.head.appendChild(script) | ||
checkoutService.initializeRecaptcha.call($ctrl) | ||
expect(document.getElementById('give-checkout-recaptcha')).not.toBeNull() | ||
expect(document.getElementById('test-script')).not.toBeNull() | ||
}) | ||
|
||
it('should only add this script once', () => { | ||
script.id = 'give-checkout-recaptcha' | ||
document.head.appendChild(script) | ||
expect(document.getElementById('give-checkout-recaptcha')).not.toBeNull() | ||
checkoutService.initializeRecaptcha.call($ctrl) | ||
expect(document.querySelectorAll('#give-checkout-recaptcha')).toHaveLength(1) | ||
}) | ||
}) | ||
|