@@ -392,3 +392,91 @@ test.cb('generates error if template has no output function', (t) => {
392392 } )
393393 project . compile ( )
394394} )
395+
396+ test . cb ( 'can use locals in template' , ( t ) => {
397+ const locals = {
398+ localTest : 'locals available'
399+ }
400+ const contentful = new Contentful ( {
401+ accessToken : process . env . accessToken ,
402+ spaceId : process . env . spaceId ,
403+ addDataTo : locals ,
404+ contentTypes : [
405+ {
406+ name : 'cats' ,
407+ id : 'cat' ,
408+ filters : {
409+ limit : 2 ,
410+ order : 'sys.createdAt'
411+ } ,
412+ template : {
413+ path : 'template.html' ,
414+ output : ( item ) => `cats/${ item . fields . name } .html`
415+ }
416+ }
417+ ]
418+ } )
419+
420+ const projectPath = path . join ( __dirname , 'fixtures/template-locals' )
421+ const project = new Spike ( {
422+ root : projectPath ,
423+ reshape : standard ( { locals } ) ,
424+ entry : { main : [ path . join ( projectPath , 'main.js' ) ] } ,
425+ plugins : [ contentful ]
426+ } )
427+
428+ project . on ( 'error' , t . end )
429+ project . on ( 'compile' , ( ) => {
430+ const file1 = fs . readFileSync ( path . join ( projectPath , 'public/cats/Happy Cat.html' ) , 'utf8' )
431+ const file2 = fs . readFileSync ( path . join ( projectPath , 'public/cats/Nyan Cat.html' ) , 'utf8' )
432+ t . is ( file1 . trim ( ) , `<p>${ locals . localTest } </p>` )
433+ t . is ( file2 . trim ( ) , `<p>${ locals . localTest } </p>` )
434+ rimraf . sync ( path . join ( projectPath , 'public' ) )
435+ t . end ( )
436+ } )
437+
438+ project . compile ( )
439+ } )
440+
441+ test . cb ( 'can use contentful in template' , ( t ) => {
442+ const locals = { }
443+ const contentful = new Contentful ( {
444+ accessToken : process . env . accessToken ,
445+ spaceId : process . env . spaceId ,
446+ addDataTo : locals ,
447+ contentTypes : [
448+ {
449+ name : 'cats' ,
450+ id : 'cat' ,
451+ filters : {
452+ limit : 2 ,
453+ order : 'sys.createdAt'
454+ } ,
455+ template : {
456+ path : 'template.html' ,
457+ output : ( item ) => `cats/${ item . fields . name } .html`
458+ }
459+ }
460+ ]
461+ } )
462+
463+ const projectPath = path . join ( __dirname , 'fixtures/template-contentful' )
464+ const project = new Spike ( {
465+ root : projectPath ,
466+ reshape : standard ( { locals } ) ,
467+ entry : { main : [ path . join ( projectPath , 'main.js' ) ] } ,
468+ plugins : [ contentful ]
469+ } )
470+
471+ project . on ( 'error' , t . end )
472+ project . on ( 'compile' , ( ) => {
473+ const file1 = fs . readFileSync ( path . join ( projectPath , 'public/cats/Happy Cat.html' ) , 'utf8' )
474+ const file2 = fs . readFileSync ( path . join ( projectPath , 'public/cats/Nyan Cat.html' ) , 'utf8' )
475+ t . is ( file1 . trim ( ) , '<p>Nyan Cat</p>' )
476+ t . is ( file2 . trim ( ) , '<p>Nyan Cat</p>' )
477+ rimraf . sync ( path . join ( projectPath , 'public' ) )
478+ t . end ( )
479+ } )
480+
481+ project . compile ( )
482+ } )
0 commit comments