Skip to content

Commit

Permalink
Integration test for the rewritePath function
Browse files Browse the repository at this point in the history
* Also updates the `spawn` support function to reject a promise if given
a non-zero exit code.
  • Loading branch information
benstepp committed Apr 13, 2016
1 parent f200c45 commit a571623
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 2 deletions.
Empty file.
7 changes: 7 additions & 0 deletions test/fixtures/path-rewrite/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
exports.rewritePath = function rewritePath (parsedPath) {
if (parsedPath.name === 'move-me') {
return '/moved/'
} else {
return undefined
}
}
29 changes: 29 additions & 0 deletions test/fixtures/path-rewrite/html.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React from 'react'
import { prefixLink } from 'gatsby-helpers'

module.exports = React.createClass({
displayName: 'HTML',
propTypes: {
body: React.PropTypes.string,
},
render () {
const { body } = this.props

return (
<html lang="en">
<head>
<meta charSet="utf-8" />
<meta httpEquiv="X-UA-Compatible" content="IE=edge" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0 maximum-scale=5.0"
/>
</head>
<body>
<div id="react-mount" dangerouslySetInnerHTML={{ __html: body }} />
<script src={prefixLink('/bundle.js')} />
</body>
</html>
)
},
})
15 changes: 15 additions & 0 deletions test/fixtures/path-rewrite/pages/_template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react'

export default class Template extends React.Component {
static get propTypes () {
return { children: React.PropTypes.any }
}

render () {
return (
<main>
{this.props.children}
</main>
)
}
}
3 changes: 3 additions & 0 deletions test/fixtures/path-rewrite/pages/dont-move-me.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
---
# This will not be moved
3 changes: 3 additions & 0 deletions test/fixtures/path-rewrite/pages/move-me.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
---
# This will be moved
29 changes: 29 additions & 0 deletions test/integration/user-rewrites-path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import test from 'ava'
import path from 'path'
import Promise from 'bluebird'
import { spawn } from '../support'
import fse from 'fs-extra'
const fs = Promise.promisifyAll(fse)

const fixturePath = path.resolve('../fixtures/path-rewrite')
const buildPath = path.resolve(fixturePath, 'public')
const gatsby = path.resolve('../../bin', 'gatsby.js')

test.before('build the site', async () => {
await fs.remove(buildPath)
await spawn(gatsby, ['build'], { cwd: fixturePath })
})

test('the move-me file has been moved', async t => {
const movedPath = path.resolve(buildPath, 'moved', 'index.html')
const file = await fs.statAsync(movedPath)

t.truthy(file)
})

test('the dont-move-me file has not been moved', async t => {
const dontMovePath = path.resolve(buildPath, 'dont-move-me', 'index.html')
const file = await fs.statAsync(dontMovePath)

t.truthy(file)
})
10 changes: 8 additions & 2 deletions test/support.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ export function spawn (command, args, options) {
let stdout = ''
let stderr = ''
const child = spawnNative(command, args, options)
child.on('exit', code => resolve({ code, stdout }))
child.on('error', error => reject({ error, stderr }))
child.stdout.on('data', data => { stdout += data })
child.stderr.on('data', data => { stderr += data })
child.on('error', error => reject({ error, stderr, stdout }))
child.on('exit', code => {
if (code === 0) {
resolve({ code, stdout, stderr })
} else {
reject({ code, stdout, stderr })
}
})
})
}

Expand Down

0 comments on commit a571623

Please sign in to comment.