Skip to content

Commit

Permalink
promise fix in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Escalante authored and joshrowley committed Jul 30, 2014
1 parent a1568be commit de82d72
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 28 deletions.
12 changes: 9 additions & 3 deletions lib/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,20 @@ module.exports = (opts) ->
if _.has(e.fields, 'sys') then return W.reject(errors.sys_conflict)
_.assign(_.omit(e, 'fields'), e.fields)

# load content
promise = configure_content(opts.content_types)
.then(get_all_content)
content = []

class RootsContentful
constructor: (@roots) ->
@roots.config.locals ||= {}

setup: ->
if content.length
configure_content(opts.content_types)
.then(get_all_content)
.then (res) -> content = res
else
W.resolve()

compile_hooks: ->
before_pass: (ctx) =>
# once content is loaded, pass contentful data into locals
Expand Down
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
"pluralize": "0.0.10"
},
"devDependencies": {
"chai": "1.x",
"chai-as-promised": "4.x",
"coffee-script": "1.7.x",
"mocha": "*",
"should": "*",
"coveralls": "2.x",
"istanbul": "0.3.x",
"mocha": "1.x",
"roots": "3.x",
"sinon": "~1.10.3",
"coveralls": "~2.11.1",
"istanbul": "~0.3.0"
"sinon": "1.x"
},
"peerDependencies": {
"roots": "3.x"
Expand Down
1 change: 1 addition & 0 deletions test/mocha.opts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
--reporter spec
--compilers coffee:coffee-script/register
--require test/support/helpers
--timeout 30000
17 changes: 17 additions & 0 deletions test/support/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var chai = require('chai'),
chai_promise = require('chai-as-promised'),
sinon = require('sinon'),
path = require('path'),
_path = path.join(__dirname, '../fixtures'),
RootsUtil = require('roots-util'),
h = new RootsUtil.Helpers({ base: _path }),
roots_contentful = require('../../lib');

var should = chai.should();
chai.use(chai_promise);

global.should = should;
global.sinon = sinon;
global._path = _path;
global.h = h;
global.roots_contentful = roots_contentful;
31 changes: 11 additions & 20 deletions test/test.coffee
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
path = require 'path'
fs = require 'fs'
should = require 'should'
sinon = require 'sinon'
W = require 'when'
node = require 'when/node'
Roots = require 'roots'
_path = path.join(__dirname, 'fixtures')
RootsUtil = require 'roots-util'
h = new RootsUtil.Helpers(base: _path)
contentful = require '../lib'

# setup, teardown, and utils

compile_fixture = (fixture_name, done) ->
@public = path.join(fixture_name, 'public')
h.project.compile(Roots, fixture_name, done)
node.call(h.project.compile.bind(h), Roots, fixture_name)

stub_contentful = (opts = {}) ->
contentful = require 'contentful'
sinon.stub(contentful, 'createClient').returns
contentType: -> W.resolve(opts.content_type || {name: 'Blog Post'})
contentType: -> W.resolve(opts.content_type || { name: 'Blog Post' })
entries: -> W.resolve [
opts.entry || {
sys: {'sys': 'data'},
Expand All @@ -39,29 +34,25 @@ after ->

describe 'config', ->
it 'should throw an error when missing an access token', ->
(-> contentful({})).should.throw()
(-> roots_contentful()).should.throw()

it 'should throw an error without content type id', (done) ->
(-> compile_fixture.call(@, 'missing_config', (e) -> done(new Error e)))
.should.throw()
it 'should throw an error without content type id', ->
compile_fixture.call(@, 'missing_config').should.be.rejected

describe 'contentful content type fields', ->
before ->
@stub = stub_contentful(entry: {fields: {sys: 'test'}})
before -> @stub = stub_contentful(entry: {fields: {sys: 'test'}})

it 'should throw an error if `sys` is a field name', ->
(-> compile_fixture.call(@, 'basic', (e) -> done(new Error e)))
.should.throw()
compile_fixture.call(@, 'basic').should.be.rejected

after ->
@stub.restore()
after -> @stub.restore()

describe 'basic compile', ->
before (done) ->
@title = 'Throw Some Ds'
@body = 'Rich Boy selling crack'
@body = 'Rich Boy selling crick'
@stub = stub_contentful(entry: {fields: {title: @title, body: @body}})
compile_fixture.call(@, 'basic', -> done())
compile_fixture.call(@, 'basic').then(-> done())

it 'compiles basic project', ->
p = path.join(@public, 'index.html')
Expand Down

0 comments on commit de82d72

Please sign in to comment.