Commit e361ec43 authored by Simon Knox's avatar Simon Knox

Merge branch 'ps-init-recaptcha-script-cleanup-callback' into 'master'

Cleanup callback set in init_recaptcha_script

See merge request gitlab-org/gitlab!52766
parents 8f36323a e67647ef
...@@ -2,9 +2,6 @@ ...@@ -2,9 +2,6 @@
import { memoize } from 'lodash'; import { memoize } from 'lodash';
export const RECAPTCHA_API_URL_PREFIX = 'https://www.google.com/recaptcha/api.js'; export const RECAPTCHA_API_URL_PREFIX = 'https://www.google.com/recaptcha/api.js';
/**
* The name which will be used for the reCAPTCHA script's onload callback
*/
export const RECAPTCHA_ONLOAD_CALLBACK_NAME = 'recaptchaOnloadCallback'; export const RECAPTCHA_ONLOAD_CALLBACK_NAME = 'recaptchaOnloadCallback';
/** /**
...@@ -21,9 +18,7 @@ export const RECAPTCHA_ONLOAD_CALLBACK_NAME = 'recaptchaOnloadCallback'; ...@@ -21,9 +18,7 @@ export const RECAPTCHA_ONLOAD_CALLBACK_NAME = 'recaptchaOnloadCallback';
* *
*/ */
export const initRecaptchaScript = memoize(() => { export const initRecaptchaScript = memoize(() => {
/** // Appends the the reCAPTCHA script tag to the head of document
* Appends the the reCAPTCHA script tag to the head of document
*/
const appendRecaptchaScript = () => { const appendRecaptchaScript = () => {
const script = document.createElement('script'); const script = document.createElement('script');
script.src = `${RECAPTCHA_API_URL_PREFIX}?onload=${RECAPTCHA_ONLOAD_CALLBACK_NAME}&render=explicit`; script.src = `${RECAPTCHA_API_URL_PREFIX}?onload=${RECAPTCHA_ONLOAD_CALLBACK_NAME}&render=explicit`;
...@@ -31,11 +26,14 @@ export const initRecaptchaScript = memoize(() => { ...@@ -31,11 +26,14 @@ export const initRecaptchaScript = memoize(() => {
document.head.appendChild(script); document.head.appendChild(script);
}; };
/**
* Returns a Promise which is fulfilled after the reCAPTCHA script is loaded
*/
return new Promise((resolve) => { return new Promise((resolve) => {
window[RECAPTCHA_ONLOAD_CALLBACK_NAME] = resolve; // This global callback resolves the Promise and is passed by name to the reCAPTCHA script.
window[RECAPTCHA_ONLOAD_CALLBACK_NAME] = (val) => {
// Let's clean up after ourselves. This is also important for testing, because `window` is NOT cleared between tests.
// https://github.com/facebook/jest/issues/1224#issuecomment-444586798.
delete window[RECAPTCHA_ONLOAD_CALLBACK_NAME];
resolve(val);
};
appendRecaptchaScript(); appendRecaptchaScript();
}); });
}); });
......
...@@ -7,19 +7,11 @@ import { ...@@ -7,19 +7,11 @@ import {
describe('initRecaptchaScript', () => { describe('initRecaptchaScript', () => {
afterEach(() => { afterEach(() => {
// NOTE: The DOM is guaranteed to be clean at the start of a new test file, but it isn't cleaned
// between examples within a file, so we need to clean it after each one. See more context here:
// - https://github.com/facebook/jest/issues/1224
// - https://stackoverflow.com/questions/42805128/does-jest-reset-the-jsdom-document-after-every-suite-or-test
//
// Also note as mentioned in https://github.com/facebook/jest/issues/1224#issuecomment-444586798
// that properties of `window` are NOT cleared between test files. So, we are also
// explicitly unsetting it.
document.head.innerHTML = ''; document.head.innerHTML = '';
window[RECAPTCHA_ONLOAD_CALLBACK_NAME] = undefined;
clearMemoizeCache(); clearMemoizeCache();
}); });
const getScriptOnload = () => window[RECAPTCHA_ONLOAD_CALLBACK_NAME];
const triggerScriptOnload = (...args) => window[RECAPTCHA_ONLOAD_CALLBACK_NAME](...args); const triggerScriptOnload = (...args) => window[RECAPTCHA_ONLOAD_CALLBACK_NAME](...args);
describe('when called', () => { describe('when called', () => {
...@@ -51,6 +43,7 @@ describe('initRecaptchaScript', () => { ...@@ -51,6 +43,7 @@ describe('initRecaptchaScript', () => {
triggerScriptOnload(instance); triggerScriptOnload(instance);
await expect(result).resolves.toBe(instance); await expect(result).resolves.toBe(instance);
expect(getScriptOnload()).toBeUndefined();
}); });
}); });
}); });
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment