Skip to content

Commit

Permalink
fix(express): adding express view tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yawetse committed Mar 27, 2021
1 parent 2f6852d commit 1aee24f
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 73 deletions.
4 changes: 2 additions & 2 deletions src/children.ts
Expand Up @@ -158,6 +158,8 @@ export function fetchJSONSync(path: string, options?: any ) {
}

export function getChildrenTemplate(template: string | any) {
console.log('template',template)

const cachedTemplate = templateCache.get(template);
if (cachedTemplate) {
return cachedTemplate;
Expand All @@ -174,9 +176,7 @@ export function getChildrenTemplate(template: string | any) {
} else if (typeof template === "string") {
const jsFile = fs.readFileSync(path.resolve(template)).toString();
const jsonxModule = scopedEval(`(${jsFile})`);
// console.log({jsonxModule})
templateCache.set(template, jsonxModule);
// console.log({ templateCache });
return jsonxModule;
}
return null;
Expand Down
8 changes: 8 additions & 0 deletions src/express.test.ts
Expand Up @@ -32,5 +32,13 @@ describe('express', function(){
done()
})
})
it('should load external templates', ()=>{
const template:string = jsonx.__express(path.resolve('./src/mock/test_template.jxm.json')) as string
const simple_template:string = jsonx.__express(path.resolve('./src/mock/simple_test_template.jxm.json')) as string
expect(template).toMatch('<!DOCTYPE html>\n' +
'<div data-reactroot=""><div>this is in the main template</div><div title="testing templates"><div>from external template</div></div></div>');
expect(simple_template).toMatch(`<!DOCTYPE html>
<main data-reactroot=""><div>this is in the simple template</div><section title="testing simple templates"><div>from external template</div></section></main>`)
})
})
})
3 changes: 1 addition & 2 deletions src/express.ts
Expand Up @@ -27,8 +27,7 @@ export function __express(filePath?: string, options?: any, callback?: any) {
delete resources.__DOCTYPE;
delete resources.__jsonx;
const context = Object.assign({disableRenderIndexKey:false}, options?.__boundConfig);
if (isJSON) context.useJSON = true;

// if (isJSON) context.useJSON = true;
const jsonxRenderedString = outputHTML.call(context, {
jsonx: jsonxModule,
resources
Expand Down
14 changes: 14 additions & 0 deletions src/mock/simple_test_template.jxm.json
@@ -0,0 +1,14 @@
{
"main":[
{ "div":"this is in the simple template"},
{
"section":{
"props":{
"title":"testing simple templates"
},
"children":"replace this",
"___template": "./src/mock/simple_template.jxm.json"
}
}
]
}
16 changes: 16 additions & 0 deletions src/mock/test_template.jxm.json
@@ -0,0 +1,16 @@
{
"component":"div",
"children":[
{
"component":"div",
"children":"this is in the main template"
},
{
"component":"div",
"props":{
"title":"testing templates"
},
"___template": "./src/mock/simple_template.jxm.json"
}
]
}

0 comments on commit 1aee24f

Please sign in to comment.