From 02ad923712a8054cad3162f0a0d77723cfca4862 Mon Sep 17 00:00:00 2001 From: Benjamin Canac Date: Mon, 27 Jul 2020 10:20:52 +0200 Subject: [PATCH] fix: improve nuxt-content attrs handling (#223) * fix(lib): improve nuxt-content attrs handling * test: check `nuxt-content` id and class generation --- lib/templates/nuxt-content.dev.vue | 24 +++++++-- lib/templates/nuxt-content.js | 11 +++- test/component.test.js | 87 +++++++++++++++++++++++------- test/fixture/pages/_slug.vue | 11 ++-- 4 files changed, 105 insertions(+), 28 deletions(-) diff --git a/lib/templates/nuxt-content.dev.vue b/lib/templates/nuxt-content.dev.vue index cbd63d724..7099312b0 100644 --- a/lib/templates/nuxt-content.dev.vue +++ b/lib/templates/nuxt-content.dev.vue @@ -10,7 +10,8 @@ /> this.$vnode.data.class[key]) + } else { + classes = this.$vnode.data.class + } + this.classes = this.classes.concat(classes) delete this.$vnode.data.class } if (this.$vnode.data.staticClass) { - this.staticClass = this.staticClass.concat(this.$vnode.data.staticClass) + this.classes = this.classes.concat(this.$vnode.data.staticClass) delete this.$vnode.data.staticClass } }, diff --git a/lib/templates/nuxt-content.js b/lib/templates/nuxt-content.js index 6a883a92c..dcf9cec6a 100644 --- a/lib/templates/nuxt-content.js +++ b/lib/templates/nuxt-content.js @@ -126,7 +126,16 @@ export default { if (!body || !body.children || !Array.isArray(body.children)) { return } - data.class = (data.class || []).concat('nuxt-content') + let classes = [] + if (Array.isArray(data.class)) { + classes = data.class + } else if (typeof data.class === 'object') { + const keys = Object.keys(data.class) + classes = keys.filter(key => data.class[key]) + } else { + classes = [data.class] + } + data.class = classes.concat('nuxt-content') data.props = Object.assign({ ...body.props }, data.props) return h('div', data, body.children.map((child) => processNode(child, h, document))) } diff --git a/test/component.test.js b/test/component.test.js index bf4bc9c02..1ca1daddf 100644 --- a/test/component.test.js +++ b/test/component.test.js @@ -4,29 +4,78 @@ const { setup, loadConfig, url } = require('@nuxtjs/module-test-utils') describe('component', () => { let nuxt, browser, page - beforeAll(async () => { - ({ nuxt } = (await setup(loadConfig(__dirname)))) - browser = await createBrowser('puppeteer') - }, 60000) - - afterAll(async () => { - await nuxt.close() - await browser.close() - }) + describe('', () => { + beforeAll(async () => { + ({ nuxt } = (await setup(loadConfig(__dirname)))) + browser = await createBrowser('puppeteer') + }, 60000) + + afterAll(async () => { + await nuxt.close() + await browser.close() + }) + + test('has generated html', async () => { + page = await browser.page(url('/home')) + const html = await page.getHtml() + + expect(html).toContain('

Home

This is the home page!

') + }) + + test('has generated html with id', async () => { + page = await browser.page(url('/home?withId=true')) + const html = await page.getHtml() + + expect(html).toContain('

Home

This is the home page!

') + }) + + test('has generated html with class', async () => { + page = await browser.page(url('/home?withClass=true')) + const html = await page.getHtml() - test('nuxt-content has generated html', async () => { - page = await browser.page(url('/home')) - const html = await page.getHtml() + expect(html).toContain('

Home

This is the home page!

') + }) - expect(html).toContain('

Home

This is the home page!

') + test('has rendered a Vue.js component', async () => { + page = await browser.page(url('/vue-component')) + const html = await page.getHtml() + + expect(html).toMatch( + new RegExp(/
\s*

<\/h1>\s*
\s*
\s*
Header content<\/header>\s*
\s*Main content\s*<\/main>\s*