Skip to content
This repository was archived by the owner on Aug 9, 2023. It is now read-only.

Commit 972677b

Browse files
committed
Update Vue in tests
1 parent 38eb037 commit 972677b

File tree

3 files changed

+38
-105
lines changed

3 files changed

+38
-105
lines changed

index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,9 @@ function addAttribute(props, prop, value, ctx, name) {
214214
value = parseStyle(value, name)
215215
}
216216

217+
// Vue 3 (used in our tests) doesn’t need this anymore.
218+
// Some major in the future we can drop Vue 2 support.
219+
/* c8 ignore next 2 */
217220
if (ctx.vue) {
218221
if (info.property !== 'style') subprop = 'attrs'
219222
} else if (!info.mustUseProperty) {
@@ -281,6 +284,9 @@ function vdom(h) {
281284
* @returns {boolean}
282285
*/
283286
function vue(h) {
287+
// Vue 3 (used in our tests) doesn’t need this anymore.
288+
// Some major in the future we can drop Vue 2 support.
289+
/* c8 ignore next 3 */
284290
const node = /** @type {unknown} */ (h('div', {}))
285291
// @ts-expect-error Looks like a Vue node.
286292
return Boolean(node && node.context && node.context._isVue)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
"unist-util-remove-position": "^4.0.0",
7171
"vdom-to-html": "^2.0.0",
7272
"virtual-dom": "^2.0.0",
73-
"vue": "^2.0.0",
73+
"vue": "^3.0.0",
7474
"vue-server-renderer": "^2.0.0",
7575
"xo": "^0.48.0"
7676
},

test.js

Lines changed: 31 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
/**
22
* @typedef {import('hast').Element} HastElement
33
* @typedef {import('hast').Root} HastRoot
4-
* @typedef {import('vue').CreateElement} VueCreateElement
5-
* @typedef {import('vue').VNode} VueNode
64
*/
75

86
import process from 'node:process'
@@ -19,8 +17,8 @@ import {rehype} from 'rehype'
1917
import vToString from 'vdom-to-html'
2018
import {createElement as r} from 'react'
2119
import {renderToStaticMarkup as rToString} from 'react-dom/server.js'
22-
import Vue from 'vue'
23-
import VueSSR from 'vue-server-renderer'
20+
import * as vue from 'vue'
21+
import serverRenderer from '@vue/server-renderer'
2422
import {toH} from './index.js'
2523

2624
const processor = rehype().data('settings', {fragment: true})
@@ -404,123 +402,62 @@ test('hast-to-hyperscript', (t) => {
404402
t.end()
405403
})
406404

407-
t.test('should support `Vue`', (t) => {
408-
const baseline = doc.replace(/<div>/, '<div data-server-rendered="true">')
409-
/** @type {VueNode} */
410-
let actual
411-
/** @type {VueNode} */
412-
let expected
413-
414-
t.plan(3)
415-
416-
Promise.all([vueToString(actualRender), vueToString(expectedRender)])
417-
.then((all) => {
418-
const actualString = all[0]
419-
const expectedString = all[0]
420-
421-
t.deepEqual(clean(actual), clean(expected), 'equal syntax trees')
422-
t.deepEqual(html(actualString), html(baseline), 'equal output')
423-
424-
t.deepEqual(
425-
html(expectedString),
426-
html(baseline),
427-
'equal output baseline'
428-
)
429-
})
430-
.catch(
431-
/** @param {Error} error */ (error) => {
432-
t.ifErr(error, 'did not expect an error')
433-
}
434-
)
435-
436-
/** @param {import('vue').CreateElement} h */
437-
function actualRender(h) {
438-
actual = toH(h, hast)
439-
return actual
440-
}
441-
442-
/** @param {import('vue').CreateElement} h */
443-
function expectedRender(h) {
444-
expected = h(
445-
'div',
446-
{key: 'h-1', attrs: {'data-server-rendered': 'true'}},
447-
[
405+
t.test('should support `Vue`', async (t) => {
406+
const h = vue.h
407+
const actual = await serverRenderer.renderToString(
408+
vue.createSSRApp(() => toH(h, hast))
409+
)
410+
const expected = await serverRenderer.renderToString(
411+
vue.createSSRApp(() => {
412+
return h('div', {key: 'h-1', attrs: {'data-server-rendered': 'true'}}, [
448413
h(
449414
'h1',
450415
{
451416
key: 'h-2',
452-
attrs: {
453-
class: 'b c',
454-
id: 'a',
455-
hidden: true,
456-
height: 2
457-
}
417+
id: 'a',
418+
class: 'b c',
419+
hidden: true,
420+
height: 2
458421
},
459422
[
460423
'bravo ',
461424
h(
462425
'strong',
463426
{
464427
key: 'h-3',
465-
style: {color: 'red'},
466-
attrs: {
467-
foo: 'bar',
468-
camelCase: 'on off',
469-
'data-123': '456',
470-
'data-some': 'yes',
471-
'aria-valuenow': '1'
472-
}
428+
style: 'color: red',
429+
ignored: false,
430+
foo: 'bar',
431+
camelCase: 'on off',
432+
'data-some': 'yes',
433+
'data-123': '456',
434+
'aria-valuenow': '1'
473435
},
474436
['charlie']
475437
),
476438
' delta',
477439
h('input', {
478440
key: 'h-4',
479-
attrs: {
480-
checked: true,
481-
type: 'file',
482-
accept: '.jpg, .jpeg'
483-
}
441+
checked: true,
442+
type: 'file',
443+
accept: '.jpg, .jpeg'
484444
})
485445
]
486446
),
487447
h(
488448
'svg',
489449
{
490450
key: 'h-5',
491-
attrs: {
492-
xmlns: 'http://www.w3.org/2000/svg',
493-
viewBox: '0 0 500 500'
494-
}
451+
xmlns: 'http://www.w3.org/2000/svg',
452+
viewBox: '0 0 500 500'
495453
},
496-
[h('circle', {key: 'h-6', attrs: {cx: 120, cy: 120, r: 100}})]
454+
[h('circle', {key: 'h-6', cx: 120, cy: 120, r: 100})]
497455
)
498-
]
499-
)
500-
return expected
501-
}
502-
503-
/**
504-
* @param {VueNode} node
505-
* @returns {unknown}
506-
*/
507-
function clean(node) {
508-
remove(node)
509-
return json(node)
510-
}
456+
])
457+
})
458+
)
511459

512-
/**
513-
* @param {VueNode} node
514-
*/
515-
function remove(node) {
516-
let index = -1
517-
delete node.context
518-
if (node.children) {
519-
while (++index < node.children.length) {
520-
remove(node.children[index])
521-
}
522-
}
523-
}
460+
t.equal(actual, expected, 'equal output')
524461
})
525462

526463
t.test('should support keys', (t) => {
@@ -815,13 +752,3 @@ function html(doc) {
815752
function json(value) {
816753
return JSON.parse(JSON.stringify(value))
817754
}
818-
819-
/**
820-
* @param {(v: VueCreateElement) => VueNode} render
821-
* @returns {Promise.<string>}
822-
*/
823-
function vueToString(render) {
824-
return VueSSR.createRenderer({template: ''}).renderToString(
825-
new Vue({render}).$mount()
826-
)
827-
}

0 commit comments

Comments
 (0)