2016-05-22 16:58:14 +09:00
|
|
|
const tape = require('tape')
|
2016-06-29 08:06:04 -07:00
|
|
|
const choo = require('../../')
|
2016-06-30 08:18:07 -07:00
|
|
|
const view = require('../../html')
|
2016-05-22 16:58:14 +09:00
|
|
|
|
|
|
|
|
tape('should render on the server', function (t) {
|
|
|
|
|
t.test('should render a static response', function (t) {
|
|
|
|
|
t.plan(1)
|
|
|
|
|
|
|
|
|
|
const app = choo()
|
|
|
|
|
app.router((route) => [
|
2016-06-30 08:18:07 -07:00
|
|
|
route('/', () => view`<h1>Hello Tokyo!</h1>`)
|
2016-05-22 16:58:14 +09:00
|
|
|
])
|
|
|
|
|
|
|
|
|
|
const html = app.toString('/')
|
|
|
|
|
const expected = '<h1>Hello Tokyo!</h1>'
|
|
|
|
|
t.equal(html, expected, 'strings are equal')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.test('should accept a state object', function (t) {
|
|
|
|
|
t.plan(1)
|
|
|
|
|
|
|
|
|
|
const app = choo()
|
|
|
|
|
app.router((route) => [
|
|
|
|
|
route('/', function (params, state) {
|
2016-06-30 08:18:07 -07:00
|
|
|
return view`<h1>meow meow ${state.message}</h1>`
|
2016-05-22 16:58:14 +09:00
|
|
|
})
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
const html = app.toString('/', { message: 'nyan!' })
|
|
|
|
|
const expected = '<h1>meow meow nyan!</h1>'
|
|
|
|
|
t.equal(html, expected, 'strings are equal')
|
|
|
|
|
})
|
|
|
|
|
|
2016-05-23 23:39:22 +09:00
|
|
|
t.test('should extend flat existing models', function (t) {
|
2016-05-22 16:58:14 +09:00
|
|
|
t.plan(1)
|
|
|
|
|
|
|
|
|
|
const app = choo()
|
|
|
|
|
app.model({ state: { bin: 'baz', beep: 'boop' } })
|
|
|
|
|
app.router((route) => [
|
|
|
|
|
route('/', function (params, state) {
|
2016-06-30 08:18:07 -07:00
|
|
|
return view`<h1>${state.foo} ${state.bin} ${state.beep}</h1>`
|
2016-05-22 16:58:14 +09:00
|
|
|
})
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
const state = { foo: 'bar!', beep: 'beep' }
|
|
|
|
|
const html = app.toString('/', state)
|
|
|
|
|
const expected = '<h1>bar! baz beep</h1>'
|
|
|
|
|
t.equal(html, expected, 'strings are equal')
|
|
|
|
|
})
|
|
|
|
|
|
2016-05-23 23:39:22 +09:00
|
|
|
t.test('should extend namespaced existing models', function (t) {
|
|
|
|
|
t.plan(1)
|
|
|
|
|
|
|
|
|
|
const app = choo()
|
|
|
|
|
app.model({
|
|
|
|
|
namespace: 'hello',
|
|
|
|
|
state: { bin: 'baz', beep: 'boop' }
|
|
|
|
|
})
|
|
|
|
|
app.router((route) => [
|
|
|
|
|
route('/', function (params, state) {
|
2016-06-30 08:18:07 -07:00
|
|
|
return view`
|
2016-05-23 23:39:22 +09:00
|
|
|
<h1>${state.hello.foo} ${state.hello.bin} ${state.hello.beep}</h1>
|
|
|
|
|
`
|
|
|
|
|
})
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
const state = {
|
|
|
|
|
hello: {
|
|
|
|
|
foo: 'bar!',
|
|
|
|
|
beep: 'beep'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const html = app.toString('/', state)
|
|
|
|
|
const expected = '<h1>bar! baz beep</h1>'
|
|
|
|
|
t.equal(html, expected, 'strings are equal')
|
|
|
|
|
})
|
|
|
|
|
|
2016-05-22 16:58:14 +09:00
|
|
|
t.test('should throw if called without route', function (t) {
|
|
|
|
|
t.plan(1)
|
|
|
|
|
|
|
|
|
|
const app = choo()
|
|
|
|
|
app.router((route) => [
|
|
|
|
|
route('/', function (params, state, send) {
|
|
|
|
|
send('hey!')
|
|
|
|
|
})
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
t.throws(app.toString.bind(null), /route must be a string/)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.test('should throw if calling send()', function (t) {
|
|
|
|
|
t.plan(1)
|
|
|
|
|
|
|
|
|
|
const app = choo()
|
|
|
|
|
app.router((route) => [
|
|
|
|
|
route('/', function (params, state, send) {
|
|
|
|
|
send('hey!')
|
|
|
|
|
})
|
|
|
|
|
])
|
|
|
|
|
|
2016-06-27 01:04:21 +02:00
|
|
|
const msg = /send\(\) cannot be called/
|
2016-05-22 16:58:14 +09:00
|
|
|
t.throws(app.toString.bind(null, '/', { message: 'nyan!' }), msg)
|
|
|
|
|
})
|
|
|
|
|
})
|