Skip to content

Commit

Permalink
Update deps; Update opts validation; Test limit
Browse files Browse the repository at this point in the history
  • Loading branch information
tmilewski committed Jul 26, 2016
1 parent 7123b04 commit 9213a8e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 20 deletions.
21 changes: 8 additions & 13 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const fs = require('fs')
const path = require('path')
const node = require('when/node')
const posthtml = require('posthtml')
const loader = require('posthtml-loader')

class Contentful {
constructor (opts) {
Expand Down Expand Up @@ -76,19 +77,15 @@ function validate (opts = {}) {
addDataTo: Joi.object().required(),
json: Joi.string(),
contentTypes: Joi.array().items(
Joi.string(), Joi.object().keys({
Joi.object().keys({
id: Joi.string(),
name: Joi.string(),
filters: Joi.object().keys({
limit: Joi.number().integer().min(1).max(100).default(100)
}),
transform: Joi.alternatives().try(Joi.boolean(), Joi.func()).default(true)
})
)

// contentTypes: Joi.array().items(
// Joi.string(), Joi.object().keys({
// name: Joi.string(),
// transform: Joi.alternatives().try(Joi.boolean(), Joi.func()).default(true)
// })
// ).default(['posts'])
})

const res = Joi.validate(opts, schema, {
Expand Down Expand Up @@ -124,11 +121,10 @@ function writeTemplate (ct, compiler, compilation, addDataTo) {
return data.map((item) => {
addDataTo = Object.assign(addDataTo, { item: item })
compiler.request = filePath
let plugins = compiler.options.posthtml
if (typeof plugins === 'function') plugins = plugins.call(this, compiler)
if (typeof plugins === 'object') plugins = plugins.defaults

return posthtml(plugins)
const options = loader.parseOptions(compiler.options.posthtml, {})

return posthtml(options.plugins)
.process(template)
.then((r) => r.html)
.then((rendered) => {
Expand All @@ -141,6 +137,5 @@ function writeTemplate (ct, compiler, compilation, addDataTo) {
})
}

// module.exports.writeTemplate = writeTemplate
module.exports = Contentful
module.exports.transform = transform
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "spike-contentful",
"description": "Contentful CMS plugin for spike",
"version": "0.1.0",
"version": "0.1.1",
"author": "Tom Milewski",
"ava": {
"verbose": "true",
Expand All @@ -11,16 +11,17 @@
"dependencies": {
"contentful": "3.5.0",
"when": "^3.7.7",
"posthtml": "^0.9.0"
"posthtml": "^0.9.0",
"posthtml-loader": "^0.10.2"
},
"devDependencies": {
"ava": "^0.15.2",
"coveralls": "^2.11.9",
"coveralls": "^2.11.11",
"dotenv": "^2.0.0",
"nyc": "^6.4.4",
"nyc": "^7.1.0",
"posthtml-jade": "^0.8.2",
"posthtml-exp": "^0.9.0",
"rimraf": "^2.5.2",
"rimraf": "^2.5.4",
"spike-core": "^0.9.0",
"standard": "^7.1.2"
},
Expand Down
57 changes: 55 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,60 @@ test('initializes with an "accessToken", "spaceId", and "addDataTo"', (t) => {
t.truthy(rt)
})

test('initializes with "limit" filter', (t) => {
t.truthy(new Contentful({ accessToken: 'xxx', spaceId: 'xxx', addDataTo: {}, contentTypes: [{
name: 'test', id: 'xxxx', filters: { limit: 50 } }
]}))
})

test('errors with "limit" filter under 1', (t) => {
t.throws(
() => {
new Contentful({ accessToken: 'xxx', spaceId: 'xxx', addDataTo: {}, contentTypes: [{
name: 'test', id: 'xxxx', filters: { limit: 0 } }
]})
}, // eslint-disable-line
/option "limit" must be larger than or equal to 1/
)
})

test('errors with "limit" filter over 100', (t) => {
t.throws(
() => {
new Contentful({ accessToken: 'xxx', spaceId: 'xxx', addDataTo: {}, contentTypes: [{
name: 'test', id: 'xxxx', filters: { limit: 101 } }
]})
}, // eslint-disable-line
/option "limit" must be less than or equal to 1/
)
})

test('initializes with "limit" filter', (t) => {
let opts = { accessToken: 'xxx', spaceId: 'xxx', addDataTo: {}, contentTypes: [{
name: 'test', id: 'xxxx', filters: { limit: 50 } }
]}
t.truthy( new Contentful(opts))

// t.throws(
// () => {
// opts.filters.limit = 0
// console.log(opts)
// new Contentful(opts)
// }, // eslint-disable-line
// 'ValidationError: [spike-contentful constructor] option "limit" must contain at least 1 items'
// )
//
// t.throws(
// () => {
// opts.filters.limit = 101
// console.log(opts)
// new Contentful(opts)
// }, // eslint-disable-line
// 'ValidationError: [spike-contentful constructor] option "limit" must contain less than or equal to 100 items'
// )
})


test.cb('returns valid content', (t) => {
const locals = {}
const api = new Contentful({
Expand All @@ -55,7 +109,6 @@ test.cb('returns valid content', (t) => {
})

api.run(compilerMock, undefined, () => {
// console.log("Locals:", JSON.stringify(locals.contentful, null, 2))
t.is(locals.contentful.press.length, 91)
t.is(locals.contentful.blogs.length, 100)
t.end()
Expand Down Expand Up @@ -227,7 +280,7 @@ test.cb('accepts template object and generates html', (t) => {
const projectPath = path.join(__dirname, 'fixtures/default')
const project = new Spike({
root: projectPath,
posthtml: { defaults: [exp({ locals })] },
posthtml: { plugins: [exp({ locals })] },
entry: { main: [path.join(projectPath, 'main.js')] },
plugins: [contentful]
})
Expand Down

0 comments on commit 9213a8e

Please sign in to comment.