mirror of
https://github.com/c8r/x0.git
synced 2024-08-17 09:20:52 +03:00
Add tests for static module
This commit is contained in:
parent
a11b83883b
commit
490587716d
2
.gitignore
vendored
2
.gitignore
vendored
@ -2,3 +2,5 @@ node_modules
|
||||
npm-debug.log
|
||||
package-lock.json
|
||||
site
|
||||
coverage
|
||||
.nyc_output
|
||||
|
@ -7,7 +7,7 @@ require('babel-register')({
|
||||
}],
|
||||
require.resolve('babel-preset-stage-0'),
|
||||
require.resolve('babel-preset-react')
|
||||
] //.map(require.resolve)
|
||||
]
|
||||
})
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
@ -27,13 +27,11 @@ ${initialProps ? `<script id='__initial-props__' type='application/json'>${JSON.
|
||||
${scripts.map(src => `<script src='${src}'></script>`)}
|
||||
</html>`)
|
||||
|
||||
const render = (Component, props, static) =>
|
||||
(static ? renderToStaticMarkup : renderToString)(
|
||||
const render = (Component, props, isStatic) =>
|
||||
(isStatic ? renderToStaticMarkup : renderToString)(
|
||||
React.createElement(Component, props)
|
||||
)
|
||||
|
||||
const doc = n => '<!DOCTYPE html>' + n
|
||||
|
||||
const renderHTML = async (Component, options) => {
|
||||
const isStatic = options.static || !options.outDir
|
||||
let body = render(Component, options, isStatic)
|
||||
@ -88,21 +86,21 @@ const createStatic = async (filename, options) => {
|
||||
const req = require(filename)
|
||||
const Component = req.default || req
|
||||
|
||||
let result
|
||||
let html
|
||||
if (options.routes && options.routes.length) {
|
||||
result = await options.routes.map(async pathname => {
|
||||
html = await options.routes.map(async pathname => {
|
||||
const res = await writePage(Component, Object.assign({}, options, {
|
||||
pathname
|
||||
}))
|
||||
return res
|
||||
})
|
||||
} else {
|
||||
result = await writePage(Component, options)
|
||||
html = await writePage(Component, options)
|
||||
}
|
||||
|
||||
const bundle = await client(filename, options)
|
||||
|
||||
return result
|
||||
return { html, bundle }
|
||||
}
|
||||
|
||||
module.exports = (filename, options = {}, cb) => {
|
||||
|
28
package.json
28
package.json
@ -8,9 +8,9 @@
|
||||
},
|
||||
"scripts": {
|
||||
"start": "./bin/cli.js dev docs/App.js -op 8888",
|
||||
"build": "./bin/cli.js build docs/App.js",
|
||||
"test": "./bin/cli.js build docs/App.js -d docs",
|
||||
"_test": "./bin/cli.js build docs/App.js "
|
||||
"build": "./bin/cli.js build docs/App.js -d docs",
|
||||
"test": "nyc ava",
|
||||
"cover": "nyc report --reporter=html --reporter=lcov"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "Brent Jackson",
|
||||
@ -37,10 +37,10 @@
|
||||
"webpack-dev-server": "2.7.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"ava": "^0.22.0",
|
||||
"cxs": "^6.0.0",
|
||||
"got": "^7.1.0",
|
||||
"isomorphic-fetch": "^2.2.1",
|
||||
"node-fetch": "^1.7.3",
|
||||
"nyc": "^11.2.1",
|
||||
"refunk": "^2.0.0-1"
|
||||
},
|
||||
"x0": {
|
||||
@ -51,5 +51,23 @@
|
||||
"/",
|
||||
"debug"
|
||||
]
|
||||
},
|
||||
"ava": {
|
||||
"files": [
|
||||
"test/*",
|
||||
"!test/components",
|
||||
"!test/output"
|
||||
],
|
||||
"require": [ "babel-register" ],
|
||||
"babel": {
|
||||
"presets": [
|
||||
"env",
|
||||
"stage-0",
|
||||
"react"
|
||||
],
|
||||
"plugins": [
|
||||
"transform-runtime"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
7
test/components/Hello.js
Normal file
7
test/components/Hello.js
Normal file
@ -0,0 +1,7 @@
|
||||
import React from 'react'
|
||||
|
||||
const App = props => (
|
||||
<h1>Hello</h1>
|
||||
)
|
||||
|
||||
export default App
|
11
test/components/Props.js
Normal file
11
test/components/Props.js
Normal file
@ -0,0 +1,11 @@
|
||||
import React from 'react'
|
||||
|
||||
const App = props => (
|
||||
<h1>Hello</h1>
|
||||
)
|
||||
|
||||
App.getInitialProps = async () => {
|
||||
return { hello: 'hi' }
|
||||
}
|
||||
|
||||
export default App
|
49
test/snapshots/static.js.md
Normal file
49
test/snapshots/static.js.md
Normal file
@ -0,0 +1,49 @@
|
||||
# Snapshot report for `test/static.js`
|
||||
|
||||
The actual snapshot is saved in `static.js.snap`.
|
||||
|
||||
Generated by [AVA](https://ava.li).
|
||||
|
||||
## static renders
|
||||
|
||||
> Snapshot 1
|
||||
|
||||
`<!DOCTYPE html><html>␊
|
||||
<div id='__APP__'>␊
|
||||
<h1>Hello</h1>␊
|
||||
</div>␊
|
||||
␊
|
||||
␊
|
||||
</html>`
|
||||
|
||||
## static writes
|
||||
|
||||
> Snapshot 1
|
||||
|
||||
`<!DOCTYPE html><html>␊
|
||||
<div id='__APP__'>␊
|
||||
<h1 data-reactroot="">Hello</h1>␊
|
||||
</div>␊
|
||||
<script id='__initial-props__' type='application/json'>{"outDir":"test/output"}</script>␊
|
||||
<script src='/bundle.js'></script>␊
|
||||
</html>
|
||||
|
||||
## static uses getInitialProps method
|
||||
|
||||
> Snapshot 1
|
||||
|
||||
`<!DOCTYPE html><html>␊
|
||||
<div id='__APP__'>␊
|
||||
<h1>Hello</h1>␊
|
||||
</div>␊
|
||||
␊
|
||||
␊
|
||||
</html>
|
||||
|
||||
## static picks up routes config
|
||||
|
||||
> Snapshot 1
|
||||
|
||||
[
|
||||
Promise {},
|
||||
]
|
BIN
test/snapshots/static.js.snap
Normal file
BIN
test/snapshots/static.js.snap
Normal file
Binary file not shown.
74
test/static.js
Normal file
74
test/static.js
Normal file
@ -0,0 +1,74 @@
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
import test from 'ava'
|
||||
import x0Static from '../lib/static'
|
||||
|
||||
const hello = path.join(__dirname, './components/Hello.js')
|
||||
const withprops = path.join(__dirname, './components/Props.js')
|
||||
const output = path.join(__dirname, './output')
|
||||
const htmlFile = path.join(__dirname, './output/index.html')
|
||||
const bundleFile = path.join(__dirname, './output/bundle.js')
|
||||
|
||||
const clean = () => {
|
||||
if (fs.existsSync(htmlFile)) {
|
||||
fs.unlinkSync(htmlFile)
|
||||
}
|
||||
if (fs.existsSync(bundleFile)) {
|
||||
fs.unlinkSync(bundleFile)
|
||||
}
|
||||
}
|
||||
|
||||
test.before(clean)
|
||||
test.after(clean)
|
||||
|
||||
test.cb('static renders', t => {
|
||||
x0Static(hello, {}, (err, result) => {
|
||||
t.is(err, null)
|
||||
t.is(typeof result, 'object')
|
||||
t.snapshot(result.html)
|
||||
t.end()
|
||||
})
|
||||
})
|
||||
|
||||
test.cb('static writes', t => {
|
||||
x0Static(hello, {
|
||||
outDir: 'test/output'
|
||||
}, (err, result) => {
|
||||
t.is(err, null)
|
||||
t.is(typeof result, 'object')
|
||||
t.snapshot(result.html)
|
||||
t.end()
|
||||
})
|
||||
})
|
||||
|
||||
test.cb('static uses getInitialProps method', t => {
|
||||
x0Static(withprops, {}, (err, result) => {
|
||||
t.is(err, null)
|
||||
t.is(typeof result, 'object')
|
||||
t.snapshot(result.html)
|
||||
t.end()
|
||||
})
|
||||
})
|
||||
|
||||
test.cb('static picks up routes config', t => {
|
||||
x0Static(hello, {
|
||||
routes: [
|
||||
'/'
|
||||
]
|
||||
}, (err, result) => {
|
||||
t.is(err, null)
|
||||
t.is(typeof result, 'object')
|
||||
t.snapshot(result.html)
|
||||
t.end()
|
||||
})
|
||||
})
|
||||
|
||||
test.cb('static makes a directory', t => {
|
||||
fs.rmdirSync(output)
|
||||
x0Static(hello, {
|
||||
outDir: 'test/output'
|
||||
}, (err, result) => {
|
||||
t.is(err, null)
|
||||
t.end()
|
||||
})
|
||||
})
|
Loading…
Reference in New Issue
Block a user