diff --git a/.gitignore b/.gitignore index 885f017..461c325 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ lib-cov build/Release .nyc_output test/fixtures/*/public +test/fixtures/basic--cache/temp.jade diff --git a/package.json b/package.json index 150b881..ace7d63 100644 --- a/package.json +++ b/package.json @@ -35,9 +35,11 @@ "babel-runtime": "^6.3.19", "coffee-script": "^1.10.0", "coveralls": "^2.11.6", + "del": "^2.2.0", "husky": "^0.10.2", "mockery": "1.4.x", "nyc": "^5.3.0", + "performance-now": "^0.2.0", "roots": "3.1.0", "snazzy": "^2.0.1", "standard": "^5.4.1" @@ -79,7 +81,6 @@ "posttest": "node test/_teardown.js", "prebuild": "npm test", "precommit": "npm run lint -s", - "precoverage": "npm run test", "prerelease": "npm run build", "pretest": "npm run lint -s && node test/_setup.js", "release": "npm publish", diff --git a/test/basic/cache.js b/test/basic/cache.js new file mode 100644 index 0000000..72f6237 --- /dev/null +++ b/test/basic/cache.js @@ -0,0 +1,94 @@ +/* since this test depends on execution time, + each individual test is run serially on + purpose */ + +import fs from 'fs' +import path from 'path' +import test from 'ava' +import del from 'del' +import now from 'performance-now' +import { + async, + helpers, + mock_contentful, + unmock_contentful, + watch_fixture +} from '../helpers' + +const performance = { now } + +async function write_file (_path, content) { + _path = path.join('..', 'fixtures', 'basic--cache', _path) + return await new Promise((resolve, reject) => { + fs.writeFile(_path, content, 'utf8', (error) => { + if (error) { + reject(error) + } else { + resolve(_path) + } + }) + }) +} + +let ctx = {} + +test.serial.before(async t => { + let title = 'Throw Some Ds' + let body = 'Rich Boy selling crick' + ctx = { ...ctx, title, body } + mock_contentful({ + entries: [{ + fields: { title, body } + }] + }) + // watch the fixture directory for changes + ctx.watch = await ctx::watch_fixture('basic--cache') + ctx.index_path = `${ctx.public_dir}/index.html` + ctx.temp_file = `${ctx.public_dir}/temp.html` +}) + +// measure performance +test.serial.cb.before(t => { + Promise.resolve(ctx.watch.project) + .then(project => { + project.once('compile', () => { + ctx.first_compile_ms = performance.now() - ctx.first_compile_ms + }) + return project + }) + .then(project => { + ctx.first_compile_ms = performance.now() + // write a file to trigger the watcher's compile step + // for the first time + return write_file('temp.jade', 'h1 foo') + .then(path => ctx.temp_file_src = path) + .then(() => project) + }) + .then(project => { + project.once('compile', () => { + ctx.second_compile_ms = performance.now() - ctx.second_compile_ms + t.end() + }) + return project + }) + .then(() => { + ctx.second_compile_ms = performance.now() + // delete a file to trigger the watcher's + // compile step a second time + return del(ctx.tmp_file_src, { force: true }) + }) +}) + +test.serial('second compile should be quicker than the first', t => { + t.true(ctx.second_compile_ms < ctx.first_compile_ms) +}) + +test.serial('has contentful data in views', async t => { + t.true(await helpers.file.contains(ctx.index_path, ctx.title, { async })) + t.true(await helpers.file.contains(ctx.index_path, ctx.body, { async })) +}) + +test.serial.after(async t => { + ctx.watch.watcher.close() + unmock_contentful() +}) diff --git a/test/fixtures/basic--cache/about.jade b/test/fixtures/basic--cache/about.jade new file mode 100644 index 0000000..2763786 --- /dev/null +++ b/test/fixtures/basic--cache/about.jade @@ -0,0 +1 @@ +h1 wow \ No newline at end of file diff --git a/test/fixtures/basic--cache/app.coffee b/test/fixtures/basic--cache/app.coffee new file mode 100644 index 0000000..c2c261c --- /dev/null +++ b/test/fixtures/basic--cache/app.coffee @@ -0,0 +1,18 @@ +contentful = require '../../../src' + +module.exports = + ignores: ["**/_*", "**/.DS_Store"] + extensions: [ + contentful( + access_token: 'YOUR_ACCESS_TOKEN' + space_id: 'aqzq2qya2jm4' + content_types: [ + { + id: '6BYT1gNiIEyIw8Og8aQAO6' + } + { + id: '7CDlVsacqQc88cmIEGYWMa' + } + ] + ) + ] diff --git a/test/fixtures/basic--cache/index.jade b/test/fixtures/basic--cache/index.jade new file mode 100644 index 0000000..4769500 --- /dev/null +++ b/test/fixtures/basic--cache/index.jade @@ -0,0 +1,5 @@ +ul + - for p in contentful.blog_posts + li + h1= p.title + p= p.body diff --git a/test/fixtures/basic--cache/package.json b/test/fixtures/basic--cache/package.json new file mode 100644 index 0000000..2d0ae2c --- /dev/null +++ b/test/fixtures/basic--cache/package.json @@ -0,0 +1,6 @@ +{ + "name": "test", + "dependencies": { + "jade": "*" + } +} diff --git a/test/helpers/index.js b/test/helpers/index.js index 6ece651..06adc06 100644 --- a/test/helpers/index.js +++ b/test/helpers/index.js @@ -2,6 +2,7 @@ import path from 'path' import mockery from 'mockery' import Roots from 'roots' import RootsUtil from 'roots-util' +import {EventEmitter} from 'events' // polyfill array includes because of // https://github.com/sindresorhus/ava/issues/263 @@ -26,6 +27,22 @@ export async function compile_fixture (name) { return await helpers.project.compile(Roots, name) } +export async function watch_fixture (name) { + this.public_dir = `${name}/public` + let project = new EventEmitter() + return await new Promise(async (resolve, reject) => { + const watcher = new Roots(path.join(__dirname, '../fixtures', name)) + watcher.on('error', reject) + watcher.on('done', () => { + project.emit('compile') + }) + resolve({ + watcher: await watcher.watch(), + project + }) + }) +} + export function unmock_contentful () { mockery.deregisterAll() return mockery.disable()