-
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.
Turn checkout service into an Angular service
- Loading branch information
1 parent
33d18a3
commit 2dc3a44
Showing
6 changed files
with
85 additions
and
55 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,24 @@ | ||
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) | ||
import angular from 'angular' | ||
|
||
const serviceName = 'checkoutService' | ||
|
||
class CheckoutService { | ||
/* @ngInject */ | ||
constructor ($window, envService) { | ||
this.$window = $window | ||
this.envService = envService | ||
} | ||
|
||
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) | ||
} | ||
} | ||
} | ||
|
||
export default angular | ||
.module(serviceName, []) | ||
.service(serviceName, CheckoutService) |
87 changes: 50 additions & 37 deletions
87
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 |
---|---|---|
@@ -1,42 +1,55 @@ | ||
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') | ||
}) | ||
import angular from 'angular' | ||
import 'angular-mocks' | ||
|
||
afterEach(() => { | ||
const foundScript = document.getElementById('give-checkout-recaptcha') | ||
if (foundScript) { | ||
document.head.removeChild(foundScript) | ||
} | ||
}) | ||
import module from './checkout.service' | ||
|
||
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() | ||
}) | ||
describe('checkout service', () => { | ||
beforeEach(angular.mock.module(module.name)) | ||
|
||
beforeEach(angular.mock.module(($provide) => { | ||
$provide.value('envService', { | ||
read: () => '123' | ||
}) | ||
})) | ||
|
||
const self = {} | ||
let script | ||
|
||
beforeEach(inject((checkoutService, envService, $window) => { | ||
self.checkoutService = checkoutService | ||
self.envService = envService | ||
self.$window = $window | ||
self.$window.document = document | ||
|
||
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) | ||
script = self.$window.document.createElement('script') | ||
})) | ||
|
||
describe('initializeRecaptcha()', () => { | ||
beforeEach(() => { | ||
script.src = 'https://www.google.com/recaptcha/enterprise.js?render=123' | ||
script.id = 'test-script' | ||
}) | ||
|
||
afterEach(() => { | ||
const foundScript = self.$window.document.getElementById('give-checkout-recaptcha') | ||
if (foundScript) { | ||
self.$window.document.head.removeChild(foundScript) | ||
} | ||
}) | ||
|
||
it('should add a script even if one already exists', () => { | ||
self.$window.document.head.appendChild(script) | ||
self.checkoutService.initializeRecaptcha() | ||
expect(self.$window.document.getElementById('give-checkout-recaptcha')).not.toBeNull() | ||
expect(self.$window.document.getElementById('test-script')).not.toBeNull() | ||
}) | ||
|
||
it('should only add this script once', () => { | ||
script.id = 'give-checkout-recaptcha' | ||
self.$window.document.head.appendChild(script) | ||
expect(self.$window.document.getElementById('give-checkout-recaptcha')).not.toBeNull() | ||
self.checkoutService.initializeRecaptcha() | ||
expect(self.$window.document.querySelectorAll('#give-checkout-recaptcha')).toHaveLength(1) | ||
}) | ||
}) | ||
}) | ||
|