-
Notifications
You must be signed in to change notification settings - Fork 244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: renderToString implementation #1572
Changes from 1 commit
a237f34
9d365bc
2d6c6cc
22855ce
f8fc5a1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { defineComponent, onMounted, onServerPrefetch, ref } from 'vue' | ||
import { renderToString } from '../src' | ||
|
||
describe('renderToString', () => { | ||
it('returns a promise', async () => { | ||
const Component = defineComponent({ | ||
template: '<div>{{ text }}</div>', | ||
setup() { | ||
return { text: 'Text content' } | ||
} | ||
}) | ||
|
||
const wrapper = await renderToString(Component) | ||
|
||
expect(wrapper).toBe('<div>Text content</div>') | ||
}) | ||
|
||
it('returns correct html on multi root nodes', async () => { | ||
const Component = defineComponent({ | ||
template: '<div>foo</div><div>bar</div>' | ||
}) | ||
|
||
const wrapper = await renderToString(Component) | ||
|
||
expect(wrapper).toBe('<!--[--><div>foo</div><div>bar</div><!--]-->') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if we should use the |
||
}) | ||
|
||
it('returns correct html when onServerPrefetch is used', async () => { | ||
const Component = defineComponent({ | ||
template: '<div>{{ text }}</div>', | ||
setup() { | ||
const text = ref('') | ||
|
||
onServerPrefetch(() => { | ||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
text.value = 'Text content' | ||
resolve(true) | ||
}, 100) | ||
}) | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. onServerPrefetch sets the text to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe I can update this part to check if the rendered text is the text from pre-fetched data? Something like it('returns correct html with pre-fetched data on server', async () => {
function fakeFetch(text: string) {
return new Promise<string>((resolve) => {
setTimeout(() => {
resolve(text)
}, 100)
})
}
const Component = defineComponent({
template: '<div>{{ text }}</div>',
setup() {
const text = ref<string | null>(null)
onServerPrefetch(async () => {
text.value = await fakeFetch('onServerPrefetch')
})
onMounted(async () => {
if (!text.value) {
text.value = await fakeFetch('onMounted')
}
})
return { text }
}
})
const contents = await renderToString(Component)
expect(contents).toBe('<div>onServerPrefetch</div>')
}) Basing on this one https://vuejs.org/api/composition-api-lifecycle.html#onserverprefetch There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes I think that would be great 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated 👍🏼 |
||
|
||
onMounted(() => { | ||
text.value = 'Text content' | ||
}) | ||
|
||
return { text } | ||
} | ||
}) | ||
|
||
const wrapper = await renderToString(Component) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. won't it be confusing to have |
||
|
||
expect(wrapper).toBe('<div>Text content</div>') | ||
}) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be
Promise<string>
?