@hcaptcha/react-hcaptcha
Advanced tools
Comparing version 0.3.2 to 0.3.4
@@ -31,9 +31,14 @@ "use strict"; | ||
var React = require('react'); // Create script to init hCaptcha | ||
var React = require('react'); | ||
var _require = require("./utils.js"), | ||
generateQuery = _require.generateQuery; // Create script to init hCaptcha | ||
var onLoadListeners = []; | ||
var captchaScriptCreated = false; // Generate hCaptcha API Script | ||
var CaptchaScript = function CaptchaScript(hl, reCaptchaCompat) { | ||
var CaptchaScript = function CaptchaScript() { | ||
var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
// Create global onload callback | ||
@@ -48,14 +53,9 @@ window.hcaptchaOnLoad = function () { | ||
var domain = params.apihost || "https://hcaptcha.com"; | ||
delete params.apihost; | ||
var script = document.createElement("script"); | ||
script.src = "https://hcaptcha.com/1/api.js?render=explicit&onload=hcaptchaOnLoad"; | ||
script.src = "".concat(domain, "/1/api.js?render=explicit&onload=hcaptchaOnLoad"); | ||
script.async = true; | ||
if (hl) { | ||
script.src += "&hl=".concat(hl); | ||
} | ||
if (reCaptchaCompat === false) { | ||
script.src += '&recaptchacompat=off'; | ||
} | ||
var query = generateQuery(params); | ||
script.src += query !== "" ? "&".concat(query) : ""; | ||
document.head.appendChild(script); | ||
@@ -101,4 +101,11 @@ }; | ||
var _this$props = this.props, | ||
languageOverride = _this$props.languageOverride, | ||
reCaptchaCompat = _this$props.reCaptchaCompat; | ||
apihost = _this$props.apihost, | ||
assethost = _this$props.assethost, | ||
endpoint = _this$props.endpoint, | ||
host = _this$props.host, | ||
imghost = _this$props.imghost, | ||
hl = _this$props.languageOverride, | ||
reCaptchaCompat = _this$props.reCaptchaCompat, | ||
reportapi = _this$props.reportapi, | ||
sentry = _this$props.sentry; | ||
var isApiReady = this.state.isApiReady; | ||
@@ -111,3 +118,13 @@ | ||
captchaScriptCreated = true; | ||
CaptchaScript(languageOverride, reCaptchaCompat); | ||
CaptchaScript({ | ||
apihost: apihost, | ||
assethost: assethost, | ||
endpoint: endpoint, | ||
hl: hl, | ||
host: host, | ||
imghost: imghost, | ||
recaptchacompat: reCaptchaCompat === false ? "off" : null, | ||
reportapi: reportapi, | ||
sentry: sentry | ||
}); | ||
} // Add onload callback to global onload listeners | ||
@@ -114,0 +131,0 @@ |
{ | ||
"name": "@hcaptcha/react-hcaptcha", | ||
"version": "0.3.2", | ||
"version": "0.3.4", | ||
"description": "A React library for hCaptcha", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -74,2 +74,9 @@ # React hCaptcha Component Library | ||
|`id`|String|No|`random id`|Manually set the ID of the hCaptcha component. Make sure each hCaptcha component generated on a single page has its own unique ID when using this prop.| | ||
|`apihost`|String|No|`-`|See enterprise docs.| | ||
|`assethost`|String|No|`-`|See enterprise docs.| | ||
|`endpoint`|String|No|`-`|See enterprise docs.| | ||
|`host`|String|No|`-`|See enterprise docs.| | ||
|`imghost`|String|No|`-`|See enterprise docs.| | ||
|`reportapi`|String|No|`-`|See enterprise docs.| | ||
|`sentry`|String|No|`-`|See enterprise docs.| | ||
@@ -76,0 +83,0 @@ ### Events |
const React = require('react'); | ||
const { generateQuery } = require("./utils.js"); | ||
@@ -8,3 +9,3 @@ // Create script to init hCaptcha | ||
// Generate hCaptcha API Script | ||
const CaptchaScript = (hl, reCaptchaCompat) => { | ||
const CaptchaScript = (params={}) => { | ||
// Create global onload callback | ||
@@ -19,12 +20,12 @@ window.hcaptchaOnLoad = () => { | ||
let script = document.createElement("script"); | ||
script.src = "https://hcaptcha.com/1/api.js?render=explicit&onload=hcaptchaOnLoad"; | ||
script.async = true | ||
if (hl) { | ||
script.src += `&hl=${hl}` | ||
} | ||
if (reCaptchaCompat === false) { | ||
script.src += '&recaptchacompat=off' | ||
} | ||
const domain = params.apihost || "https://hcaptcha.com"; | ||
delete params.apihost; | ||
const script = document.createElement("script"); | ||
script.src = `${domain}/1/api.js?render=explicit&onload=hcaptchaOnLoad`; | ||
script.async = true; | ||
const query = generateQuery(params); | ||
script.src += query !== ""? `&${query}` : ""; | ||
document.head.appendChild(script); | ||
@@ -65,6 +66,5 @@ } | ||
componentDidMount () { //Once captcha is mounted intialize hCaptcha - hCaptcha | ||
const { languageOverride, reCaptchaCompat } = this.props; | ||
const { apihost, assethost, endpoint, host, imghost, languageOverride:hl, reCaptchaCompat, reportapi, sentry } = this.props; | ||
const { isApiReady } = this.state; | ||
if (!isApiReady) { //Check if hCaptcha has already been loaded, if not create script tag and wait to render captcha | ||
@@ -75,3 +75,13 @@ | ||
captchaScriptCreated = true; | ||
CaptchaScript(languageOverride, reCaptchaCompat); | ||
CaptchaScript({ | ||
apihost, | ||
assethost, | ||
endpoint, | ||
hl, | ||
host, | ||
imghost, | ||
recaptchacompat: reCaptchaCompat === false? "off" : null, | ||
reportapi, | ||
sentry | ||
}); | ||
} | ||
@@ -78,0 +88,0 @@ |
@@ -1,16 +0,16 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import ReactTestUtils from 'react-dom/test-utils'; | ||
import HCaptcha from '../src/index'; | ||
import {getMockedHcaptcha, MOCK_EKEY, MOCK_TOKEN, MOCK_WIDGET_ID} from './hcaptcha.mock'; | ||
import React from "react"; | ||
import ReactDOM from "react-dom"; | ||
import ReactTestUtils, { act } from "react-dom/test-utils"; | ||
import HCaptcha from "../src/index"; | ||
import {getMockedHcaptcha, MOCK_EKEY, MOCK_TOKEN, MOCK_WIDGET_ID} from "./hcaptcha.mock"; | ||
const TEST_PROPS = { | ||
sitekey: '10000000-ffff-ffff-ffff-000000000001', | ||
theme: 'light', | ||
size: 'invisible', | ||
sitekey: "10000000-ffff-ffff-ffff-000000000001", | ||
theme: "light", | ||
size: "invisible", | ||
tabindex: 0, | ||
}; | ||
describe('hCaptcha', () => { | ||
describe("hCaptcha", () => { | ||
let instance; | ||
@@ -41,9 +41,9 @@ let mockFns; | ||
it('renders into a div', () => { | ||
it("renders into a div", () => { | ||
expect(ReactDOM.findDOMNode(instance).nodeName).toBe("DIV"); | ||
}); | ||
it('has functions', () => { | ||
expect(typeof instance.execute).toBe('function'); | ||
expect(typeof instance.resetCaptcha).toBe('function'); | ||
it("has functions", () => { | ||
expect(typeof instance.execute).toBe("function"); | ||
expect(typeof instance.resetCaptcha).toBe("function"); | ||
expect(instance.execute).toBeDefined(); | ||
@@ -53,3 +53,3 @@ expect(instance.resetCaptcha).toBeDefined(); | ||
it('can execute', () => { | ||
it("can execute", () => { | ||
expect(window.hcaptcha.execute.mock.calls.length).toBe(0); | ||
@@ -61,3 +61,3 @@ instance.execute(); | ||
it('can reset', () => { | ||
it("can reset", () => { | ||
expect(window.hcaptcha.reset.mock.calls.length).toBe(0); | ||
@@ -69,3 +69,3 @@ instance.resetCaptcha(); | ||
it('can remove', () => { | ||
it("can remove", () => { | ||
expect(window.hcaptcha.remove.mock.calls.length).toBe(0); | ||
@@ -77,3 +77,3 @@ instance.removeCaptcha(); | ||
it('emits verify with token and eKey', () => { | ||
it("emits verify with token and eKey", () => { | ||
expect(mockFns.onVerify.mock.calls.length).toBe(0); | ||
@@ -86,5 +86,5 @@ instance.handleSubmit(); | ||
it('emits error and calls reset', () => { | ||
it("emits error and calls reset", () => { | ||
expect(mockFns.onError.mock.calls.length).toBe(0); | ||
const error = 'invalid-input-response'; | ||
const error = "invalid-input-response"; | ||
instance.handleError(error); | ||
@@ -96,3 +96,3 @@ expect(mockFns.onError.mock.calls.length).toBe(1); | ||
it('emits expire and calls reset', () => { | ||
it("emits expire and calls reset", () => { | ||
expect(mockFns.onExpire.mock.calls.length).toBe(0); | ||
@@ -104,3 +104,3 @@ instance.handleExpire(); | ||
it('el renders after api loads and a widget id is set', () => { | ||
it("el renders after api loads and a widget id is set", () => { | ||
expect(instance.state.captchaId).toBe(MOCK_WIDGET_ID); | ||
@@ -116,3 +116,3 @@ expect(window.hcaptcha.render.mock.calls.length).toBe(1); | ||
it('should set id if id prop is passed', () => { | ||
it("should set id if id prop is passed", () => { | ||
instance = ReactTestUtils.renderIntoDocument( | ||
@@ -128,4 +128,4 @@ <HCaptcha | ||
it('should not set id if no id prop is passed', () => { | ||
process.env.NODE_ENV = 'development'; | ||
it("should not set id if no id prop is passed", () => { | ||
process.env.NODE_ENV = "development"; | ||
instance = ReactTestUtils.renderIntoDocument( | ||
@@ -140,2 +140,135 @@ <HCaptcha | ||
describe("Query parameter", () => { | ||
beforeEach(() => { | ||
// Setup hCaptcha as undefined to load script | ||
window.hcaptcha = undefined; | ||
}); | ||
afterEach(() => { | ||
// Clean up created script tag | ||
document.querySelectorAll("head > script") | ||
.forEach(script => document.head.removeChild(script)); | ||
}); | ||
it("validate src without", () => { | ||
instance = ReactTestUtils.renderIntoDocument(<HCaptcha | ||
sitekey={TEST_PROPS.sitekey} | ||
/>); | ||
const script = document.querySelector("head > script"); | ||
expect(script.src).toEqual("https://hcaptcha.com/1/api.js?render=explicit&onload=hcaptchaOnLoad"); | ||
}); | ||
it("apihost should change script src, but not be added as query", () => { | ||
const ExpectHost = "https://test.com"; | ||
instance = ReactTestUtils.renderIntoDocument(<HCaptcha | ||
apihost={ExpectHost} | ||
sitekey={TEST_PROPS.sitekey} | ||
/>); | ||
const script = document.querySelector("head > script"); | ||
expect(script.src).toContain(ExpectHost); | ||
expect(script.src).not.toContain(`apihost=${encodeURIComponent(ExpectHost)}`); | ||
}); | ||
it("assethost should be found when prop is set", () => { | ||
const ExpectHost = "https://test.com"; | ||
instance = ReactTestUtils.renderIntoDocument(<HCaptcha | ||
assethost={ExpectHost} | ||
sitekey={TEST_PROPS.sitekey} | ||
/>); | ||
const script = document.querySelector("head > script"); | ||
expect(script.src).toContain(`assethost=${encodeURIComponent(ExpectHost)}`); | ||
}); | ||
it("endpoint should be found when prop is set", () => { | ||
const ExpectHost = "https://test.com"; | ||
instance = ReactTestUtils.renderIntoDocument(<HCaptcha | ||
endpoint={ExpectHost} | ||
sitekey={TEST_PROPS.sitekey} | ||
/>); | ||
const script = document.querySelector("head > script"); | ||
expect(script.src).toContain(`endpoint=${encodeURIComponent(ExpectHost)}`); | ||
}); | ||
it("imghost should be found when prop is set", () => { | ||
const ExpectHost = "https://test.com"; | ||
instance = ReactTestUtils.renderIntoDocument(<HCaptcha | ||
imghost={ExpectHost} | ||
sitekey={TEST_PROPS.sitekey} | ||
/>); | ||
const script = document.querySelector("head > script"); | ||
expect(script.src).toContain(`imghost=${encodeURIComponent(ExpectHost)}`); | ||
}); | ||
it("reportapi should be found when prop is set", () => { | ||
const ExpectHost = "https://test.com"; | ||
instance = ReactTestUtils.renderIntoDocument(<HCaptcha | ||
reportapi={ExpectHost} | ||
sitekey={TEST_PROPS.sitekey} | ||
/>); | ||
const script = document.querySelector("head > script"); | ||
expect(script.src).toContain(`reportapi=${encodeURIComponent(ExpectHost)}`); | ||
}); | ||
it("hl should be found when prop languageOverride is set", () => { | ||
instance = ReactTestUtils.renderIntoDocument(<HCaptcha | ||
languageOverride="fr" | ||
sitekey={TEST_PROPS.sitekey} | ||
/>); | ||
const script = document.querySelector("head > script"); | ||
expect(script.src).toContain("hl=fr"); | ||
}); | ||
it("reCaptchaCompat should be found when prop is set to false", () => { | ||
instance = ReactTestUtils.renderIntoDocument(<HCaptcha | ||
reCaptchaCompat={false} | ||
sitekey={TEST_PROPS.sitekey} | ||
/>); | ||
const script = document.querySelector("head > script"); | ||
expect(script.src).toContain("recaptchacompat=off"); | ||
}); | ||
it("reCaptchaCompat should not be found when prop is set to anything except false", () => { | ||
instance = ReactTestUtils.renderIntoDocument(<HCaptcha | ||
reCaptchaCompat={true} | ||
sitekey={TEST_PROPS.sitekey} | ||
/>); | ||
const script = document.querySelector("head > script"); | ||
expect(script.src).not.toContain("recaptchacompat"); | ||
}); | ||
it("sentry should be found when prop is set", () => { | ||
instance = ReactTestUtils.renderIntoDocument(<HCaptcha | ||
sentry={true} | ||
sitekey={TEST_PROPS.sitekey} | ||
/>); | ||
const script = document.querySelector("head > script"); | ||
expect(script.src).toContain("sentry=true"); | ||
}); | ||
it("host should be found when prop is set", () => { | ||
instance = ReactTestUtils.renderIntoDocument(<HCaptcha | ||
host="test.com" | ||
sitekey={TEST_PROPS.sitekey} | ||
/>); | ||
const script = document.querySelector("head > script"); | ||
expect(script.src).toContain("host=test.com"); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
48280
19
840
136