From 5fc338e6b2f688469632debbdabe9062d87a351b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Chopin?= Date: Fri, 9 Dec 2016 18:51:19 +0100 Subject: [PATCH] Add tests --- test/basic.test.js | 66 +++++++++++++++++++ .../pages/async-data.vue} | 2 +- test/{fixtures => }/basic/pages/css.vue | 0 test/basic/pages/head.vue | 16 +++++ test/{fixtures => }/basic/pages/stateful.vue | 2 +- test/{fixtures => }/basic/pages/stateless.vue | 0 test/fixtures/basic/pages/head.vue | 8 --- test/index.js | 39 ----------- test/index.test.js | 14 ++++ 9 files changed, 98 insertions(+), 49 deletions(-) create mode 100755 test/basic.test.js rename test/{fixtures/basic/pages/async-props.vue => basic/pages/async-data.vue} (70%) rename test/{fixtures => }/basic/pages/css.vue (100%) create mode 100755 test/basic/pages/head.vue rename test/{fixtures => }/basic/pages/stateful.vue (90%) rename test/{fixtures => }/basic/pages/stateless.vue (100%) delete mode 100755 test/fixtures/basic/pages/head.vue delete mode 100755 test/index.js create mode 100644 test/index.test.js diff --git a/test/basic.test.js b/test/basic.test.js new file mode 100755 index 0000000000..f8e3462725 --- /dev/null +++ b/test/basic.test.js @@ -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('

My component!

')) +}) + +/* +** 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('

The answer is 42

')) +}) + +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('

I can haz meta tags

')) +}) + +test('/async-data', async t => { + const { html } = await nuxt.renderRoute('/async-data') + t.true(html.includes('

Nuxt.js

')) +}) + +// Close server and ask nuxt to stop listening to file changes +test.after('Closing server and nuxt.js', t => { + server.close() + nuxt.close() +}) diff --git a/test/fixtures/basic/pages/async-props.vue b/test/basic/pages/async-data.vue similarity index 70% rename from test/fixtures/basic/pages/async-props.vue rename to test/basic/pages/async-data.vue index 0299b74965..6eec4d9466 100755 --- a/test/fixtures/basic/pages/async-props.vue +++ b/test/basic/pages/async-data.vue @@ -6,7 +6,7 @@ export default { data () { return new Promise((resolve) => { - setTimeout(() => resolve({ name: 'Kobe Bryant' }), 10) + setTimeout(() => resolve({ name: 'Nuxt.js' }), 10) }) } } diff --git a/test/fixtures/basic/pages/css.vue b/test/basic/pages/css.vue similarity index 100% rename from test/fixtures/basic/pages/css.vue rename to test/basic/pages/css.vue diff --git a/test/basic/pages/head.vue b/test/basic/pages/head.vue new file mode 100755 index 0000000000..a4795f298b --- /dev/null +++ b/test/basic/pages/head.vue @@ -0,0 +1,16 @@ + + + diff --git a/test/fixtures/basic/pages/stateful.vue b/test/basic/pages/stateful.vue similarity index 90% rename from test/fixtures/basic/pages/stateful.vue rename to test/basic/pages/stateful.vue index 1ef1e2ee31..3931e79db1 100755 --- a/test/fixtures/basic/pages/stateful.vue +++ b/test/basic/pages/stateful.vue @@ -9,7 +9,7 @@ export default { data () { return { answer: null } }, - beforeMount () { + created () { this.answer = 42 } } diff --git a/test/fixtures/basic/pages/stateless.vue b/test/basic/pages/stateless.vue similarity index 100% rename from test/fixtures/basic/pages/stateless.vue rename to test/basic/pages/stateless.vue diff --git a/test/fixtures/basic/pages/head.vue b/test/fixtures/basic/pages/head.vue deleted file mode 100755 index 7d86ac9d89..0000000000 --- a/test/fixtures/basic/pages/head.vue +++ /dev/null @@ -1,8 +0,0 @@ - diff --git a/test/index.js b/test/index.js deleted file mode 100755 index b3b43ab79b..0000000000 --- a/test/index.js +++ /dev/null @@ -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('

My component!

')) -}) - -test(async t => { - const html = await render('/css') - t.true(html.includes('.red{color:red;}')) - t.true(html.includes('
This is red
')) -}) - -test(async t => { - const html = await render('/stateful') - t.true(html.includes('

The answer is 42

')) -}) - -test(async t => { - const html = await (render('/head')) - t.true(html.includes('')) - t.true(html.includes('

I can haz meta tags

')) -}) - -test(async t => { - const html = await render('/async-props') - t.true(html.includes('

Kobe Bryant

')) -}) - -function render (url, ctx) { - return _render(url, ctx, { dir, staticMarkup: true }) -} diff --git a/test/index.test.js b/test/index.test.js new file mode 100644 index 0000000000..69f4ef8dee --- /dev/null +++ b/test/index.test.js @@ -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') +})