Commit ef8e0ab7 authored by Paul Gascou-Vaillancourt's avatar Paul Gascou-Vaillancourt Committed by Paul Gascou-Vaillancourt

Improve initArkoseLabsScript spec

parent 73847e4e
import { initArkoseLabsScript } from 'ee/arkose_labs/init_arkose_labs_script';
jest.mock('lodash/uniqueId', () => () => 'initArkoseLabsScript_callback');
jest.mock('lodash/uniqueId', () => (x) => `${x}7`);
const EXPECTED_CALLBACK_NAME = '_initArkoseLabsScript_callback_7';
const TEST_PUBLIC_KEY = 'arkose-labs-public-api-key';
describe('initArkoseLabsScript', () => {
let subject;
const initSubject = () => {
subject = initArkoseLabsScript({ publicKey: 'arkose-labs-public-api-key' });
subject = initArkoseLabsScript({ publicKey: TEST_PUBLIC_KEY });
};
const findScriptTags = () => document.querySelectorAll('script');
......@@ -19,7 +22,7 @@ describe('initArkoseLabsScript', () => {
it('sets a global enforcement callback', () => {
initSubject();
expect(window.initArkoseLabsScript_callback).not.toBe(undefined);
expect(window[EXPECTED_CALLBACK_NAME]).not.toBe(undefined);
});
it('adds ArkoseLabs scripts to the HTML head', () => {
......@@ -27,15 +30,21 @@ describe('initArkoseLabsScript', () => {
initSubject();
expect(findScriptTags()).toHaveLength(1);
const scriptTag = findScriptTags().item(0);
expect(scriptTag.getAttribute('type')).toBe('text/javascript');
expect(scriptTag.getAttribute('src')).toBe(
`https://client-api.arkoselabs.com/v2/${TEST_PUBLIC_KEY}/api.js`,
);
expect(scriptTag.getAttribute('data-callback')).toBe(EXPECTED_CALLBACK_NAME);
});
it('when callback is called, cleans up the global object and resolves the Promise', () => {
initSubject();
const enforcement = 'ArkoseLabsEnforcement';
window.initArkoseLabsScript_callback(enforcement);
window[EXPECTED_CALLBACK_NAME](enforcement);
expect(window.initArkoseLabsScript_callback).toBe(undefined);
expect(window[EXPECTED_CALLBACK_NAME]).toBe(undefined);
return expect(subject).resolves.toBe(enforcement);
});
});
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