Skip to content

Commit 607069f

Browse files
MathieuLoutrejescalan
authored andcommitted
Add tests for contentful and locals in template (#79)
1 parent a0f9a65 commit 607069f

4 files changed

Lines changed: 92 additions & 1 deletion

File tree

lib/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class Contentful {
5656

5757
// add a reshape multi option to compile each template separately
5858
options.multi = data.map((d) => {
59-
return { locals: { item: d, contentful: this.addDataTo.contentful }, name: conf.template.output(d) }
59+
return { locals: Object.assign({}, this.addDataTo, { item: d }), name: conf.template.output(d) }
6060
})
6161
return options
6262
})
@@ -95,6 +95,7 @@ class Contentful {
9595
.tap((res) => { m[ct.name] = res })
9696
.yield(m)
9797
}, {}).done((res) => {
98+
9899
this.addDataTo = Object.assign(this.addDataTo, { contentful: res })
99100
done()
100101
}, done)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>{{ contentful.cats[0].fields.name }}</p>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>{{ localTest }}</p>

test/index.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)