From 9213a8e6ef3f4d5974f45c5b8abd5a06e50092ae Mon Sep 17 00:00:00 2001 From: Tom Milewski Date: Mon, 25 Jul 2016 22:47:35 -0400 Subject: [PATCH] Update deps; Update opts validation; Test `limit` --- lib/index.js | 21 ++++++++----------- package.json | 11 +++++----- test/index.js | 57 +++++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 69 insertions(+), 20 deletions(-) diff --git a/lib/index.js b/lib/index.js index 1e9de38..4e88897 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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) { @@ -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, { @@ -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) => { @@ -141,6 +137,5 @@ function writeTemplate (ct, compiler, compilation, addDataTo) { }) } -// module.exports.writeTemplate = writeTemplate module.exports = Contentful module.exports.transform = transform diff --git a/package.json b/package.json index db66376..f29ccbb 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" }, diff --git a/test/index.js b/test/index.js index a93c0f1..8e9e37f 100644 --- a/test/index.js +++ b/test/index.js @@ -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({ @@ -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() @@ -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] })