mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-23 14:15:13 +00:00
Add tests
This commit is contained in:
parent
d482711cbe
commit
5fc338e6b2
66
test/basic.test.js
Executable file
66
test/basic.test.js
Executable file
@ -0,0 +1,66 @@
|
||||
import test from 'ava'
|
||||
import jsdom from 'jsdom'
|
||||
import { createServer } from 'http'
|
||||
import { resolve } from 'path'
|
||||
const port = 4002
|
||||
const url = (route) => 'http://localhost:' + port + route
|
||||
|
||||
let nuxt = null
|
||||
let server = null
|
||||
|
||||
// Init nuxt.js and create server listening on localhost:4000
|
||||
test.before('Init Nuxt.js', t => {
|
||||
const Nuxt = require('../')
|
||||
const options = {
|
||||
rootDir: resolve(__dirname, 'basic'),
|
||||
dev: false
|
||||
}
|
||||
nuxt = new Nuxt(options)
|
||||
return nuxt.build()
|
||||
.then(function () {
|
||||
server = createServer((req, res) => nuxt.render(req, res))
|
||||
server.listen(port, 'localhost')
|
||||
})
|
||||
})
|
||||
|
||||
test('/stateless', async t => {
|
||||
const { html } = await nuxt.renderRoute('/stateless')
|
||||
t.true(html.includes('<h1>My component!</h1>'))
|
||||
})
|
||||
|
||||
/*
|
||||
** Example of testing via dom checking
|
||||
*/
|
||||
test('/css', async t => {
|
||||
const window = await nuxt.renderAndGetWindow(jsdom, url('/css'))
|
||||
const element = window.document.querySelector('.red')
|
||||
t.not(element, null)
|
||||
t.is(element.textContent, 'This is red')
|
||||
t.is(element.className, 'red')
|
||||
t.is(window.getComputedStyle(element).color, 'red')
|
||||
})
|
||||
|
||||
test('/stateful', async t => {
|
||||
const { html } = await nuxt.renderRoute('/stateful')
|
||||
t.true(html.includes('<div><p>The answer is 42</p></div>'))
|
||||
})
|
||||
|
||||
test('/head', async t => {
|
||||
const window = await nuxt.renderAndGetWindow(jsdom, url('/head'))
|
||||
const html = window.document.body.innerHTML
|
||||
const metas = window.document.getElementsByTagName('meta')
|
||||
t.is(window.document.title, 'My title')
|
||||
t.is(metas[0].getAttribute('content'), 'my meta')
|
||||
t.true(html.includes('<div><h1>I can haz meta tags</h1></div>'))
|
||||
})
|
||||
|
||||
test('/async-data', async t => {
|
||||
const { html } = await nuxt.renderRoute('/async-data')
|
||||
t.true(html.includes('<p>Nuxt.js</p>'))
|
||||
})
|
||||
|
||||
// Close server and ask nuxt to stop listening to file changes
|
||||
test.after('Closing server and nuxt.js', t => {
|
||||
server.close()
|
||||
nuxt.close()
|
||||
})
|
@ -6,7 +6,7 @@
|
||||
export default {
|
||||
data () {
|
||||
return new Promise((resolve) => {
|
||||
setTimeout(() => resolve({ name: 'Kobe Bryant' }), 10)
|
||||
setTimeout(() => resolve({ name: 'Nuxt.js' }), 10)
|
||||
})
|
||||
}
|
||||
}
|
16
test/basic/pages/head.vue
Executable file
16
test/basic/pages/head.vue
Executable file
@ -0,0 +1,16 @@
|
||||
<template>
|
||||
<div>
|
||||
<h1>I can haz meta tags</h1>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
head: {
|
||||
title: 'My title',
|
||||
meta: [
|
||||
{ content: 'my meta' }
|
||||
]
|
||||
}
|
||||
}
|
||||
</script>
|
@ -9,7 +9,7 @@ export default {
|
||||
data () {
|
||||
return { answer: null }
|
||||
},
|
||||
beforeMount () {
|
||||
created () {
|
||||
this.answer = 42
|
||||
}
|
||||
}
|
8
test/fixtures/basic/pages/head.vue
vendored
8
test/fixtures/basic/pages/head.vue
vendored
@ -1,8 +0,0 @@
|
||||
<template>
|
||||
<div>
|
||||
<Head>
|
||||
<meta content='my meta' />
|
||||
</Head>
|
||||
<h1>I can haz meta tags</h1>
|
||||
</div>
|
||||
</template>
|
@ -1,39 +0,0 @@
|
||||
import test from 'ava'
|
||||
import { join } from 'path'
|
||||
import build from '../server/build'
|
||||
import { render as _render } from '../server/render'
|
||||
|
||||
const dir = join(__dirname, 'fixtures', 'basic')
|
||||
|
||||
test.before(() => build(dir))
|
||||
|
||||
test(async t => {
|
||||
const html = await render('/stateless')
|
||||
t.true(html.includes('<h1>My component!</h1>'))
|
||||
})
|
||||
|
||||
test(async t => {
|
||||
const html = await render('/css')
|
||||
t.true(html.includes('.red{color:red;}'))
|
||||
t.true(html.includes('<div class="red">This is red</div>'))
|
||||
})
|
||||
|
||||
test(async t => {
|
||||
const html = await render('/stateful')
|
||||
t.true(html.includes('<div><p>The answer is 42</p></div>'))
|
||||
})
|
||||
|
||||
test(async t => {
|
||||
const html = await (render('/head'))
|
||||
t.true(html.includes('<meta content="my meta" class="nuxt-head"/>'))
|
||||
t.true(html.includes('<div><h1>I can haz meta tags</h1></div>'))
|
||||
})
|
||||
|
||||
test(async t => {
|
||||
const html = await render('/async-props')
|
||||
t.true(html.includes('<p>Kobe Bryant</p>'))
|
||||
})
|
||||
|
||||
function render (url, ctx) {
|
||||
return _render(url, ctx, { dir, staticMarkup: true })
|
||||
}
|
14
test/index.test.js
Normal file
14
test/index.test.js
Normal file
@ -0,0 +1,14 @@
|
||||
import test from 'ava'
|
||||
const Nuxt = require('../')
|
||||
const nuxt = new Nuxt()
|
||||
|
||||
test('Nuxt.js Class', (t) => {
|
||||
t.is(typeof Nuxt, 'function')
|
||||
})
|
||||
|
||||
test('Nuxt.js Instance', (t) => {
|
||||
t.is(typeof nuxt, 'object')
|
||||
t.is(nuxt.dev, true)
|
||||
t.is(typeof nuxt.build, 'function')
|
||||
t.is(typeof nuxt.generate, 'function')
|
||||
})
|
Loading…
Reference in New Issue
Block a user