@@ -2,11 +2,6 @@ const fs = require('fs');
22const path = require ( 'path' ) ;
33const expect = require ( 'chai' ) . expect ;
44
5- const {
6- execute,
7- executeRequireStatement
8- } = require ( '../src/execute.js' ) ;
9-
105const abellRenderer = require ( '../src/index.js' ) ;
116
127describe ( 'render() - renders abellTemplate into HTML Text' , ( ) => {
@@ -33,7 +28,10 @@ describe('render() - renders abellTemplate into HTML Text', () => {
3328 . render (
3429 abellTemplate ,
3530 sampleSandbox ,
36- { basePath : path . join ( __dirname , 'resources' ) }
31+ {
32+ basePath : path . join ( __dirname , 'resources' ) ,
33+ allowRequire : true
34+ }
3735 )
3836 ) . to . equal ( htmlTemplate ) ;
3937 } ) ;
@@ -79,11 +77,30 @@ describe('render() - renders abellTemplate into HTML Text', () => {
7977 expect (
8078 abellRenderer . render (
8179 abellTemplate ,
82- { }
80+ { } ,
81+ { allowRequire : true }
8382 ) . trim ( )
8483 ) . to . equal ( '<div>8 hi/hello hi/hello</div>' ) ;
8584 } ) ;
8685
86+ it ( 'should throw an error if require() is used without allowRequire: true option' , ( ) => {
87+ const abellTemplate = `
88+ {{
89+ const path = require('path');
90+ const hiHelloPath = require('path').join('hi', 'hello');
91+ }}
92+ <div>{{ path.join('hi', 'hello') }} {{ hiHelloPath }}</div>
93+ ` ;
94+
95+ expect (
96+ ( ) =>
97+ abellRenderer . render (
98+ abellTemplate ,
99+ { } ,
100+ )
101+ ) . to . throw ( 'require() is not allowed in the script' ) ;
102+ } ) ;
103+
87104 it ( 'should not throw error and return same value if blank brackets passed' , ( ) => {
88105 expect (
89106 abellRenderer . render (
@@ -102,41 +119,3 @@ describe('render() - renders abellTemplate into HTML Text', () => {
102119 ) . to . equal ( '{{ This is ignored }}' ) ;
103120 } ) ;
104121} ) ;
105-
106-
107- describe ( 'execute() - Executes JavaScript passed to it as string' , ( ) => {
108- it ( 'should output added value when addition is performed on two values' , ( ) => {
109- expect (
110- execute ( '24 + 12' , { } ) . value
111- ) . to . equal ( 36 ) ;
112- } ) ;
113-
114- it ( 'should update value of a to new value' , ( ) => {
115- expect (
116- execute ( 'a = 22 + 22' , { a : 4 } ) . sandbox . a
117- ) . to . equal ( 44 ) ;
118- } ) ;
119-
120- it ( 'should not update value that is inside string' , ( ) => {
121- expect (
122- execute ( '(() => \'a = b\')()' ) . value
123- ) . to . equal ( 'a = b' ) ;
124- } ) ;
125- } ) ;
126-
127-
128- describe ( 'executeRequireStatement() - executes the code with require() in its string' , ( ) => {
129- it ( 'should add path native object when required' , ( ) => {
130- expect (
131- executeRequireStatement ( 'const path = require(\'path\')' )
132- . path . join ( 'test' , 'path' )
133- ) . to . equal ( path . join ( 'test' , 'path' ) ) ;
134- } ) ;
135-
136- it ( 'should handle the case of require(\'module\').property' , ( ) => {
137- expect (
138- executeRequireStatement ( 'const testPath = require(\'path\').join(\'test\',\'path\')' )
139- . testPath
140- ) . to . equal ( path . join ( 'test' , 'path' ) ) ;
141- } ) ;
142- } ) ;
0 commit comments