-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ability to add custom scripts on report page
- Loading branch information
Showing
9 changed files
with
151 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
'use strict'; | ||
|
||
import React, {useEffect, useRef} from 'react'; | ||
import {isEmpty} from 'lodash'; | ||
|
||
export default function CustomScripts(props) { | ||
const {scripts} = props; | ||
|
||
if (isEmpty(scripts)) { | ||
return null; | ||
} | ||
|
||
const ref = useRef(null); | ||
|
||
useEffect(() => { | ||
scripts.forEach((script) => { | ||
const s = document.createElement('script'); | ||
s.type = 'text/javascript'; | ||
s.innerHTML = `(${script})();`; | ||
ref.current.appendChild(s); | ||
}); | ||
}, [scripts]); | ||
|
||
return <div className="custom-scripts" ref={ref}></div>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import React from 'react'; | ||
import CustomScripts from 'lib/static/components/custom-scripts'; | ||
|
||
describe('<CustomScripts />', () => { | ||
it('should render component for scripts', () => { | ||
const props = { | ||
scripts: [function() {}] | ||
}; | ||
|
||
const component = mount(<CustomScripts {...props} />); | ||
const node = component.find('.custom-scripts'); | ||
|
||
assert.lengthOf(node, 1); | ||
}); | ||
|
||
it('should not render component if no scripts to inject', () => { | ||
const props = { | ||
scripts: [] | ||
}; | ||
|
||
const component = mount(<CustomScripts {...props} />); | ||
const node = component.find('.custom-scripts'); | ||
|
||
assert.lengthOf(node, 0); | ||
}); | ||
|
||
it('should wrap function with IIFE', () => { | ||
const props = { | ||
scripts: [function foo() {}] | ||
}; | ||
|
||
const component = mount(<CustomScripts {...props} />); | ||
const script = component.text(); | ||
|
||
assert.equal(script, '(function foo() {})();'); | ||
}); | ||
|
||
it('should split each function with ";"', () => { | ||
const props = { | ||
scripts: [ | ||
function foo() {}, | ||
function bar() {} | ||
] | ||
}; | ||
|
||
const component = mount(<CustomScripts {...props} />); | ||
const script = component.text(); | ||
|
||
assert.equal(script, '(function foo() {})();(function bar() {})();'); | ||
}); | ||
}); |