debian-mirror-gitlab/spec/frontend/captcha/init_recaptcha_script_spec.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

59 lines
1.5 KiB
JavaScript
Raw Normal View History

2021-03-11 19:13:27 +05:30
import {
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"
2022-11-25 23:54:43 +05:30
src="undefined?onload=recaptchaOnloadCallback&render=explicit"
2021-03-11 19:13:27 +05:30
/>
</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);
});
2023-06-20 00:43:36 +05:30
it('sets window[RECAPTCHA_ONLOAD_CALLBACK_NAME] to undefined', () => {
2021-03-11 19:13:27 +05:30
expect(getScriptOnload()).toBeUndefined();
});
});
});
});