diff --git a/package.json b/package.json index 6880be1..96d8ef1 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,8 @@ ], "ava": { "files": [ - "tests/**/*.js" + "tests/**/*.js", + "!tests/example/**/*.js" ], "source": [ "src/**/*.js", @@ -50,8 +51,8 @@ "scripts": { "release": "generate-release", "flow": "flow", - "debug": "npm run build && node --inspect example/app.js", - "start": "npm run build && node example/app.js", + "debug": "npm run build && node --inspect tests/example/app.js", + "start": "npm run build && node tests/example/app.js", "prepublish": "npm run build && nsp check", "pretest": "eslint . --fix", "test": "eslint src && nyc ava", diff --git a/tests/example/app.js b/tests/example/app.js new file mode 100644 index 0000000..999e9fa --- /dev/null +++ b/tests/example/app.js @@ -0,0 +1,75 @@ +// @flow +const path = require('path'); +const express = require('express'); +const uuidv4 = require('uuid/v4'); + +const expressVueRenderer = require('../lib'); +const expressVue = require('./expressVue'); + +var exampleMixin = { + methods: { + hello: function () { + console.log('Hello'); + } + } +}; + +const options = { + rootPath: path.join(__dirname, 'vueFiles'), + vue: { + head: { + meta: [{ + property: 'og:title', + content: 'Page Title' + }, + { + name: 'twitter:title', + content: 'Page Title' + }, + { + script: 'https://unpkg.com/vue@2.4.2/dist/vue.js' + }, { + name: 'viewport', + content: 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no' + } + ] + } + }, + data: { + thing: true + } +}; + + +renderer = expressVue.init(options); + +const app = express(); +app.use(express.static('./dist')); + +app.use(renderer); + +app.get('/', (req, res) => { + const data = { + title: 'Express Vue', + message: 'Hello world', + uuid: uuidv4(), + uuid2: uuidv4() + }; + const vueOptions = { + head: { + title: 'Page Title', + meta: [ + { + property: 'og:title2', + content: 'Page Title2' + } + ] + } + } + res.renderVue('main.vue', data, vueOptions) +}); + +app.listen(3000); + +console.log('express example start in 127.0.0.1:3000'); +module.exports = app; diff --git a/tests/example/components/inner.vue b/tests/example/components/inner.vue new file mode 100644 index 0000000..80b547b --- /dev/null +++ b/tests/example/components/inner.vue @@ -0,0 +1,16 @@ + + + Inner Text + + + + + + diff --git a/tests/example/components/uuid.vue b/tests/example/components/uuid.vue new file mode 100644 index 0000000..efe7450 --- /dev/null +++ b/tests/example/components/uuid.vue @@ -0,0 +1,25 @@ + + + + Uuid: {{uuid ? uuid : 'no uuid'}} + + + + + + diff --git a/tests/example/components/uuid2.vue b/tests/example/components/uuid2.vue new file mode 100644 index 0000000..6cf46ec --- /dev/null +++ b/tests/example/components/uuid2.vue @@ -0,0 +1,20 @@ + + + Uuid2: {{uuid2 ? uuid2 : 'no uuid'}} + + + + + + diff --git a/tests/example/expressVue.js b/tests/example/expressVue.js new file mode 100644 index 0000000..fb70ee5 --- /dev/null +++ b/tests/example/expressVue.js @@ -0,0 +1,26 @@ +const ExpressVueRenderer = require('../lib'); + +//This is the Middlewarein express-vue this wont be in the file +function init(options) { + //Make new object + const Renderer = new ExpressVueRenderer(options); + //Middleware init + return (req, res, next) => { + //Res RenderVUE function + res.renderVue = (componentPath, data = {}, vueOptions = {}) => { + res.set('Content-Type', 'text/html'); + Renderer.renderToStream(componentPath, data, vueOptions) + .then(stream => { + stream.on('data', chunk => res.write(chunk)); + stream.on('end', () => res.end()); + }) + .catch(error => { + console.error(error); + res.send(error); + }); + }; + return next(); + }; +} + +module.exports.init = init; diff --git a/tests/example/mixins/exampleMixin.js b/tests/example/mixins/exampleMixin.js new file mode 100644 index 0000000..b89cc67 --- /dev/null +++ b/tests/example/mixins/exampleMixin.js @@ -0,0 +1,10 @@ +const exampleMixin = { + methods: { + hello: function () { + console.log('Hello'); + } + } +}; + +module.exports = exampleMixin; +exports.default = exampleMixin; diff --git a/tests/example/vueFiles/main.vue b/tests/example/vueFiles/main.vue new file mode 100644 index 0000000..7fe51bf --- /dev/null +++ b/tests/example/vueFiles/main.vue @@ -0,0 +1,37 @@ + + + {{title}} + Welcome to the {{title}} demo. Click a link: + + {{message}} + + + Test mixin + Test method + + + + + + diff --git a/tests/index.js b/tests/index.js index d63f734..4e1a688 100644 --- a/tests/index.js +++ b/tests/index.js @@ -5,7 +5,7 @@ const Models = require('../src/models'); const path = require('path'); const options = { - rootPath: path.join(__dirname, '../example') + rootPath: path.join(__dirname, 'example/vueFiles') }; const data = { diff --git a/tests/parser/index.js b/tests/parser/index.js index 3f7bc31..8690723 100644 --- a/tests/parser/index.js +++ b/tests/parser/index.js @@ -20,9 +20,9 @@ var cacheOptions = { var lruCache = LRU(cacheOptions); let types = new Types(); -const component = __dirname + '/../../example/components/uuid.vue'; +const component = path.join(__dirname, '../example/components/uuid.vue'); const options = { - rootPath: path.join(__dirname, '../../example'), + rootPath: path.join(__dirname, '../example/vueFiles'), component: 'uuid.vue' }; diff --git a/tests/utils/checkPathUtils.js b/tests/utils/checkPathUtils.js index 5c80dd6..197def0 100644 --- a/tests/utils/checkPathUtils.js +++ b/tests/utils/checkPathUtils.js @@ -5,7 +5,7 @@ import { } from '../../src/utils'; import Models from '../../src/models'; -const rootPath = path.join(__dirname, '../../example/'); +const rootPath = path.join(__dirname, '../example/vueFiles'); const defaults = new Models.Defaults(); var LRU = require('lru-cache'); var cacheOptions = { @@ -15,8 +15,8 @@ var cacheOptions = { var lruCache = LRU(cacheOptions); test('correctPath Path', t => { - const filePath = path.join(rootPath, '../example/components/uuid.vue'); - const correctPath = rootPath + 'components/uuid.vue'; + const filePath = path.join(rootPath, '/../components/uuid.vue'); + const correctPath = path.join(rootPath + '/../components/uuid.vue'); return PathUtils.getCorrectPathForFile(filePath, 'view', defaults, lruCache) .then(returnedPath => { @@ -29,7 +29,7 @@ test('correctPath Path', t => { test('shows error for fake test Path ', t => { const filePath = path.join(rootPath, 'componentDoesntExist.vue'); - const errMessage = `Could not find test file at ${rootPath}componentDoesntExist.vue` + const errMessage = `Could not find test file at ${rootPath}/componentDoesntExist.vue` return PathUtils.getCorrectPathForFile(filePath, 'test', defaults, lruCache) .catch(error => {
Inner Text
Welcome to the {{title}} demo. Click a link:
{{message}}