Skip to content

Commit

Permalink
add test for caching
Browse files Browse the repository at this point in the history
  • Loading branch information
zspecza committed Jan 27, 2016
1 parent 76a75e4 commit b401994
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ lib-cov
build/Release
.nyc_output
test/fixtures/*/public
test/fixtures/basic--cache/temp.jade
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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",
Expand Down
94 changes: 94 additions & 0 deletions test/basic/cache.js
Original file line number Diff line number Diff line change
@@ -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()
})
1 change: 1 addition & 0 deletions test/fixtures/basic--cache/about.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
h1 wow
18 changes: 18 additions & 0 deletions test/fixtures/basic--cache/app.coffee
Original file line number Diff line number Diff line change
@@ -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'
}
]
)
]
5 changes: 5 additions & 0 deletions test/fixtures/basic--cache/index.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ul
- for p in contentful.blog_posts
li
h1= p.title
p= p.body
6 changes: 6 additions & 0 deletions test/fixtures/basic--cache/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "test",
"dependencies": {
"jade": "*"
}
}
17 changes: 17 additions & 0 deletions test/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down

0 comments on commit b401994

Please sign in to comment.