Welcome to mirror list, hosted at ThFree Co, Russian Federation.

init_recaptcha_script_spec.js « captcha « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: af07c9e474eea566791f27b8357161b02887742b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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();
      });
    });
  });
});