diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-02-18 13:34:06 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-02-18 13:34:06 +0300 |
commit | 859a6fb938bb9ee2a317c46dfa4fcc1af49608f0 (patch) | |
tree | d7f2700abe6b4ffcb2dcfc80631b2d87d0609239 /spec/frontend/captcha/init_recaptcha_script_spec.js | |
parent | 446d496a6d000c73a304be52587cd9bbc7493136 (diff) |
Add latest changes from gitlab-org/gitlab@13-9-stable-eev13.9.0-rc42
Diffstat (limited to 'spec/frontend/captcha/init_recaptcha_script_spec.js')
-rw-r--r-- | spec/frontend/captcha/init_recaptcha_script_spec.js | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/spec/frontend/captcha/init_recaptcha_script_spec.js b/spec/frontend/captcha/init_recaptcha_script_spec.js new file mode 100644 index 00000000000..af07c9e474e --- /dev/null +++ b/spec/frontend/captcha/init_recaptcha_script_spec.js @@ -0,0 +1,59 @@ +import { + RECAPTCHA_API_URL_PREFIX, + RECAPTCHA_ONLOAD_CALLBACK_NAME, + clearMemoizeCache, + initRecaptchaScript, +} from '~/captcha/init_recaptcha_script'; + +describe('initRecaptchaScript', () => { + afterEach(() => { + document.head.innerHTML = ''; + clearMemoizeCache(); + }); + + const getScriptOnload = () => window[RECAPTCHA_ONLOAD_CALLBACK_NAME]; + const triggerScriptOnload = () => window[RECAPTCHA_ONLOAD_CALLBACK_NAME](); + + describe('when called', () => { + let result; + + beforeEach(() => { + result = initRecaptchaScript(); + }); + + it('adds script to head', () => { + expect(document.head).toMatchInlineSnapshot(` + <head> + <script + class="js-recaptcha-script" + src="${RECAPTCHA_API_URL_PREFIX}?onload=${RECAPTCHA_ONLOAD_CALLBACK_NAME}&render=explicit" + /> + </head> + `); + }); + + it('is memoized', () => { + expect(initRecaptchaScript()).toBe(result); + expect(document.head.querySelectorAll('script').length).toBe(1); + }); + + describe('when onload is triggered', () => { + beforeEach(() => { + window.grecaptcha = 'fake grecaptcha'; + triggerScriptOnload(); + }); + + afterEach(() => { + window.grecaptcha = undefined; + }); + + it('resolves promise with window.grecaptcha as argument', async () => { + await expect(result).resolves.toBe(window.grecaptcha); + }); + + it('sets window[RECAPTCHA_ONLOAD_CALLBACK_NAME] to undefined', async () => { + expect(getScriptOnload()).toBeUndefined(); + }); + }); + }); +}); |